Grouping in Mysql

7nbnzgx9  于 2022-12-22  发布在  Mysql
关注(0)|答案(3)|浏览(122)

我需要得到顶级touristCount在每个月像一月赞比亚有4 touristCount我需要选择只有赞比亚一月等

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

这就是我所尝试过的

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

我需要的结果是

nationality    |     TouritIdCount   |      MonthNumber
  US                       2                     01
  UK                       1                     04
  US                       1                     05
  UK                       1                     11
  ZAMBIA                   1                     12
lnxxn5zx

lnxxn5zx1#

试试这个:

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
nbnkbykc

nbnkbykc2#

您可以使用

having COUNT(b.tourist_id) >= 2
dz6r00yl

dz6r00yl3#

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:

  1. Rank the nationalities' booking counts per month with RANK and only show the best ranked rows.
  2. 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_idusernameemailnationality
1Josephjoseph@mail.usUS
2Marymary@mail.usUS
3Abrahamabraham@mail.ukUK

bookings

booking_iduser_idbooking_datetour_id
112022-01-111
222022-01-111
332022-01-111
432022-01-222
512022-05-013
622022-05-013
712022-05-124
822022-05-124
932022-05-145
1032022-05-206
1132022-05-277

result

yearmonthnationalitybooking_count
20221UK2
20221US2
20225US4

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.

相关问题