Grouping in Mysql - mysql

i need to get the top touristCount in each month like January Zambia has 4 touristCount i need to select only Zambia for January and so on
user
`useri_id` | `username` | `email` | `nationality`
1 Joseph `` US
2 Abraham. `` UK
3 g.wood '' Zambia
4 Messi. '' France
5 Ronaldo. '' Namibia
6 Pogba. '' Holand.
bookings
booking_id | user_id | booking_date | tour_id
1 1 2022-01-01 1
2 1 2022-01-01 6
3 1 2022-05-01 2
4 3 2022-01-01 5
5 2 2022-04-01 5
6 2 2022-11-01 7
7 3 2022-12-01 2
8 6 2022-01-01 1
this is what i have tried
SELECT s.nationality AS Nationality,
COUNT(b.tourist_id) AS touristsCount,
MONTH(STR_TO_DATE(b.booked_date, '%d-%m-%Y')) AS `MonthNumber`
FROM bookings b, users s
WHERE s.user_id = b.tourist_id
AND YEAR(STR_TO_DATE(b.booked_date, '%d-%m-%Y')) = '2022'
GROUP BY Nationality,MonthNumber
order BY MonthNumber ASC
LIMIT 100
i need the results to be like
nationality | TouritIdCount | MonthNumber
US 2 01
UK 1 04
US 1 05
UK 1 11
ZAMBIA 1 12

Try this :
SELECT nationality, COUNT(booking_id) AS TouristIdCount, MONTH(booking_date) AS MonthNumber
FROM users u
JOIN bookings b ON u.user_id = b.user_id
WHERE YEAR(booking_date) = 2022
GROUP BY nationality, MonthNumber
ORDER BY TouristIdCount DESC, MonthNumber ASC

you can use
having COUNT(b.tourist_id) >= 2

You want to count bookings per month and tourist's nationality and then show only the top nationality (or nationalities) per month.
There are two very similar approaches:
Rank the nationalities' booking counts per month with RANK and only show the best ranked rows.
Select the top booking count per month and only show rows matching their top count.
The following query uses the second method. It shows one row per month and top booking nationality. Often there may be excatly one row for a month showing the one top booking nationality, but there may also be months where nationalities tie and share the same top booking count, in which case we see more than one row for a month.
select year, month, nationality, booking_count
from
(
select
year(b.booking_date) as year,
month(b.booking_date) as month,
u.nationality,
count(*) as booking_count,
max(count(*)) over (partition by year(b.booking_date), month(b.booking_date)) as months_max_booking_count
from bookings b
join users u on u.user_id = b.tourist_id
group by year(b.booking_date), month(b.booking_date), u.nationality
) ranked
where booking_count = months_max_booking_count
order by year, month, nationality;
As your own sample data doesn't contain any edge cases, here is some other sample data along with my query's result and an explanation. (In other words, this is what you should have shown in your request ideally.)
users
user_id
username
email
nationality
1
Joseph
joseph#mail.us
US
2
Mary
mary#mail.us
US
3
Abraham
abraham#mail.uk
UK
bookings
booking_id
user_id
booking_date
tour_id
1
1
2022-01-11
1
2
2
2022-01-11
1
3
3
2022-01-11
1
4
3
2022-01-22
2
5
1
2022-05-01
3
6
2
2022-05-01
3
7
1
2022-05-12
4
8
2
2022-05-12
4
9
3
2022-05-14
5
10
3
2022-05-20
6
11
3
2022-05-27
7
result
year
month
nationality
booking_count
2022
1
UK
2
2022
1
US
2
2022
5
US
4
In January there were two tours, but we are not interested in tours. We see four bookings, two by the Americans, two by the Britsh person. This is a tie, and we show two rows, one for UK and one for US with two bookings each.
In May there were five tours, but again, we are not interested in tours. There are seven bookings, four by the Americans, three by the Britsh person. So we only show US as the top country with four bookings here.

Related

count of users who've bought same item on a given day 1,2,3,...n in MySql

I using MySql version 5.7
I have a table that looks like this:
user_id item_id date
1 2 2020-01-01
1 2 2020-01-01
1 2 2020-01-01
1 3 2020-01-01
1 4 2020-01-01
33 7 2020-02-02
33 7 2020-02-02
44 11 2020-02-02
44 11 2020-02-02
I want to count number of users who have bought same item on a given day.
Desired result:
date one two three
2020-01-01 1 0 1
2020-02-02 0 2 0
one column = number of users who've bought same item once in a given day
two column = number of users who've bought same item twice in a given day.
Let me know if anything is unclear.
Thanks in advance!
Hmmm . . . Two levels of aggregation:
select date,
sum(cnt = 1) as one,
sum(cnt = 2) as two,
sum(cnt = 3) as three
from (select date, user_id, item_id, count(*) as cnt
from t
group by uesr_id, item_id
) ui
group by date

How to check if records within selected date using multiple tables?

It is an additional question to my previous one that is already answered.
There are 4 tables: buildings, rooms, reservations, information
1 building = n rooms
1 room = n reservations
TABLE BUILDINGS - ID(int), name(varchar)
TABLE ROOMS - ID(int), building_id(int)
TABLE RESERVATIONS - ID(int), room_id(int), date_start(datetime), date_end(datetime)
TABLE INFORMATION - ID(int), building_id(int), hours_start(int), hours_end(int)
Buildings table example
ID name
1 Building A
2 Building B
3 Building C
Rooms table example
ID building_id
1 1
2 1
3 2
4 3
Reservations table example
ID room_id date_start date_end
1 1 2014-08-09 14:00:00 2014-08-09 14:30:00
2 1 2014-08-09 14:30:00 2014-08-09 15:30:00
3 3 2014-08-09 16:30:00 2014-08-09 17:30:00
4 2 2014-08-09 16:00:00 2014-08-09 17:00:00
5 3 2014-08-09 16:00:00 2014-08-09 16:30:00
Information table example
ID building_id hours_start hours_end
1 1 9 22
2 2 8 20
3 3 8 22
Question
Can we filter buildings that has atleast 1 available room on selected date in any hour? Buildings working hours may be different (Information table).
I think this will do what you want. It calculates the total number of meeting hours in the building for all the rooms. It then calculates the total meeting hours. If a room is available the second is less than the first:
SELECT b.id, b.name,
sum(timestampdiff(minute, rv.date_start, rv.date_end))/60 as MeetingHours,
max(hours_end - hours_start)*count(distinct r.id) as BuildingHours
FROM buildings b JOIN
information bi
on b.id = bi.building_id
rooms r
ON b.id = r.building_id LEFT JOIN
reservations rv
ON rv.room_id = r.id AND
'2014-08-09' between date(rv.date_start) AND date(rv.date_end)
GROUP BY b.id
HAVING MeetingHours is Null or MeetingHours < BuildingHours;

MYSQL: Select from table A, based on seen date of user in Table B

OK I have three tables RegionName(Key, Name), Players(Key, Name, Seen), and regionplayers(key, regionkey, playerkey)
there is more to each table but it isn't needed so to make this easier i have only included what is needed.
I have regions, and a player can be added to a region, a region may have many players, and players can be on many regions. there are 1680 regions, 900ish players and about 3500 entries in the table linking them together.
I want to be able to find regions, where none of the players have been seen in 8+ days.
what I have atm is:
SELECT RegionName.*, RegionPlayer.*, Players.*
FROM RegionName
JOIN RegionPlayer
ON RegionPlayer.Regionkey= RegionName.Key
JOIN Players
ON Players.Key = RegionPlayer.Playerkey
WHERE
( Seen <= (NOW() - INTERVAL 8 DAY ) ) )
AND RegionName.Perent = 'none'
ORDER BY `RegionName`.`Name` ASC,
Players.Seen DESC
currently I get all the regions where people who have not been seen for 8+ days, so if a region has 1 player who hasn't been seen, it is returned, but I only want regions where everyone has been away for 8+days.
Here is a sort of sampling if the data for each of the tables, this was done by me just now. because its just easier then trying to pull enough data that makes the point, and should give the results I want.
RegionName
key Name
1 regionone
2 regiontwo
3 regionthree
4 regionfouor
5 regionfive
Players
Key Name Seen
1 jack 2014-03-21 12:43:46
2 joe 2014-03-26 12:43:46
3 bob 2014-03-20 12:43:46
4 bill 2014-03-19 12:43:46
5 dave 2014-03-17 12:43:46
6 tina 2014-03-28 12:43:46
7 tony 2014-03-29 12:43:46
8 george 2014-03-15 12:43:46
9 sam 2014-03-18 12:43:46
10 frank 2014-03-18 12:43:46
RegionPlayer
key Regionkey PlayerKey
1 1 1
2 1 4
3 1 5
4 2 1
5 2 4
6 2 2
7 3 6
8 3 1
9 3 7
10 4 1
11 4 8
12 4 7
13 4 5
14 5 3
So I should based on this data get back regions regionone, & regionfive.
SELECT RegionName.*, RegionPlayer.*, Players.*
FROM RegionName
JOIN RegionPlayer
ON RegionPlayer.Regionkey= RegionName.Key
JOIN Players
ON Players.Key = RegionPlayer.Playerkey
WHERE RegionName.Perent = 'none'
GROUP BY RegionName.Name
HAVING
SUM( Seen > (NOW() - INTERVAL 8 DAY ) ) =0
ORDER BY `RegionName`.`Name` ASC,
Players.Seen DESC

Not getting the right expected output for my Mysql Query?

I've 4 tables as shown below
doctors
id name
------------
1 Mathew
2 Praveen
3 Rosie
4 Arjun
5 Denis
doctors_appointments
id doctors_id patient_name contact date status
--------------------------------------------------------------------------------------
1 5 Nidhin 9876543210 2012-12-10 15:39:41 Registered
2 5 Sunny 9876543210 2012-12-18 15:39:48 Registered
3 5 Mani 9876543210 2012-12-12 15:39:57 Registered
4 2 John 9876543210 2012-12-24 15:40:09 Registered
5 4 Raj 9876543210 2012-12-05 15:41:57 Registered
6 3 Samuel 9876543210 2012-12-14 15:41:33 Registered
7 2 Louis 9876543210 2012-12-24 15:40:23 Registered
8 1 Federick 9876543210 2012-12-28 15:41:05 Registered
9 2 Sam 9876543210 2012-12-12 15:40:38 Registered
10 4 Sita 9876543210 2012-12-12 15:41:00 Registered
doctors_dutyplan
id doctor_id weeks time no_of_patients
------------------------------------------------------------------
1 1 3,6,7 9:00am-1:00pm 10
2 2 3,4,5 1:00pm-4:00pm 7
3 3 3,6,7 10:00am-2:00pm 10
4 4 3,4,5,6 8:30am-12:30pm 12
5 5 3,4,5,6,7 9:00am-4:00pm 30
emp_leave
id empid leavedate
--------------------------------
1 2 2012-12-05 14:42:36
2 2 2012-12-03 14:42:59
3 3 2012-12-03 14:43:06
4 3 2012-12-06 14:43:14
5 5 2012-12-04 14:43:24
My task is to find all the days in a month in which the doctor is available excluding the leave dates.
My query what is wrote is given below:
SELECT DATE_ADD( '2012-12-01', INTERVAL
ROW DAY ) AS Date,
ROW +1 AS DayOfMonth
FROM (
SELECT #row := #row +1 AS
ROW FROM (
SELECT 0
UNION ALL SELECT 1
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
)t1, (
SELECT 0
UNION ALL SELECT 1
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
)t2, (
SELECT #row := -1
)t3
LIMIT 31
)b
WHERE DATE_ADD( '2012-12-01', INTERVAL
ROW DAY )
BETWEEN '2012-12-01'
AND '2012-12-31'
AND DAYOFWEEK( DATE_ADD( '2012-12-01', INTERVAL
ROW DAY ) ) =2
AND DATE_ADD( '2012-12-01', INTERVAL
ROW DAY ) NOT
IN (
SELECT DATE_FORMAT( l.leavedate, '%Y-%m-%d' ) AS date
FROM doctors_dutyplan d
LEFT JOIN emp_leave AS l ON d.doctor_id = l.empid
WHERE doctor_id =2
)
This works fine for all doctors who took any leave in a particular day in a month (here in the example it is Decemeber 2012). and the result for the above query is shown below:
Date DayOfMonth
-----------------------
2012-12-10 10
2012-12-17 17
2012-12-24 24
2012-12-31 31
But on the other hand for the doctors who didn't took any leave , for that my query is showing empty table, example for the doctor Mathew whose id is 1, my query returns an empty result
can anyone please tell a solution for this problem.
Thanks in advance.
Your query is large, but this part looks fishy:
NOT IN (
SELECT DATE_FORMAT( l.leavedate, '%Y-%m-%d' ) AS date
FROM doctors_dutyplan d
LEFT JOIN emp_leave AS l ON d.doctor_id = l.empid
WHERE doctor_id =2
The left join means a null would be returned for doctor 1. Now, col1 not in (null) does not behave as you may expect. It translates to:
col1 <> null
Which is never true. You could solve this by changing the left join to an inner join, so an empty set instead of null is returned for a doctor without leave.

mysql query but two differant group by

If my Data is
Name - playerID - matchID - Innings - Runs
James 1 1 1 5
James 1 1 2 8
Darren 2 1 1 3
Darren 2 1 2 9
James 1 2 1 10
James 1 2 2 12
Darren 2 2 1 13
Darren 2 2 2 19
and my sql data is
$query = "SELECT playerID, name,
SUM(runs) AS runs_scored,
MAX(runs) AS highest_score
FROM matchPlayer GROUP BY playerID";
Then the output would read
James has scored 35 runs with a highest score of 18
Darren has scored 44 runs with a highest score of 19
Now I wish to get the highest total scored in one match (that is combining innings 1 & 2)?
I have no idea how to start on this query :(
EDIT
The exact info I require is the HIGHEST match total, so James has 13 combined runs from matchID 1 and 22 combined runs from matchID 2 - so the answer I am after is 22.
You need to do it in two stages:
SELECT ms.playerID, mp.name, SUM(ms.runs_by_match) AS runs_scored,
MAX(ms.runs_by_match) as highest_score
FROM
matchPlayer as mp
INNER JOIN (
SELECT playerID, matchID, SUM(runs) AS runs_by_match
FROM matchPlayer
GROUP BY playerID, matchID
) AS ms ON mp.playerID = ms.playerID
GROUP BY
ms.playerID, mp.name