I have problem with creating pivot, but firstly here are all my data structure with sets: https://pastebin.com/LiLHAMqs
And I have to create sql to get data in this format .
I have to use pivot.
I have problem with counting how many cats of one sex is in every group.
Here is my SQL query
SELECT "Banda","Plec", NVL(SZEFUNIO,0) as SZEFUNIO, NVL(BANDZIOR,0) AS BANDZIOR, NVL(LOWCZY,0) AS LOWCZY, NVL(LAPACZ,0) AS LAPACZ, NVL(KOT,0) AS KOT, NVL(MILUSIA,0) AS MILUSIA, NVL(DZIELCZY,0) AS DZIELCZY,
(NVL(SZEFUNIO,0) + NVL(BANDZIOR,0) + NVL(LOWCZY,0) + NVL(LAPACZ,0) + NVL(KOT,0) + NVL(MILUSIA,0) + NVL(DZIELCZY,0)) as Suma
FROM (SELECT b.nazwa "Banda", funkcja "Funkcja", plec "Plec", przydzial_myszy "przydzial", NVL(myszy_extra,0) "myszy_extra" FROM Kocury k
join Bandy b on b.numer_bandy = k.nr_bandy
)
PIVOT
( sum("przydzial" + NVL("myszy_extra",0))
for "Funkcja"
in ('SZEFUNIO' as SZEFUNIO, 'BANDZIOR' AS BANDZIOR, 'LOWCZY' AS LOWCZY, 'LAPACZ' AS LAPACZ, 'KOT' AS KOT, 'MILUSIA' AS MILUSIA, 'DZIELCZY' AS DZIELCZY ))
order by "Banda";
You could use count over partition
and indicate the name ( nazwa
) and the genre ( plec
), like this:
SELECT "Banda",
"Plec",
"Ile",
NVL(SZEFUNIO, 0) as SZEFUNIO,
NVL(BANDZIOR, 0) AS BANDZIOR,
NVL(LOWCZY, 0) AS LOWCZY,
NVL(LAPACZ, 0) AS LAPACZ,
NVL(KOT, 0) AS KOT,
NVL(MILUSIA, 0) AS MILUSIA,
NVL(DZIELCZY, 0) AS DZIELCZY,
(NVL(SZEFUNIO, 0) + NVL(BANDZIOR, 0) + NVL(LOWCZY, 0) +
NVL(LAPACZ, 0) + NVL(KOT, 0) + NVL(MILUSIA, 0) + NVL(DZIELCZY, 0)) as Suma
FROM (SELECT b.nazwa "Banda",
count(*) over (partition by b.nazwa, k.plec) as "Ile",
funkcja "Funkcja",
plec "Plec",
przydzial_myszy "przydzial",
NVL(myszy_extra, 0) "myszy_extra"
FROM Kocury k
join Bandy b
on b.numer_bandy = k.nr_bandy)
PIVOT(sum("przydzial" + NVL("myszy_extra", 0))
for "Funkcja" in('SZEFUNIO' as SZEFUNIO,
'BANDZIOR' AS BANDZIOR,
'LOWCZY' AS LOWCZY,
'LAPACZ' AS LAPACZ,
'KOT' AS KOT,
'MILUSIA' AS MILUSIA,
'DZIELCZY' AS DZIELCZY))
order by "Banda", "Ile";
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.