So there are 2 date columns in a table In sql - mysql

I have table , with date format in YYYY-MM-DD.
Acc_No
On_board_date
Arrival_date
001
2022-02-01
2022-02-04
002
2022-03-10
2022-03-07
003
2022-03-12
2022-03-25
What I want is Acc_No's whose arrival date is +/- 5 days from on_board_date
which will result in like these
Acc_No
On_board_date
Arrival_date
001
2022-02-01
2022-02-04
002
2022-03-10
2022-03-07
This is what I have tried
select * from table
where on_board_date <= dateadd(arrival_date,5)
or on_board_date <= datesub(arrival_date,5)
Not getting desired result with this

You can try this :
select * from table
where on_board_date <= arrival_date + INTERVAL '5' day

I tried the one Akina suggested
WHERE DATEDIFF(On_board_date, Arrival_date) BETWEEN -5 AND 5
This worked.

Related

Combining last and first timestamp data of a group data in 1 row

I have a screenshot table and I want to get the user screenshot time starts and screenshot time ends. I want to create a query to be able to export the data to provide to my users.
Let's say this is my table data.
scs_id
scs_tracker_id
created_at
1
1000
2022-02-22 00:00:00
2
1001
2022-02-22 04:00:00
3
1000
2022-02-22 01:00:00
4
1002
2022-02-22 12:00:00
5
1001
2022-02-22 08:00:00
3
1000
2022-02-22 02:00:00
My expected output should be:
scs_tracker_id
screenshot_starts
screenshot_ends
1000
2022-02-22 00:00:00
2022-02-22 02:00:00
1001
2022-02-22 04:00:00
2022-02-22 08:00:00
1002
2022-02-22 12:00:00
2022-02-22 12:00:00
Code that I'm playing as of the moment:
SELECT
(SELECT MIN(created_at) FROM screen_shots GROUP BY scs_tracker_id ORDER BY scs_id ASC LIMIT 1) AS screenshot_starts,
(SELECT MAX(created_at) FROM screen_shots GROUP BY scs_tracker_id ORDER BY scs_id DESC LIMIT 1) AS screenshot_ends
FROM screen_shots
Aggregate by tracker ID and then take the min/max timestamp:
SELECT
scs_tracker_id,
MIN(created_at) AS screenshot_starts,
MAX(created_at) AS screenshot_ends
FROM screen_shots
GROUP BY scs_tracker_id;

SQL Order by 2 conditions

Let's say that I have a database, that looks like that:
price date hour
12.00 2018-12-11 5
13.00 2018-12-04 2
14.00 2018-12-06 1
15.00 2018-12-11 1
16.00 2018-12-04 6
17.00 2018-12-06 10
I need to order by date and if days are the same after hour, so results should be:
price date hour
13.00 2018-12-04 2
16.00 2018-12-04 6
14.00 2018-12-06 1
17.00 2018-12-06 10
15.00 2018-12-11 1
12.00 2018-12-11 5
I tried to write a simple query, but it couldn't take into account 2 conditions, one after another:
SELECT price, date, hour
FROM table
WHERE date BETWEEN '2018-12-04' AND '2018-12-11'
ORDER BY date, hour
Could anyone help with this issue ?
Thanks All !
The only real issue I can think of would be if hour were stored as a string. If so, use implicit conversion:
SELECT price, date, hour
FROM table
WHERE date BETWEEN '2018-12-04' AND '2018-12-11'
ORDER BY date, hour + 0;
A simple ORDER BY will suffice:
select * from my_table order by `date`, hour;
Please note the date is usually a reserved word, so its usage is discouraged, and needs to be quoted.

SQL Server : get next date of last minimum negative amount date with group by sum

I have a table like this:
date amt sr
----------------------
2013-07-01 -10 1
2013-07-02 40 2
2013-07-03 10 3
2013-07-04 -10 4
2013-07-06 10 5
2013-07-07 -10 6
2013-07-08 10 7
2013-07-09 10 8
2013-07-10 10 9
I want this result:
date amt sr
--------------------------
2013-07-08 60 7
Get next date of last minimum negative amount date
Assuming a table called date_value with a date and int columns called dat and value
Then you can get the desired result by:
SELECT (
SELECT MIN(dat) FROM dbo.date_value
WHERE dat > ( SELECT MAX(dat) FROM dbo.date_value WHERE val < 0 )
) AS 'date',
SUM(val) AS 'amt'
FROM dbo.date_value

Mysql Sum and Group By Method

Input
Date Points
Email
2012-07-01 5 a#sample.com
2012-07-01 6 b#sample.com
2012-07-01 2 c#sample.com
2012-07-02 5 d#sample.com
2012-07-03 8 e#sample.com
2012-07-03 7
f#sample.com
2012-07-03 1
y#sample.com
2012-07-04 3 x#sample.com
2012-07-04 2 f#sample.com
2012-07-05 3 g#sample.com
2012-07-05 9 b#sample.com
Output
Date Points
2012-07-01 13
2012-07-02 5
2012-07-03 16
2012-07-04 5
2012-07-05 12
Suggest me an MySQL Query for output like the above
try
SELECT `date`, SUM(`Points`) AS sumPoint FROM table GROUP BY `date`
Note : dont forget to wrap date column with ` as DATE is reserve keyword of mySQL
SELECT date, SUM(Points) AS sumPoint
FROM table
GROUP BY DATE(date)
Since date column might be datetime or date type in table

Datediff in date format

I have 2 tables with structure as
Emp Table
id name
001 Smith
002 Jerry
Leave
sr.no reason from_date to_date request_by status
1 PL 2011-12-11 2011-12-15 001 Declined
2 PL 2011-11-13 2011-11-13 001 Approved
3 PL 2011-10-02 2011-10-05 002 Declined
Now I have written this query
select DATEDIFF(Leave.from_date,Leave.to_date)as cdate,
Emp.id as emp
from Leave left join Emp
on Leave.request_by=Emp.id
gives me difference between these 2 dates like...
cdate emp
-4 001
0 001
-3 002
The first thing about this output difference between '2011-12-11 & 2011-12-15 ' need to be 5 as for 5 consecutive days employee is absent. That we achieve it.
But I need this cdate in date format like('%Y%m%d') and + if date difference is say -4 then 4 records should be displayed for that.
So I want to write a query which gives output like this......
cdate emp
2011-12-11 001
2011-12-12 001
2011-12-13 001
2011-12-14 001
2011-12-15 001
2011-11-13 001
2011-10-02 002
2011-10-03 002
2011-10-04 002
2011-10-05 002
So can anybody tell me what how should I need to write my query to get this output?
Try this query -
CREATE TABLE temp_days(d INT(11));
INSERT INTO temp_days VALUES
(0),(1),(2),(3),(4),(5),
(6),(7),(8),(9),(10),
(11),(12),(13),(14),(15); -- maximum day difference, add more days here
SELECT l.from_date + INTERVAL td.d DAY cdate, e.id emp
FROM
`leave` l
LEFT JOIN Emp e
ON l.request_by = e.id
JOIN temp_days td
ON DATEDIFF(l.to_date, l.from_date) >= td.d
ORDER BY
e.id