Question

I have this query:

select f.DesPrefettura AS desPrefettura, a.flagPrefetturaQuestura AS flag, count(a.idAccordo) AS totaleAccordo
    from ACCORDO a
    inner join PREFETTURA f on f.idProvincia = a.idPrefetturaSottoscrizione
    GROUP BY f.DesPrefettura, a.flagPrefetturaQuestura
    ORDER BY f.DesPrefettura

that give me this result:

desPrefettura   flag    totaleAccordo
AGRIGENTO       0       3
AGRIGENTO       1       86
ALESSANDRIA     0       253
ALESSANDRIA     1       12

instead I would like to have:

desPrefettura       tot01    tot02
AGRIGENTO           3        86
ALESSANDRIA         253      12

How could I edit my query?

Was it helpful?

Solution

This would work in SQL server and I don't think there is anything DBMS specific in there so this should work

select f.DesPrefettura AS desPrefettura, 
count(case when  a.flagPrefetturaQuestura=0 then    a.idAccordo else null end) AS tot01,
count(case when  a.flagPrefetturaQuestura=1 then a.idAccordo else null end) AS tot02
from ACCORDO a
inner join PREFETTURA f on f.idProvincia = a.idPrefetturaSottoscrizione
GROUP BY f.DesPrefettura
ORDER BY f.DesPrefettura
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top