I want to group the below table by country and if the country is blank then I want to group by state. Is there a way to do in plain SQL? I am able to group if there is no condition on state!!
+--------+-------+---------+
| EMP ID | STATE | COUNTRY |
+--------+-------+---------+
| 1 | AP | IN |
| 2 | UP | IN |
| 3 | MP | IN |
| 4 | NJ | US |
| 5 | NY | US |
| 6 | CA | US |
| 7 | PA | |
| 8 | PA | |
| 9 | CR | |
+--------+-------+---------+
Expected Output:
+--------+-----------+---------+
| EMP ID | STATE | COUNTRY |
+--------+-----------+---------+
| 1,2,3 | AP,UP, MP | IN |
| 4,5,6 | NJ,NY,CA | US |
| 7,8 | PA,PA | |
| 9 | CR | |
+--------+-----------+---------+
Do this:
WITH d AS (
SELECT 1 id, 'AP' state, 'IN' country FROM DUAL UNION ALL
SELECT 2 id, 'UP' state, 'IN' country FROM DUAL UNION ALL
SELECT 3 id, 'MP' state, 'IN' country FROM DUAL UNION ALL
SELECT 4 id, 'NJ' state, 'US' country FROM DUAL UNION ALL
SELECT 5 id, 'NY' state, 'US' country FROM DUAL UNION ALL
SELECT 6 id, 'CA' state, 'US' country FROM DUAL UNION ALL
SELECT 7 id, 'PA' state, null country FROM DUAL UNION ALL
SELECT 8 id, 'PA' state, null country FROM DUAL UNION ALL
SELECT 9 id, 'CR' state, null country FROM DUAL)
SELECT listagg(id,',') within group ( order by id),
listagg(state,',') within group (order by state),
country
FROM d
group by nvl(country, state), country
order by 1
Use a case statement in your SELECT:
SELECT EMP_ID, STATE, CASE WHEN COUNTRY IS NULL THEN STATE ELSE COUNTRY END AS COUNTRY_STATE
FROM [TABLE]
Then you can use LISTAGG() to rollup the values, depending on your version of Oracle
SELECT LISTAGG(EMP_ID, ',') WITHIN GROUP,
LISTAGG(STATE, ',') WITHIN GROUP,
COUNTRY_STATE
FROM (
SELECT EMP_ID, STATE, CASE WHEN COUNTRY IS NULL THEN STATE ELSE COUNTRY END AS COUNTRY_STATE
FROM [TABLE] )
GROUP BY COUNTRY_STATE
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.