我尝试过以下语句的不同变体,结果是它必须做的一半,我不知道缺少什么。
SELECT DISTINCT COUNT(*) AS NO_RESERVATIONS, HG.RESV_ID, HR.BOOKING_CUS_ID AS BOOKED_BY, C.CUS_NAME
FROM HOLIDAY_RESERVATION HR INNER JOIN(HOLIDAY_GROUP HG INNER JOIN CUSTOMER C
ON HG.CUS_ID = C.CUS_ID)
ON HR.BOOKING_CUS_ID = HG.CUS_ID
WHERE HR.RESV_ID >= 2
GROUP BY HG.RESV_ID, HR.BOOKING_CUS_ID, C.CUS_NAME;
产量
NO_RESERVATIONS RESV_ID BOOKED_BY CUS_NAME
--------------- ---------- ---------- --------------------
1 3 5 Beatrice P. Rosa
1 8 15 Phillip B. Fleming
1 7 13 Debra V. Key
1 4 7 Magee A. Pace
2 11 3 Hadassah T. Hebert
1 5 9 Portia D. Melton
2 2 3 Hadassah T. Hebert
1 6 11 Larissa X. Boyer
1 9 17 Wayne F. Burnett
1 10 19 Eleanor J. Padilla
10 rows selected.
它应该只显示已经提出两次或更多预订的人,在这种情况下 Hadassah T. Hebert
ANSWER 感谢Brian的帮助
SELECT DISTINCT C.CUS_NAME AS BOOKED_BY, COUNT(*) AS NO_RESERVATIONS
FROM HOLIDAY_RESERVATION HR INNER JOIN(HOLIDAY_GROUP HG INNER JOIN CUSTOMER C
ON HG.CUS_ID = C.CUS_ID)
ON HR.BOOKING_CUS_ID = HG.CUS_ID
GROUP BY HG.RESV_ID, HR.BOOKING_CUS_ID, C.CUS_NAME
HAVING count(*) > 1;
产量
BOOKED_BY NO_RESERVATIONS
-------------------- ---------------
Hadassah T. Hebert 2
答案 0 :(得分:5)
要过滤掉分组结果,您需要使用HAVING语句。因此,对于您的示例,您的查询将如下所示:
SELECT DISTINCT COUNT(*) AS NO_RESERVATIONS, HG.RESV_ID, HR.BOOKING_CUS_ID AS BOOKED_BY, C.CUS_NAME
FROM HOLIDAY_RESERVATION HR INNER JOIN(HOLIDAY_GROUP HG INNER JOIN CUSTOMER C
ON HG.CUS_ID = C.CUS_ID)
ON HR.BOOKING_CUS_ID = HG.CUS_ID
WHERE HR.RESV_ID >= 2
GROUP BY HG.RESV_ID, HR.BOOKING_CUS_ID, C.CUS_NAME
HAVING count(*) > 1;