SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
WITH t (hospital, city, region) AS ( SELECT 'ПУДП 3', 'Москва', 'Московский' FROM dual UNION ALL SELECT 'ЦКБ УДП', 'Москва', 'Московский' FROM dual UNION ALL SELECT 'Поликлиника №1', 'Ярославль', 'Ярославский' FROM dual ) SELECT CASE WHEN GROUPING_ID(region,hospital,city) = 3 THEN t.region ELSE t.hospital END hospital, t.city FROM t GROUP BY GROUPING SETS(t.region,(t.region,t.hospital,t.city)) ORDER BY t.region, GROUPING_ID(t.region,t.hospital,t.city) DESC, t.hospital, t.city

Stuck with a problem? Got Error? Ask ChatGPT!

Copy Clear