How to store weekly time availability in database? - c#

I am programming an appointment system for doctors office. I need to store weekly availability of doctor. Something like this:-
DoctorID | 10AM | 11AM | 12AM | 1PM | 2PM | 3PM | 4PM |
------------------------------------------------------------------
[MON] 5477J | 1 | 0 | AppointID | 1 | 1 | AppointID | 0 |
------------------------------------------------------------------
[TUE] 5477J | 0 | 1 | AppointID | 1 | 1 | AppointID | 0 |
------------------------------------------------------------------
I am storing the time slots as numeric 1 implies **avaialble** 0 implies **will not be in office** and if there is an appointment booked then the timeslot will be replaced by AppointmentID.
The availability is going to differ each day. My question is How do I store for availability for each day? Should I have 5 rows per weekly schedule. Can someone point me to some simple schema or is there a better way to do this.

One model I've used in the past is something like this:
TABLE [AppointmentBlock]
- [DoctorID] INT
- [StartTime] DATETIME
- [EndTime] DATETIME
- [IsAvailable] BIT
- [IsRecurring] BIT
- [AppointmentID] INT
If [IsRecurring] = 1, this would represent a block that recurs at the same time every week, and you should assume that start / end times are offsets calculated from the start of the week.
If [IsRecurring] = 0, this would just be a one-time occurrence and start / end times are absolute.
So each block in a doctor's schedule is accounted for, even if it doesn't fall precisely on each hour. Some strict business logic needs to be layered on top of this to ensure that no overlaps are created (unless for some reason you want to allow this).
To fit your example to this model you'd have something like this:
DoctorID | Start | End | IsAvailable | IsRecurring | AppointmentId |
----------------------------------------------------------------------------
5477J | 10AM Mon | 11AM Mon | 1 | 1 | NULL |
5477J | 11AM Mon | 12PM Mon | 0 | 1 | NULL |
5477J | 12PM Mon | 1PM Mon | 0 | 1 | AppointID |
5477J | 1PM Mon | 3PM Mon | 1 | 1 | NULL |
5477J | 3PM Mon | 4PM Mon | 0 | 1 | AppointID |
5477J | 4PM Mon | 5PM Mon | 0 | 1 | NULL |
5477J | 10AM Tue | 11AM Tue | 0 | 1 | NULL |
5477J | 11AM Tue | 12PM Tue | 1 | 1 | NULL |
5477J | 12PM Tue | 1PM Tue | 0 | 1 | AppointID |
5477J | 1PM Tue | 3PM Tue | 1 | 1 | NULL |
5477J | 3PM Tue | 4PM Tue | 0 | 1 | AppointID |
5477J | 4PM Tue | 5PM Tue | 0 | 1 | NULL |

Related

How to list the products under each category in sql? side by side

I'm a bit of a beginner in sql and I need your help.
I'm sorry that I don't know if the question is correct.
now the code gives this;
+---------+----------+-------------+------------+
| id | CompanyId| DealId | price |
+---------+----------+-------------+------------+
| 1 | 1 | 1 | 100 |
| 2 | 1 | 2 | 50 |
| 3 | 1 | 3 | 25 |
| 4 | 2 | 1 | 1000 |
| 5 | 2 | 2 | 2000 |
| 6 | 2 | 3 | 2500 |
+---------+----------+-------------+------------+
but this is what i want;
+---------+----------+-------------+------------+------------+--+
| id | companyId| DealName1 | DealName2 | DealName3 | |
+---------+----------+-------------+------------+------------+--+
| 1 | 1 | 100 | 50 | 25 | |
| 2 | 2 | 1000 | 2000 | 2500 | |
| 3 | 3 | value | value | value | |
| 4 | 4 | value | value | value | |
+---------+----------+-------------+------------+------------+--+
select CompanyId
,[1] as DealName1
,[2] as DealName2
,[3] as DealName3
from (select CompanyId, DealId, price from t) t
pivot (sum(price) for DealId in([1],[2],[3])) p
CompanyId
DealName1
DealName2
DealName3
1
100
50
25
2
1000
2000
2500
Fiddle

How to select closest date from today using Entity Framework?

I have a MySQL database like this
Id Name StartTime
1 test1 2021-02-21 08:15:00
2 test2 2021-02-11 18:00:00
3 test3 2021-02-24 14:00:00
4 test4 2021-02-10 10:30:00
5 test5 2021-02-23 09:00:00
6 test6 2021-02-26 16:00:00
7 test7 2021-02-23 12:00:00
8 test8 2021-02-15 17:00:00
I want to get 5 closest date from today (2021-02-20).
I expected a result like this :
2021-02-26
2021-02-24
2021-02-23
2021-02-21
2021-02-15
I tried with this code :
var result = _context.EventItems
.FromSqlRaw("Select * from Event ORDER BY ABS(`StartTime` - CURRENT_TIMESTAMP()) LIMIT 5")
.Select(e => e.StartTime.Date)
.ToList();
and the result is :
2021-02-24
2021-02-23
2021-02-23
2021-02-21
2021-02-15
The problem is the duplicate 2021-02-23. How can I do to resolve my problem? Thank you !!
Consider the following; I've used '12:00:00' (as opposed to '00:00:00') as the comparison time because it seems more intuitive to me that the 24th is closer to the 20th than the 15th is.
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(id SERIAL PRIMARY KEY
,name VARCHAR(12) NOT NULL UNIQUE
,start_time DATETIME NOT NULL
);
INSERT INTO my_table VALUES
(1,'test1','2021-02-21 08:15:00'),
(2,'test2','2021-02-11 18:00:00'),
(3,'test3','2021-02-24 14:00:00'),
(4,'test4','2021-02-10 10:30:00'),
(5,'test5','2021-02-23 09:00:00'),
(6,'test6','2021-02-26 16:00:00'),
(7,'test7','2021-02-23 12:00:00'),
(8,'test8','2021-02-15 17:00:00');
SELECT *
, SEC_TO_TIME(ABS(UNIX_TIMESTAMP(start_time)-UNIX_TIMESTAMP('2021-02-20 12:00:00')))x
FROM my_table
ORDER
BY x ;
+----+-------+---------------------+-----------+
| id | name | start_time | x |
+----+-------+---------------------+-----------+
| 1 | test1 | 2021-02-21 08:15:00 | 20:15:00 |
| 5 | test5 | 2021-02-23 09:00:00 | 69:00:00 |
| 7 | test7 | 2021-02-23 12:00:00 | 72:00:00 |
| 3 | test3 | 2021-02-24 14:00:00 | 98:00:00 |
| 8 | test8 | 2021-02-15 17:00:00 | 115:00:00 |
| 6 | test6 | 2021-02-26 16:00:00 | 148:00:00 |
| 2 | test2 | 2021-02-11 18:00:00 | 210:00:00 |
| 4 | test4 | 2021-02-10 10:30:00 | 241:30:00 |
+----+-------+---------------------+-----------+

Grouping rows for calculation using LINQ to SQL

I have a c# program that I am programing where someone inputs production for the whole day like this:
Date | Part Number | Mold Num | Machine Num | Total Parts |Cycle
2/12/2016 | 1185-5B8 | 6580 | 12 | 220 | 56
2/12/2016 | 2249300 | 7797 | 36 | 600 | 13
2/12/2016 | 146865 | 5096789 | 12 | 500 | 15
2/16/2016 | 123456 | 7787 | 56 | 300 | 34
2/16/2016 | 123456 | 787 | 54 | 360 | 36
2/16/2016 | 123456 | 777 | 56 | 500 | 46
2/16/2016 | 123456 | 87 | 54 | 400 | 44
I then calculate a machine usage (MU) for each entry like so:
Date | Part Number | Mold Num | Machine Num | MU
2/12/2016 | 1185-5B8 | 6580 | 12 | .428
2/12/2016 | 2249300 | 7797 | 36 | .271
2/12/2016 | 146865 | 5096789 | 12 | .260
2/16/2016 | 123456 | 7787 | 56 | .354
2/16/2016 | 123456 | 787 | 54 | .45
2/16/2016 | 123456 | 777 | 56 | .799
2/16/2016 | 123456 | 87 | 54 | .611
What I want to do is to make something like a pivot table and it takes all similar dates/Mold Numbers/Machine Numbers and makes an average the MU and display it in any way that I want. Example:
Date | MU
2/12/2016 | 32.0%
2/16/2016 | 55.4%
or
Machine Num. | MU
12 | 34.4%
36 | 27.1%
54 | 53.0%
56 | 57.6%
etc. Basically I want it to be variable and to show whatever the person that is looking at it needs. I have looked for ways of doing this but they either did not make sense or not quite what I was looking for. Please keep in mind that I am very new to c# and LINQ to SQL

ORDER BY year and date in mysql and gridview

im having salary table,in which im storing salary details with corresponding month and year and payment date.Im displaying these data in gridview in my ASP.net c# application. i want to display the data like latest in first page.
Below my sample salary database:
+------------+-------+----------+------+----------+------+-------------+
| EmployeeID | Gross | TotalDed | Net | Month | Year | paymentdate |
+------------+-------+----------+------+----------+------+-------------+
| 2066 | 2219 | 3750 | 1531 | January | 2016 | 30.01.2016 |
| 2023 | 2218 | 1649 | 570 | January | 2016 | 30.01.2016 |
| 2001 | 2219 | 3750 | 1531 | October | 2015 | 30.10.2015 |
| 2023 | 2218 | 1649 | 570 | October | 2015 | 30.10.2015 |
| 2034 | 2328 | 5728 | 3400 | October | 2015 | 30.10.2015 |
| 2023 | 2218 | 1649 | 570 | November | 2015 | 30.11.2015 |
| 2030 | 2219 | 1550 | 669 | November | 2015 | 30.11.2015 |
| 2047 | 2218 | 1649 | 570 | November | 2015 | 30.11.2015 |
| 2031 | 2219 | 8450 | 6231 | December | 2015 | 30.12.2015 |
| 2057 | 2219 | 8450 | 6231 | December | 2015 | 30.12.2015 |
| 2023 | 2218 | 1649 | 570 | December | 2015 | 30.12.2015 |
+------------+-------+----------+------+----------+------+-------------+
i want this table to display in gridview with pagination.
String sQuery = #"SELECT EmployeeID,GrossSalary,TotalDed,NetSalary,Month,Year,paymentdate
FROM salary ";
MySqlDataAdapter ada = new MySqlDataAdapter(sQuery, GlobalCS.objMyCon);
using (DataTable dt = new DataTable())
{
ada.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
But it displays latest to last page.
if i use
SELECT EmployeeID,GrossSalary,TotalDed,NetSalary,Month,Year,paymentdate FROM salary order by Paymentdate
then it displays Jan2016,oct 2016,nov 2016,dec 2016.
But what i want to display as jan2016,dec 2015, nov 2015, oct 2015
I tried
SELECT EmployeeID,GrossSalary,TotalDed,NetSalary,Month,Year,paymentdate FROM salary order by month and year
the output is mixing of all data. So how can i get that?
If your Paymentdate is string datatype then order by Paymentdate order your results in Lexicographical order.
Before order convert Paymentdate to date.
SELECT
`EmployeeID`,
`GrossSalary`,
`TotalDed`,
`NetSalary`,
`Month`,
`Year`,
`paymentdate`
FROM salary
order by STR_TO_DATE(`paymentdate`,'%d.%m.%Y') DESC
sql fiddle demo
Note :- Its a bad practice to store the date data in string datatype instead of date datatype

MYSQL - Rows to Columns using join statement

I have to get data from 2 tables using join.1st table Name is sliprecord
+-------+-----------+-----------------------+---------+
| cid | cname | date | totalAmount |
+-------+-----------+-----------------------+---------+
| 8 | Umer | 2015-12-15 | 1000 |
| 9 | Shakir | 2015-12-20 | 2000 |
+-------+-----------+-----------------------+---------+
Another table Name is expense
+-------+-----------+-----------------------+---------+
| idExpense | title | date | amount |
+-------+-----------+-----------------------+---------+
| 1 | BreakFast | 2015-12-15 | 300 |
| 2 | Lunch | 2015-12-15 | 500 |
| 3 | Dinner | 2015-12-17 | 700 |
+-------+-----------+-----------------------+---------+
I want to create balance sheet, If sliprecord Table don't have a date in expense Table then it should also give me sliprecord date and sliprecord totalAmount .
And If expense don't have a date in sliprecord Table then it should also give me expense title,expense date,and expense amount .
Desired Out put should be like this:
+-------+-----------+-----------------------+---------+
| title | EXP_date | amount | Slip_date | totalAmount
+-------+-----------+-----------------------+---------+
| BreakFast | 2015-12-15 | 300 |2015-12-15 | 1000
| Lunch | 2015-12-15 | 500 | |
| Dinner | 2015-12-17 | 700 |
| | | |2015-12-20 | 2000
+-------+-----------+-----------------------+---------+

Categories