Ho una query SQL Server che utilizza la clausola ROLLUP
durante il raggruppamento. Voglio una query equivalente in Postgres. Query in SQL Server è:PostgreSQL equivalente per SQL Server GROUP BY WITH ROLLUP
SELECT (CASE WHEN acnt_dba_name Is Null THEN 'Total' ELSE acnt_dba_name END) as account,
(CASE WHEN evt_name Is Null THEN '' ELSE evt_name END) as event,
COUNT(CASE reg_is_complete WHEN true THEN 1 ELSE Null END) as regsComplete,
COUNT(CASE WHEN reg_frn_pro_id > 0 AND reg_is_complete = false THEN 1 ELSE Null END) as regsInComplete,
COUNT(CASE WHEN reg_frn_pro_id > 0 THEN Null ELSE 1 END) as regsClicks
FROM registrations_view
LEFT JOIN events ON (evt_id = reg_frn_evt_id)
LEFT JOIN accounts ON (acnt_id = evt_frn_acnt_id)
WHERE reg_date_created < #CreateODBCDate(url.endDate)#
AND reg_date_created > #CreateODBCDate(url.startDate)#
AND reg_is_active = true -- only active regs
AND reg_is_test = false -- only live registrations
-- AND reg_is_denied = false -- exclude denied reg statuses (include these for now RWB 8/7/2)
GROUP BY rollup(acnt_dba_name, evt_name)
-- Sort with Nulls at the bottom
ORDER BY acnt_dba_name, evt_name
grazie e grande lavoro da voi –
Si dovrebbe usare 'UNION ALL' invece di 'UNION', quindi il DB non dovrà cercare di rimuovere i duplicati. –