A problem with ordering a search result by 2 columns.
My table: transit time is stored in seconds, appointment is time
id transit_time appointment
----------------------------
2 3845 09:00:00
11 22053 13:00:00
10 4852 08:00:00
11 5985 NULL
13 7221 12:45:00
14 3812 NULL
17 4256 NULL
18 5663 NULL
19 4725 NULL
I want to make a select that order the records by:
1. appointment IS NULL at the end
2. by appointment time ASC
3. if transit time is greater than appointment then that record should be on top of sorting at point 2
For example the right order should be:
id transit_time appointment
----------------------------
11 22053 13:00:00
13 10221 12:45:00
10 3852 08:00:00
2 4245 09:00:00
11 5985 NULL
18 5663 NULL
19 4725 NULL
17 4256 NULL
14 3812 NULL
I've tried many sorts with CASE but no luck to get that order by. Any help will be appreciated.
SELECT * FROM table WHERE ...
ORDER BY (CASE WHEN appointment IS NULL THEN 1 ELSE 0 END) asc,
transit_time desc
Convert to seconds to make the comparison
ORDER BY CASE WHEN time_appointment IS NULL THEN 1 ELSE 0 END ,
transit_time DESC,
CASE WHEN transit_time > TIME_TO_SEC(time_appointment) THEN transit_time ELSE NULL END
Related
I want show data according date wise with geoup by User ID.
The first shows the data stored in database and second table shows how i want to show the data on front end.
This is the table in Database :-
UserID Date Working Hrs
1 2021-08-01 10
2 2021-08-01 1
3 2021-08-01 15
1 2021-08-02 11
2 2021-08-02 11
3 2021-08-02 16
1 2021-08-03 9
2 2021-08-03 10
3 2021-08-03 11
This is the table i want to create from db table :-
UserID 2021-08-01 2021-08-02 2021-08-03
1 10 11 9
2 1 11 10
3 15 16 11
If your columns are static then you can use conditional aggregation. Otherwise use pivot or dynamic.
-- MySQL(v5.8)
SELECT userid
, MAX(CASE WHEN tdate = '2021-08-01' THEN working_hours END) "2021-08-01"
, MAX(CASE WHEN tdate = '2021-08-02' THEN working_hours END) "2021-08-02"
, MAX(CASE WHEN tdate = '2021-08-03' THEN working_hours END) "2021-08-03"
FROM test
GROUP BY userid
Please check from url https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=d364ddf6f942a54beddb2ba719d1932f
I have 3 mysql tables:
appointments
id slot_id patient_name doctor_id deleted_at
1 11 Tasin 23 2019-10-10
2 12 Nawaz 22 null
3 13 Rakib 23 null
4 14 Hossen 23 null
5 15 Aritra 24 null
6 16 Anik 22 null
7 17 Manik 22 null
doctors
id status doctor_name
22 1 Khaled
23 1 Hasan
24 0 Rumi
slots
id date duration time
11 2019-10-10 2900 01:01
12 2019-10-11 1200 02:01
13 2019-10-18 1100 03:01
14 2019-09-08 200 11:01
15 2019-08-01 500 01:31
16 2019-10-07 300 02:31
17 2019-10-02 1200 03:31
Now, I want to show a list of doctors with their total appointment durations in decreasing order using SQL query.
Unfortunately, I don't have any idea about this SQL query. Can you assist me?
SELECT DOCTOR_NAME, SUM(DURATION) FROM APPOINTMENTS A
JOIN DOCTORS D ON D.ID = A.DOCTOR_ID
JOIN SLOTS S ON S.ID = A.SLOT_ID
GROUP BY D.ID, DOCTOR_NAME
ORDER BY SUM(DURATION) DESC;
select d.doctor_id, d.doctor_name, sum(apt.duration) as total_duration from
doctors as d
join appointments as apt on apt.doctor_id = d.doctor_id
join slots as s on s.id = apt.slot_id
group by d.doctor_id, d.doctor_name
The above query should work fine.
There might be some typo as I didn't write it in the SQL management studio.
I have the below table:
id reference created_at closed_at
__ ______ ___________ __________
1 62506 2017-01-09 12:05:34 2017-01-09 16:14:55
2 62507 2017-01-09 12:09:47 NULL
3 62508 2017-01-10 12:09:48 NULL
4 62509 2017-01-10 12:11:15 NULL
5 62510 2017-01-10 12:12:41 2017-01-12 13:52:04
6 62511 2017-01-11 12:18:01 NULL
7 62512 2017-01-11 12:20:26 2017-01-15 11:39:31
8 62513 2017-01-11 12:29:19 NULL
9 62514 2017-01-12 12:37:11 NULL
10 62515 2017-01-12 12:43:31 NULL
11 62516 2017-01-12 13:20:49 NULL
12 62517 2017-01-12 13:30:58 2017-01-12 17:36:24
I would like a query that returns a rolling total of items and also how many we have 'open' that day (open being null in closed_at or a close date greater than the results rows date)
somedate Total Open
---------- ------ -----
2017-01-09 2 1
2017-01-10 5 4
2017-01-11 8 7
2017-01-12 12 10
I know i need to do some kind of cumulative count, but i'm rubbish and need your help :)
Here is one easy method. Get the unique dates and then use correlated subqueries:
select d.dte,
(select count(*) from t where t.created_at < d.dte + interval 1 day
) as total,
(select count(*) from t where t.closed_at < d.dte + interval 1 day
) as closed,
(select count(*) from t
where t.created_at < d.dte + interval 1 day and
(t.closed_at is null or t.closed_at >= d.dte + interval 1 day)
) as total
from (select date(created_at) as dte from t
union -- on purpose to remove duplicates
select date(closed_at) from t
) d;
The + interval 1 day is so the "day" is as of midnight of the day.
The lack of date() on the comparisons is so the correlated subqueries can use indexes.
The union in the subquery is so all dates with activity are counted, even if there are no new items (only closed items).
I am using MySQL, I have following table structure
Id id2 classId sectionId validFrom validTill
------------------------------------------------------
1 1 5 13 2016-01-01 2016-03-30
2 1 5 22 2016-01-15 2016-03-30
3 1 5 23 2016-01-15 2016-04-29
4 1 5 13 2016-04-01 2016-04-30
9 10 6 24 2016-01-17 2016-02-05
10 10 6 25 2016-01-23 2016-02-05
11 10 6 24 2016-01-31 2016-02-05
My SQL statement is
SELECT count(*) as timeCount FROM TimeTableClassSection a
WHERE classId=5 AND sectionId=13 AND ((a.ValidFrom BETWEEN '2016-01-18' AND '2016-01-24') OR (a.ValidTill BETWEEN '2016-01-18' AND '2016-01-24'))
Its returning timeCount = 0. But it should return 1 as record with Id=1 falls between this date range ('2016-01-18' AND '2016-01-24')
I am trying to achieve, find out any overlapping record for particular classId & sectionId between provided date range.
If classId=5 and sectionId=13 has validFrom=2016-01-01 validTill=2016-03-30 exist, then any date range between this date range ('2016-01-18' AND '2016-01-24') should throw this record as count.
If I give date range 2015-12-25 to 2016-09-20 then record count should = 1
If I give date range 2016-2-1 to 2016-02-20 then record count should = 1
If I give date range 2016-2-1 to 2016-09-20 then record count should = 1
What wrong I am doing here ... all date format is in YYYY-MM-DD
You are only checking if the boundaries are within the date range, but you do not check whether the data range is within the boundaries. You should extend your where criteria:
...AND ((a.ValidFrom BETWEEN '2016-01-18' AND '2016-01-24')
OR (a.ValidTill BETWEEN '2016-01-18' AND '2016-01-24')
OR (a.ValidFrom<'2016-01-18' AND a.ValidTill>'2016-01-24'))
my record:
SF_ID CardID Status Received_Date Meetup_Date
12 1 Yes 2015-01-12 2015-12-03
13 1 No 2015-12-01 NULL
14 1 No 2015-12-01 NULL
15 2 No 2015-12-02 NULL
16 2 No 2015-12-02 NULL
17 3 No 2015-12-01 NULL
18 4 No 2015-12-06 NULL
19 5 Yes 2015-11-30 2015-12-01
20 5 No 2015-11-30 NULL
22 5 No 2015-11-30 NULL
23 7 yes 2015-12-06 2015-12-07
Requirement #1:
all cardID where Received_Date is Minimum and Status
is No and Top 01 SF_ID. I've tried it in the following way without success:
SELECT CardSFID,
CardID,
CardSFShortDate
FROM CC_Shortfall AS [data]
WHERE ( CardSFShortDate = (SELECT TOP 1 PERCENT CardSFShortDate
FROM CC_Shortfall
WHERE CardID = [data].CardID) )
AND CardSFYesNo = 'No'
Requirement #2:
all cardID Where MeetUp_Date is Maximum and Status
is Yes and there are not any No Status under CardID
i've tried to do it in the following way without succcess:
SELECT CardSFID,
CardID,
CardSfShortRcvDate
FROM CC_Shortfall AS [data]
WHERE ( CardSfShortRcvDate = (SELECT Max(CardSfShortRcvDate)
FROM CC_Shortfall
WHERE CardID = [data].CardID) )
AND CardSFYesNo = 'Yes'