I need to show how many different values every 'id' has.
It should look like this:
id | component_a | component_b | component_c
--------------------------------------------------
KLS11 | none | one | none
KLS12 | one | one | none
KLS13 | several | one | none
KLS14 | one | one | one
KLS15 | one | several | several
I have the following table (table_a):
id | component_a | component_b | component_c
--------------------------------------------------
KLS11 | | a |
KLS12 | a | a |
KLS13 | a | a |
KLS13 | b | a |
KLS14 | a | a | a
KLS15 | a | a | a
KLS15 | a | b | b
Here an example/explanation:
- KLS13 has different values in component_a ( a,b ) - so it should display 'several'
- KLS13 has the same values in component_b ( a,a ) - so it should display 'one'
- KLS13 has no value in component_c - so it should display 'none'
Here's my SQL-code:
I already did it for component_a but it doesnt work. What am i doing wrong?
SELECT
CASE WHEN component_a is NULL THEN 'none'
WHEN (SELECT count(DISTINCT component_a)
FROM table_a
WHERE id=(SELECT id
FROM table_a GROUP BY id HAVING count(*)>1)>1 THEN 'several'
WHEN (SELECT count(DISTINCT component_a)
FROM table_a
WHERE id=(SELECT id
FROM table_a GROUP BY id HAVING count(*)>1)=1 THEN 'one'
END as componentA
FROM table_a
i am a beginner at SQL so i would appreciate any help.
Have a nice day