I’ve the below table
ID TYPE
--- ----
1 P
1 W
2 P
3 W
4 W
4 X
5 P
6 null
I need a new table to be created like the one below
ID Count of Type Code
-- -------------- -------
1 2 null
2 1 P
3 1 W
4 2 null
5 1 P
6 0 null
1st col ---> ID
2nd col ---> count of "type" for an ID
3rd col ---> if count(type) = 1 then TYPE
else null
Kindly help me in writing an ORACLE SQL query
You could use a subquery with the max function to get a value for the code and then use that in a case statement to get the value in your final query only when the count=1.