Count records for previous month SQL - mysql

Can you help me with a simple task, but I can't really figure out, how I should do that request. Help, please.
I have:
doctors
id name profession
1 James Harden dental
2 James Jones therapist
3 LeBron James cardiologist
4 Kobe Braynt surgeon
5 Sabrina Williams nurse
6 Tyler Okonma speech therapist
patients
id name diagnostic
1 Mo Bamba tooth pulling out
2 Kaney West astma
3 Post Malone heart attack
4 Denzel Curry headache
5 Nicola Jokic stomac-ache
6 Dwayne Wade AIDS
visits
doctorId patientId visitDate
1 1 2019-03-09
2 4 2019-03-01
2 5 2019-02-26
2 6 2019-02-05
3 3 2019-03-03
4 2 2019-03-07
I need to show the doctors serving the largest number of patients in the previous month. I am lost in counting the patients which were serving by doctors. Can you suggest the request, please?

You need to group by doctorid the visits table and join to the doctors table:
select d.name, g.counter
from doctors d inner join (
select doctorid, count(distinct patientid) counter
from visits
where
year(visitdate) = year(current_date - interval 1 month)
and
month(visitdate) = month(current_date - interval 1 month)
group by doctorid
order by counter desc limit 3
) g on g.doctorid = d.id
order by g.counter desc, d.name
You can change limit 3 to what you like.

Try this
select d.name, count( distinct v.patientid) noofvisits
from visits v inner join
doctors d on v.doctorid=d.doctorid
where v.visitdate < '1-mar-2019'
group by d.name
order by noofvisits desc

Related

Choose the person who worked the most night shifts

How do you choose the employee who has worked the most night shifts of all time? There are 2 tables, one with workers, the second with night shifts, in which 2 people work per shift.
Users:
id
name
1
Oliver
2
Harry
3
Jacob
Hours:
id
NightShift1
NightShift2
1
1
3
2
2
2
3
3
1
4
3
2
5
2
2
6
1
2
7
1
3
8
3
1
To do this you can to essentially loop over the hours table twice; you do this by joining an ad hoc table specifying which shift you are looking at:
select users.id, users.name
from hours
join (select 1 position union all select 2) position
join users on users.id=if(position=1,hours.NightShift1,hours.NightShift2)
group by users.id
order by count(*) desc
limit 1
You can UNION the Hours table on top of itself and then group by the user id to see who has the highest count:
SELECT COUNT(*) as nightshiftcount, userid
FROM (
SELECT NightShift1 as userid FROM Hours
UNION ALL SELECT NightShift2 FROM Hours
) as hrs
ORDER BY nightshiftcount DESC
LIMIT 1
If you need the name, you can just INNER JOIN to that table in that outer FROM clause and pull that column through.

SQL Inner Join only one row based on filtered records

I have the following tables
user
id
name
1
John
2
Jack
3
George
essay
id
date_submitted
user_id
project_id
1
2020-12-10
1
1
2
2020-05-01
2
2
3
2020-04-10
3
2
project_phase
id
project_id
date
phase
1
1
2020-01-01
early
2
1
2020-05-31
mid
3
1
2020-11-30
late
4
2
2020-01-01
early
5
2
2020-03-31
mid
6
2
2020-04-30
late
1 User submits only 1 essay in one project
and I want to have a combined table which shows me the user and the phase at which the essay was submitted.
I'm trying to get the record with the MAX date from project_phase that is below the date_submitted
So using the above data I want to get
user
date_submitted
phase
John
2020-12-10
late
Jack
2020-05-01
late
George
2020-04-10
mid
so far I combined the tables with INNER JOIN but the following query returns duplicate records. I also tried ROW_NUMBER() and MAX() but I didn't structure it correctly to get the answer I'm looking for
SELECT
U.name,
E.date_submitted,
P.phase
FROM
essay E
INNER JOIN user U ON U.id = E.user_id
INNER JOIN project_phase P ON P.project_id = E.project_id and E.date <= P.date_submitted
Any help appreciated, thanks.
I would suggest a correlated subquery:
SELECT U.name, E.date_submitted,
(SELECT P.phase
FROM project_phase P
WHERE P.project_id = E.project_id AND
P.date <= E.date_submitted
ORDER BY p.date DESC
LIMIT 1
) as phase
FROM essay E JOIN
user U
ON U.id = E.user_id;

Get most recent groups ordered by users last login date DESC

The title makes it sound easy but what I'd like to do is get the last 20 groups from the groups table, ordered by their corresponding users last login date. A group can have one or more users and ultimately what I want to do is find out which groups have had the least user activity and retrieve the last login date.
Here's the query I came up with -
SELECT DISTINCT g.name, user_max.max_login_last_at FROM groups g
LEFT JOIN group_user gu on g.id = gu.group_id
LEFT JOIN (
SELECT MAX(login_last_at) max_login_last_at, u.id
FROM users u GROUP BY id
) AS user_max ON (user_max.id = gu.user_id)
ORDER BY user_max.max_login_last_at ASC
The problem is when I perform the join, it pulls in every group_user record and results in duplicates. I feel like there could be an easy solution to this one but I can't seem to figure it out!
groups table
id name
1 Group 1
2 Group 2
users table
id email login_last_at
1 user1#example.com 2018-10-17 16:08:47
2 user2#example.com 2018-10-02 15:41:53
3 user3#example.com NULL
4 user4#example.com 2018-10-08 12:01:48
5 user5#example.com 2018-10-15 9:24:57
6 user6#example.com 2018-10-17 11:10:58
7 user7#example.com 2018-10-17 15:33:03
group_user table
id group_id user_id
1 1 1
2 2 1
3 1 2
4 1 3
5 1 4
6 2 5
7 1 5
8 2 6
9 1 7
Current example result -
name max_login_last_ts
Group 1 2018-10-02 15:41:53
Group 1 2018-10-08 12:01:48
Group 2 2018-10-15 09:24:57
Group 1 2018-10-15 09:24:57
Group 2 2018-10-17 11:10:58
Group 1 2018-10-17 15:33:03
Group 1 2018-10-17 16:08:47
Group 2 2018-10-17 16:08:47
Group 1 2018-10-18 08:55:17
The problem is as you can see in the result above is that I'm getting all groups, all I really want is the following -
name max_login_last_ts
Group 2 2018-10-17 16:08:47
Group 1 2018-10-18 08:55:17
Thanks in advance!
I think this query will do what you want. There isn't enough data in your sample to replicate your desired results though.
SELECT g.name, MAX(u.login_last_at) AS max_login_last_at
FROM `groups` g
JOIN group_user gu on gu.group_id = g.id
JOIN users u ON u.id = gu.user_id
GROUP BY g.name
ORDER By max_login_last_at DESC
LIMIT 20
Demo on dbfiddle

SELECT sql with four different tables with primary key and foreign key

For my database, having these four table
First one, DEPARTMENT
//DEPARTMENT
D# DNAME
------------------
1 RESEARCH
2 IT
3 SCIENCE
Second one, EMPLOYEE
//Employee
E# ENAME D#
-----------------------
1 ALI 1
2 SITI 2
3 JOHN 2
4 MARY 3
5 CHIRS 3
Third, PROJECT
//PROJECT
P# PNAME D#
-----------------------
1 Computing 1
2 Coding 3
3 Researching 3
Fourth, WORKSON
//WORKSON
E# P# Hours
--------------------
1 1 3
1 2 5
4 3 6
So my output should be something like
E# ENAME D# TOTAL HOURS/W
--------------------------------------------
1 ALI 1 8
2 SITI 2 0
3 JOHN 2 0
4 MAY 3 6
5 CHIRS 3 0
Display 0 because the employee has no project to works on.
my currently statement using
SELECT E#,ENAME,D# and sum(Hours) as TOTAL HOURS/W
FROM EMPLOYEE,PROJECT,WORKSON
WHERE EMPLOYEE.P#
no idea how should it select
You should use an left join like this. You only need 2 tables employee and workson.
Try this query:
SELECT e_tbl.E#, e_tbl.ENAME, e_tbl.D#,
coalesce(SUM(w_tbl.Hours), 0) as "Total Hours/W"
FROM
EMPLOYEE e_tbl LEFT JOIN WORKSON w_tbl
ON e_tbl.E# = w_tbl.E#
GROUP BY e_tbl.E#
You need to use GROUP BY and JOINS , in order to achieve your output
SELECT E.E#,
E.ENAME,
E.D#,
sum(Hours) AS TOTAL HOURS/W
FROM Employee AS E
JOIN WORKSON AS W ON E.E# = W.E#
GROUP BY E.E#,
E.ENAME,E.D#
Use this :)
With the given output you do not need to join all the tables, and this could be done by joining employee and works on as
select
e.`E#`,
e.ENAME,
e.`D#`,
coalesce(tot,0) as `TOTAL HOURS/W`
from Employee e
left join
(
select `E#`,
sum(Hours) as tot
from WORKSON
group by `E#`
)w
on w.`E#` = e.`E#`
group by e.`E#`
DEMO

MySQL date reservation

I try to find a similar question but didn't find an exact one so I'll post mine:
I have 2 tables:
room (room_id)
room_reservation (room_id, date)
How can I return list of rooms that have at least 1 day free between 2 dates.
For example, if I have those rooms:
room_id 1, 2 & 3
then in room_reservation table, I have the following:
1 - 2012-07-22
1 - 2012-07-23
1 - 2012-07-24
1 - 2012-07-25
1 - 2012-07-26
2 - 2012-07-23
2 - 2012-07-24
then doing a search for available room between 2012-07-22 and 2012-07-26 should return only room #2 and #3.
I have this query so far but didn't work... please help!
SELECT DISTINCT room_id AS id
FROM room
WHERE (SELECT COUNT('listing') FROM listing_calendar WHERE listing = l.listing_id AND date BETWEEN '2012-07-22' AND '2012-07-26' GROUP BY listing) < 5"
Thanks for the help!
You can use this solution:
SELECT a.room_id
FROM rooms a
LEFT JOIN room_reservations b ON
a.room_id = b.room_id AND
b.date BETWEEN '2012-07-22' AND '2012-07-26'
GROUP BY a.room_id
HAVING COUNT(b.room_id) < DATEDIFF('2012-07-26', '2012-07-22') + 1
SQLFiddle Demo