2016-05-10 6 views
0

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!

+0

Was hat das mit PL/SQL zu tun? Versuchen Sie anzugeben, dass Sie das Oracle DBMS verwenden? –

Antwort

3

Ist das nicht einfach:

  1. Summe der Prozentsätze nach Produkt, Abteilung und das Datum in der innersten Unterabfrage
  2. den Durchschnitt der Monate für jedes Produkt und dept in der nächsten Unterabfrage Get
  3. Ermitteln Sie den maximalen Durchschnittswert für jedes Produkt in der Hauptabfrage.

Abfrage:

select product, max(avg_sum_percentage) 
from 
(
    select product, dept, avg(sum_percentage) as avg_sum_percentage 
    from 
    (
    select product, dept, date, sum(percentage) as sum_percentage 
    from mytable 
    group by product, dept, date 
) per_product_dept_date 
    group by product, dept 
) per_product_dept 
group by product; 
0

Von dem, was Sie Beschreiber lag() scheint, wie die geeignete Methode, zusammen mit Aggregation und Auswahl der besten:

select * 
from (select product, dept, (sump_1 + sump_2 + sump_3) /3 as avg_max, 
      row_number() over (partition by product order by (sump_1 + sump_2 + sump_3) /3 desc) as seqnum 
     from (select product, dept, date, sum(percentage) as sump, 
        lag(sum(percentage)) over (partition by product, dept order by date) as sump_1, 
        lag(sum(percentage, 2)) over (partition by product, dept order by date) as sump_2 
      from TBO_POS pos join 
       TBL_MV mv 
       on pos.IV_ID = mv.IV_ID 
      where Date = […] and TFC in […] 
      group by product, dept, date 
      ) t 
    ) t 
where seqnum = 1; 

Diese Lösung folgt die Beschreibung des Problems . Es produziert eine Zeile für jeden Monat und jedes Produkt. Diese Version berücksichtigt fehlende Werte und andere Probleme nicht. I denke, Dies ist die Logik, die Sie wollen, aber ohne die erwarteten Ergebnisse könnte die Frage mehrdeutig sein.

Verwandte Themen