I've tried writing my sql query to select multiple records on to one row but it isn't working the way I expected it to Currently my table looks something like this
| person id | fruit |
|---|---|
| 1 | apple |
| 1 | orange |
| 1 | banana |
| 2 | apple |
| 2 | orange |
| 3 | apple |
I've tried using CASE and GROUP BY but it just gave extra records and didn't display the way I wanted it to and is displaying like this
SELECT DISTINCT
F.MEMBER
,F.GIVEN_NAMES
,F.SURNAME
--VALUES NEEDED
,CASE WHEN F.VALUE_NEEDED = 'Postal Address' THEN 'Yes' ELSE '' END POSTAL_ADDRESS
,CASE WHEN F.VALUE_NEEDED = 'Birthday' THEN 'Yes' ELSE '' END BIRTHDAY
,CASE WHEN F.VALUE_NEEDED = 'Email Address' THEN 'Yes' ELSE '' END EMAIL_ADDRESS
,CASE WHEN F.VALUE_NEEDED = 'First Name' THEN 'Yes' ELSE '' END FIRST_NAME
,CASE WHEN F.VALUE_NEEDED = 'Surname' THEN 'Yes' ELSE '' END SURNAME
,CASE WHEN F.VALUE_NEEDED = 'Title and Gender' THEN 'Yes' ELSE '' END 'TITLE|GENDER'
,CASE WHEN F.VALUE_NEEDED = 'Mobile' THEN 'Yes' ELSE '' END MOBILE
,CASE WHEN F.VALUE_NEEDED = 'Beneficiary' THEN 'Yes' ELSE '' END BENEFICIARY
FROM #FINAL F
GROUP BY F.MEMBER,F.GIVEN_NAMES
,F.SURNAME,VALUE_NEEDED
ORDER BY F.MEMBER
| person id | apple | orange | banana |
|---|---|---|---|
| 1 | yes | ||
| 1 | yes | ||
| 1 | yes |
How do I write the query so it looks more like this?
| person id | apple | orange | banana |
|---|---|---|---|
| 1 | yes | yes | yes |
| 2 | yes | yes | |
| 3 | yes |
You are almost there, just needed to add the max and group by to aggregate it . This used to be a typical interview question back then. Some thing like this if I understood correctly