How do I display a guest's full name (family_name, given_name) from another table using only guest_id from the first table?

53 Views Asked by At

For my question I am to create a query showing the guest's full name that have made more than 2 bookings. It is to show the (family_name, given_name) in one column under the alias "Guest" AND alias "Count(*)" which what assume, counts the the guest_id from a2_booking (table1) which must be more than 2. The second table, "a2_guest", contains the guest_id (same as in a2_booking), family_name, given_name. What can I do to get the following query:

Guest                                                                                COUNT(*)
-- ------------------------------------------------------------------------------------ ----------
-- Fellows, John                                                                               3
-- Gaye, Anthony                                                                               3
-- Grace, Thomas                                                                               3
-- Marvin, Leon                                                                                4
-- Oslovski, Boris                                                                             3
-- Rivers, Jill                                                                                3
-- Slater, Martin                                                                              3
-- Strettle, George

I only managed to use CONCAT to combine the family_name and given_name to one column on the first try and then I tried to count the guests that had multiple bookings made (but I didn't do the calculation greater than 2 yet).

1

There are 1 best solutions below

0
Nicholas Rohrmoser On

Apologies all. I managed to find an answer that really helped.

SELECT x.family_name || ', ' || x.given_name AS "Guest", COUNT(y.guest) as 
"COUNT(*)"
FROM a2_guest x JOIN a2_booking y
ON (x.guest_id = y.guest)
GROUP BY x.family_name || ', ' || x.given_name
HAVING COUNT(y.guest) > 2;

I appreciate the help though ;)