Below are the tables:
relation table
| cid | pid |
|---|---|
| 101 | 202 |
| 566 | 322 |
| 875 | 345 |
people table
| id | name | gender |
|---|---|---|
| 101 | Riya | F |
| 566 | Aman | M |
| 202 | Rakesh | M |
| 875 | lucky | M |
| 202 | Reena | F |
| 322 | Raina | F |
| 345 | Rohit | M |
| 322 | Mohit | M |
| 345 | Meena | F |
output
| Child | Mother | Father |
|---|---|---|
| Riya | Reena | Rakesh |
| Aman | Raina | Mohit |
| Lucky | Rohit | Meena |
I tried this:
SELECT mother,
father
FROM (
SELECT id,
name,
sum(
CASE
WHEN gender = 'F' THEN 1
ELSE 0) AS mother,
sum (
CASE
WHEN gender = 'M' THEN 1
ELSE 0) AS father
FROM people
INNER JOIN relation
ON people. id = relation.p_id
GROUP BY id,
name) t1
INNER JOIN relation
ON relation.p_id = t1.id
Please let me know the query, for how to fetch this output. this above query does not work, I am not able to figure how to output child also.