Meine Daten wie folgt aussehen:Erste Durchschnitt aus zwei Bedingungen (Spalten und Zeilen)
PRODUCT DEPT DATE PERCENTAGE
1 A JAN 2
1 B FEB 4
1 A MAR 1
1 B JAN 5
1 A FEB 3
1 B MAR 7
1 A JAN 3
1 B FEB 4
1 A MAR 2
1 B JAN 8
1 A FEB 9
1 B MAR 6
... ... ... ...
Mit Tausenden von verschiedenen Produkten und Dutzende von Abteilungen.
Die Berechnung I durchlaufen haben, ist:
1 - Summe der Prozentsätze wie folgt: nach Produkt, dept und Datum (so Artikel 1/DEPT A/JAN => SUM (Percentage) für jedes Produkt. , DEPT und DATE. 2 - Wenn ich meine Summen habe, erhalten Sie den Durchschnitt der 3 Monate für jedes Produkt und Abteilung (Produkt 1 Abteilung A: JAN/FEB/MAR, und so weiter) 3 - Erhalten Sie den maximalen Durchschnitt (. für jedes Produkt, das dept den höchsten Durchschnitt hat)
habe ich etwas, das funktioniert, aber es ist so lange ich bin sicher, dass ich lernen und etwas besser machen kann:
Select
Verylong_q.TFC,
Round(MAX(verylong_q.average),2) AS HIGHEST_AVERAGE
FROM
(
SELECT
Long_Q.TFC,
Long_Q.DEPT,
Long_Q.Percentage1,
Long_Q.Percentage2,
Long_Q.Percentage3,
((Percentage1 + Percentage2 + Percentage3)/3) AS Average
FROM
(
SELECT
t_Month1.TFC,
t_Month1.DEPT,
t_Month1.Percentage1,
t_Month2.Percentage2,
t_Month3.Percentage3
From
(
Select
pos.TFC,
mv.Dept AS Sector,
sum(pos.percentage) AS Percentage3
FROM
TBO_POS pos,
TBL_MV mv
Where
pos.IV_ID = mv.IV_ID
and Date = […]
and TFC in […]
group by pos.TFC, mv.Dept, pos.Date
order by 1 DESC) t_Month1
LEFT JOIN
(
Select
pos.TFC,
mv.Dept AS Sector,
sum(pos.percentage) AS Percentage2
FROM
TBO_POS pos,
TBL_MV mv
Where
pos.IV_ID = mv.IV_ID
and Date = […]
and TFC in […]
group by pos.TFC, mv.Dept, pos.Date
order by 1 DESC) t_Month2
On t_month1.DEPT = t_month2.DEPT and t_month1.TFC = t_month2.TFC
LEFT JOIN
(
Select
pos.TFC,
mv.Dept AS Sector,
sum(pos.percentage) AS Percentage3
FROM
TBO_POS pos,
TBL_MV mv
Where
pos.IV_ID = mv.IV_ID
and Date = […]
and TFC in […]
group by pos.TFC, mv.Dept, pos.Date
order by 1 DESC) t_Month3
on t_month1.DEPT = t_month3.DEPT and t_month1.TFC = t_month3.TFC
) Long_Q
) VeryLong_Q
Group by verylong_q.TFC
Wie könnte ich das besser machen? Vielen Dank!
Was hat das mit PL/SQL zu tun? Versuchen Sie anzugeben, dass Sie das Oracle DBMS verwenden? –