简体   繁体   中英

MySQL - Group by multiple columns from same table

I have a table similar to the following

sno | booking_id | room_type | gender | age | amount | days
1   | 2016JUL001 | AC        | Male   | 25  | 1000   | 15 
2   | 2016JUL001 | AC        | Male   | 42  | 1000   | 15 
3   | 2016JUL001 | AC        | Male   | 28  | 1000   | 15 
4   | 2016JUL010 | N AC      | Female | 45  | 1000   | 15 
5   | 2016JUL010 | N AC      | Female | 46  | 1000   | 15 
6   | 2016JUL005 | N AC      | Male   | 28  | 1000   | 15 
7   | 2016JUL005 | N AC      | Female | 35  | 1000   | 15 
8   | 2016JUL009 | AC        | Female | 26  | 1000   | 15 
9   | 2016JUL009 | AC        | Female | 25  | 1000   | 15

... so on

Expected output [If I want to get gender='Female']

sno | booking_id | room_type | gender | age | amount | days
4   | 2016JUL010 | N AC      | Female | 45  | 1000   | 15 
5   | 2016JUL010 | N AC      | Female | 46  | 1000   | 15
8   | 2016JUL009 | AC        | Female | 26  | 1000   | 15 
9   | 2016JUL009 | AC        | Female | 25  | 1000   | 15

Expected output [If I want to get gender='Male']

sno | booking_id | room_type | gender | age | amount | days
1   | 2016JUL001 | AC        | Male   | 25  | 1000   | 15 
2   | 2016JUL001 | AC        | Male   | 42  | 1000   | 15 
3   | 2016JUL001 | AC        | Male   | 28  | 1000   | 15

Expected output [If I want to get gender='Male' AND gender='Female']

sno | booking_id | room_type | gender | age | amount | days
6   | 2016JUL005 | N AC      | Male   | 28  | 1000   | 15 
7   | 2016JUL005 | N AC      | Female | 35  | 1000   | 15 

NOTE: I want 3 separate individual QUERIES to get the above outputs

Thanks in advance

First query :

SELECT sno, booking_id, room_type, gender, age 
FROM customer_data 
WHERE booking_id IN ( SELECT booking_id FROM customer_data 
                      WHERE gender='female' AND age>0 and RIGHT(booking_id,1) <> '1' 
                      GROUP BY booking_id HAVING COUNT(*) > 1 ) 
ORDER BY booking_id ASC, age ASC

Second :

SELECT sno, booking_id, room_type, gender, age 
FROM customer_data 
WHERE booking_id IN ( SELECT booking_id FROM customer_data 
                      WHERE gender='male' AND age>0 
                      GROUP BY booking_id HAVING COUNT(*) > 1 ) 
ORDER BY booking_id ASC, age ASC

And third:

SELECT sno, booking_id, room_type, gender, age 
FROM customer_data 
WHERE booking_id IN ( SELECT booking_id FROM customer_data 
                      WHERE gender IN('male','female') AND age>0 
                      GROUP BY booking_id HAVING COUNT(distinct gender) = 2 ) 
ORDER BY booking_id ASC, age ASC

If in the first two you wanted only booking_id that has only 1 gender, add to the having clause :

AND COUNT(distinct gender) = 1

Your schema appears somewhat flawed. Nevertheless, here's something to think about...

SELECT booking_id
     , COUNT(DISTINCT gender) x 
  FROM customer_data 
 WHERE gender IN ('Male','Female') <-- not strictly necessary if there are only two genders.
 GROUP 
    BY booking_id;

After lot of tries, I am able to get the data I want

Query ['Female']

SELECT sno, bd.booking_id, bd.room_type, bd.gender, bd.age 
FROM customer_data bd 
INNER JOIN ( 
    SELECT booking_id, GROUP_CONCAT(DISTINCT gender) AS g 
    FROM customer_data 
    WHERE gender!='' AND age>0 
    GROUP BY booking_id 
    HAVING COUNT(booking_id) > 1 
    ORDER BY booking_id ASC, gender DESC 
) cbd 
WHERE cbd.booking_id = bd.booking_id AND cbd.g = 'Female'

Query ['Male']

SELECT sno, bd.booking_id, bd.room_type, bd.gender, bd.age 
FROM customer_data bd 
INNER JOIN ( 
    SELECT booking_id, GROUP_CONCAT(DISTINCT gender) AS g 
    FROM customer_data 
    WHERE gender!='' AND age>0 
    GROUP BY booking_id 
    HAVING COUNT(booking_id) > 1 
    ORDER BY booking_id ASC, gender DESC 
) cbd 
WHERE cbd.booking_id = bd.booking_id AND cbd.g ='Male'

Query ['Male and Female']

SELECT sno, bd.booking_id, bd.room_type, bd.gender, bd.age 
FROM customer_data bd 
INNER JOIN ( 
    SELECT booking_id, GROUP_CONCAT(DISTINCT gender ORDER BY gender DESC) AS g 
    FROM customer_data 
    WHERE gender!='' AND age>0 
    GROUP BY booking_id 
    HAVING COUNT(booking_id) > 1 
    ORDER BY booking_id ASC, gender DESC 
) cbd 
WHERE cbd.booking_id = bd.booking_id AND cbd.g = 'Male,Female'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM