2

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

5
  • 1
    What is the question ? Commented Aug 22, 2016 at 11:02
  • Please read the complete post, I have mentioned the 3 expected outputs and the query to be modified @sagi Commented Aug 22, 2016 at 11:03
  • sathvik can you please expain your question Commented Aug 22, 2016 at 11:04
  • So you want 3 queries to create the 3 outputs you provided? Commented Aug 22, 2016 at 11:04
  • yeah, exactly @sagi Commented Aug 22, 2016 at 11:06

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

11 Comments

Booking ID's are not same as I mentioned.. It will change
Will change how? What's wrong? Are you expecting me to guess what you need?
Thanks. @Manishsharma
I mean there can be other bookings will have the number 1. you have mentioned booking_id NOT LIKE '%1%' in the query. The original format of booking id is YEARMONTHNUMBER [2016AUG0001]
Sorry for that, I mean to say is that will keep on increment and I have lots of rows and don't mention the booking id in such way.
|
1

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'

4 Comments

Looks very inefficient . A) Replace LIKE with = when looking for perfect match. B) You can order by the GROUP_CONCAT() so it will always be the same order and you won't have to look both Male,Female and Female,Male . Look at my having clause, you can add it in the inner query , add to the having clause HAVING MAX(gender) = 'female' for only females , HAVING MIN(gender) = 'man' for mens. and HAVING COUNT(DISTINCT GENDER) = 2 for both.
Yeah I have already optimized in my code @sagi. Thank you, I have answered because some other will get some idea from this. Anyways, I will also edit here.
I think HAVING MAX(gender) = 'female' this will not work in some cases
We are talking about your scenario , only man,female available. So if MAX returns female and not male(since male is larger than female on ASCI table) that it must be the only option. Same goes for man.
0

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;

2 Comments

By his query, I assume that there is also an empty string value for gender.
@sagi Yes, I think you're right - so it is necessary in this instance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.