How to select closest date from today using Entity Framework? - c#

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 |
+----+-------+---------------------+-----------+

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 update stock table quantity on the basis of expiry date?

Hello everyone i have a table of inventory stock with columns as following
Item_ID | Expiry_Date | Quantity | Rate
cB0001 | 01-18-2021 | 5 | 150
cB0001 | 12-08-2020 | 3 | 145
cB0001 | 02-15-2021 | 25 | 155
Note : Rate is not an issue I just want to update the Stock Table Quantity on the basis on expiry date and available qty.
Now i want to sale inventory to customers of item cB0001 qty : 10 from the stock that expire first. Now the problem is that sale quantity is 10 and I want to minus the quantity 5 of expiry date 01-18-2021 and 3 From expiry date 12-08-2020 and 2 from 02-15-2021. Basically i want to implement FIFO logic using one query. I want to update the stock table quantity using this
update stock set quantity = quantity - '10' where item_id = 'cB0001' and expiry_Date < Now()
But this not giving accurate result. How to fix this problem? you have any idea?
I hope you understand my question
I assume you intend to do this in a trigger. So the 'trick' is to work out when the sale is fulfilled (working out IF it can be fulfilled is another issue). To do this allocate a value of 0 to rows where the cumulative quantity is less than the sale, 1 to the row where the sale can finally be fullfilled and 2 after that. I have assumed there is a unique row identifier to cope with multiple inventory entries for the same date - in my case inventory.id
drop table if exists inventory,sales;
create table inventory
(id int auto_increment primary key,Item_ID varchar(20), Expiry_Date date, Quantity int, Rate int);
create table sales (item_id varchar(20), quantity int);
insert into inventory values
( null,'cB0001' , str_to_date('01-18-2021','%m-%d-%Y'), 5 , 150),
( null,'cB0001' , str_to_date('12-08-2020','%m-%d-%Y'), 3 , 145),
( null,'cB0001' , str_to_date('02-15-2021','%m-%d-%Y'), 25 , 155),
( null,'cB0001' , str_to_date('02-15-2021','%m-%d-%Y'), 10 , 155),
( null,'cB0001' , str_to_date('02-15-2021','%m-%d-%Y'), 10 , 155),
( null,'cB0001' , str_to_date('02-15-2021','%m-%d-%Y'), 10 , 155),
( null,'cB0002' , str_to_date('02-15-2021','%m-%d-%Y'), 30 , 155);
drop trigger if exists t;
delimiter $$
create trigger t after insert on sales
for each row
begin
update inventory left join
(select s.*,
if(fst = 0, #runqty:=#runqty+quantity,#runqty:=#runqty) qty
from
(
select *,
#t:=#t+quantity cumqty,
if(#t>=#sale,if(#p>=1,2,#p:=1),#p:=0) fst
from inventory
cross join (select #t:=0,#p:=0,#sale:=new.quantity) t
where quantity > 0 and item_id = new.item_id
order by item_id,expiry_date
) s
cross join (select #runqty:=0) r
where fst in (0,1)
) a
on a.id = inventory.id
set inventory.quantity = case when fst = 0 then 0
when fst = 1 then inventory.quantity - (new.quantity - a.qty)
else inventory.quantity
end;
end $$
delimiter ;
MariaDB [sandbox]> insert into sales values('cb0001',10);
Query OK, 1 row affected (0.095 sec)
MariaDB [sandbox]> select * from inventory order by item_id,expiry_date;
+----+---------+-------------+----------+------+
| id | Item_ID | Expiry_Date | Quantity | Rate |
+----+---------+-------------+----------+------+
| 2 | cB0001 | 2020-12-08 | 0 | 145 |
| 1 | cB0001 | 2021-01-18 | 0 | 150 |
| 3 | cB0001 | 2021-02-15 | 23 | 155 |
| 4 | cB0001 | 2021-02-15 | 10 | 155 |
| 5 | cB0001 | 2021-02-15 | 10 | 155 |
| 6 | cB0001 | 2021-02-15 | 10 | 155 |
| 7 | cB0002 | 2021-02-15 | 30 | 155 |
+----+---------+-------------+----------+------+
7 rows in set (0.001 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> insert into sales values('cb0001',25);
Query OK, 1 row affected (0.111 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> select * from inventory order by item_id,expiry_date;
+----+---------+-------------+----------+------+
| id | Item_ID | Expiry_Date | Quantity | Rate |
+----+---------+-------------+----------+------+
| 2 | cB0001 | 2020-12-08 | 0 | 145 |
| 1 | cB0001 | 2021-01-18 | 0 | 150 |
| 3 | cB0001 | 2021-02-15 | 0 | 155 |
| 4 | cB0001 | 2021-02-15 | 8 | 155 |
| 5 | cB0001 | 2021-02-15 | 10 | 155 |
| 6 | cB0001 | 2021-02-15 | 10 | 155 |
| 7 | cB0002 | 2021-02-15 | 30 | 155 |
+----+---------+-------------+----------+------+
7 rows in set (0.001 sec)

Get record for each type with latest / bigger dates in C# DBML

I have a DB table which has some data as follows:
LINE | QTY | USERID | DATE
-----------------------------------------
1 | 5 | qb1 | 2015-03-02 11:23:25
2 | 1 | qb2 | 2015-03-02 18:24:03
3 | 3 | ch1 | 2015-03-03 05:38:49
1 | 2 | qb1 | 2015-03-03 08:47:02
2 | 4 | qb2 | 2015-03-03 14:01:31
3 | 2 | ch1 | 2015-03-03 21:11:53
1 | 4 | qb1 | 2015-03-04 09:34:04
2 | 5 | qb2 | 2015-03-04 15:29:27
3 | 1 | ch1 | 2015-03-04 19:28:33
As you can see I have only 3 unique LINE values in the DB. I require a LINQ query to select the latest record of every line. The date can be any date, I just need the latest status of the lines based on "DATE" field.
At the moment I am doing it very roughly something like this:
var line1 = db.GetTable<lnk_sts>().Where(x=> x.LINE== 1).OrderByDescending(x => x.DATE).FirstOrDefault();
Same for the other 2. What I Require is a list of lnk_sts with only the ones with a bigger date, in this case:
LINE | QTY | USERID | DATE
---------------------------------------
1 | 4 | qb1 | 2015-03-04 09:34:04
2 | 5 | qb2 | 2015-03-04 15:29:27
3 | 1 | ch1 | 2015-03-04 19:28:33
What you need to do is, group on Line and then take the first item in group after ordering in descending.
db.GetTable<lnk_sts>()
.GroupBy(x=>x.LINE)
.Select(x=>x.OrderByDescending(o=>o.DATE).FirstOrDefault())
.ToList();
First group by the line, then order by date and take the last item.
var result = db.GetTable<lnk_sts>().GroupBy(x=> x.LINE).Select(x => x.OrderBy(y => y.Date).Last())

C# Visual Studio 2008 - SQLite Update loop

I'm using C# and quite new to SQLite .
I need to update the record field "Value1" by dividing it by a given denominator for a Certain User and date range (First record date to a spicific Date eg. 2012-03-01).
So I need to loop through the data set and if User=John and Date <= than 2012-03-01 then divide Value1 by lets say 10
This obviously can't be done in SQL alone as I need to know what Value1 is, so that I can devided it. How would I go about doing this ?
Below is a example of what data I need to modify
User | Date | Value1 | Value2 | Value3
----------------------------------------------
John | 2012-01-01 | *50 | 5 | 80
John | 2012-02-01 | *100 | 78 | 60
John | 2012-03-01 | *10 | 100 | 5
John | 2012-04-01 | 5 | 89 | 55
John | 2012-05-01 | 15 | 50 | 44
Max | 2012-01-01 | 40 | 25 | 90
Max | 2012-02-01 | 99 | 79 | 30
Max | 2012-03-01 | 100 | 110 | 59
Max | 2012-04-01 | 25 | 29 | 85
Max | 2012-05-01 | 15 | 30 | 88
Modified data divided by 10
User | Date | Value1 | Value2 | Value3
----------------------------------------------
John | 2012-01-01 | *5 | 5 | 80
John | 2012-02-01 | *10 | 78 | 60
John | 2012-03-01 | *1 | 100 | 5
John | 2012-04-01 | 5 | 89 | 55
John | 2012-05-01 | 15 | 50 | 44
Max | 2012-01-01 | 40 | 25 | 90
Max | 2012-02-01 | 99 | 79 | 30
Max | 2012-03-01 | 100 | 110 | 59
Max | 2012-04-01 | 25 | 29 | 85
Max | 2012-05-01 | 15 | 30 | 88
If Value1 was constant then I could use the code below. But it is'nt and it will update 2012-01-01 to 2012-03-01 Value1 will all be 5.
Value1 = 50
Val = Value1 / 10
sql_cmd = sql_con.CreateCommand();
sql_cmd.CommandText = "UPDATE Tablename SET Value1= '"+Val+"' WHERE User = 'John' and Date <= '2012-03-01'";
sql_cmd.ExecuteNonQuery();
You really need to brush up on your SQL syntax, but generally, if you want to use fileds in the current row in an UPDATE statement, you can just put the equation (or string function or whatever is appropriate) in the UPDATE statement itself.
So, for your example, to divide Value1 by 10 for a particular row (or set of rows):
UPDATE Tablename SET Value1 = Value1/10 WHERE User = 'John' and Date <= '2012-03-01'
From your latest comment above, if you need additional conditions to select different rows, put those in the WHERE clause:
UPDATE Tablename SET Value1 = 1 WHERE User = 'John' and Date <= '2012-03-01' and Value1 = 10
So, maybe do some reading about SQL syntax generally (it's a big subject) and then repost any specific questions about syntax.

How to store weekly time availability in database?

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 |

Categories