Hi! Could we please enable some services and cookies to improve your experience and our website?

SQLize | PHPize | SQLtest

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

A A A
Login    Share code      Blog   FAQ
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 AI support!

Copy Clear