2009-04-22 60 views
4

比方說,我有一個表,ClientTrade,像這樣的:PLSQL:獲取一週全額的每一天星期和在單個查詢

ClientName , TradeDate , Quantity 

而且我想在Oracle中創建一個查詢PLSQL應該返回這樣的結果: (天數從爲週一,週二= SUM(數量)爲週二的TradeDate列,週一= SUM(數量)派生...等)

ClientName Mon Tue Wed Thu Fri Sat Sun TotalForWeek 
ABC   10 15 5 2 4 0 0 34 
XYZ   1 1 2 1 2 0 0 7 

假設這份報告總是有條件使它運行一週,這是否正確是否可以在單個查詢中創建這個?

+0

這是有趣的 - 幾乎每一次,人們都忘記給表格命名。在SQL問題中95%一致。 – 2009-04-22 05:31:33

+1

第一個表的名稱總是「a」。 ;) – tpdi 2009-04-22 05:35:40

回答

3

子查詢。

select ClientName, 
(select sum(b.quantity) 
from table b where b.clientName = a.clientname 
and b.tradedate = [some constant or calculation that identifies monday]) 
as Mon, 
(select sum(b.quantity) 
from table b where b.clientName = a.clientname 
and b.tradedate = [some constant or calculation that identifies tuesday]) 
as Tue, 

..etc.. 
from table a 

一個更清潔,但可能不太有效的方式涉及視圖的一組是:EW

create view quantityperday as 
select clientname, 
tradedate, 
dayofweek(tradedate) as dow, 
weekofyear(tradedate) as woy, 
year(tradedate) as y, 
sum(quantity) as quantity 
from table 
group by clientname, tradedate; 

然後:

select clientname, b.quantity as Mon, c.quantity as Tue .... 
from table a join quantityperday b 
on (a.clientname = b.clientname and b.y = '2008' 
and b.doy = 2 and b.dow = 'Monday') 
quantityperday c 
on (a.clientname = c.clientname and c.y = '2008' 
and c.doy = 2 and c.dow = 'Tuesday') 
join .... 

這之所以變得醜陋的是,我們將行旋轉到列中。

2

讓我們來看看:

SELECT Client, MonSum, TueSum, WedSum, ThuSum, FriSum, SatSum, SunSum, TotSum 
    FROM (SELECT ClientName AS Client, SUM(Quantity) AS MonSum 
      FROM Trades 
      WHERE DayOfWeek(TradeDate) = 'Monday' 
       AND TradeDate BETWEEN DATE '..Monday..' AND DATE '..Sunday..' 
      GROUP BY ClientName 
     ) AS MonData 
     JOIN 
     (SELECT ClientName AS Client, SUM(Quantity) AS TueSum ... 
     ) AS TueData ON Mondata.Client = TueData.Client 
     JOIN 
     ... 
     (SELECT ClientName AS Client, SUM(Quantity) AS TotSum 
      FROM Trades 
      WHERE TradeDate BETWEEN DATE '..Monday..' AND DATE '..Sunday..' 
      GROUP BY ClientName 
     ) AS TotData ON MonData.Client = TotData.Client 
    ORDER BY Client; 

不整潔,但@tpdi在他的答覆中提到,這是因爲我們樞行成列。我已使用一致的TradeDate BETWEEN ...條款來涵蓋相關的一週。

0

感謝您的回答。其實,我發現在PLSQL一些東西,工作地非常好:

select clientname, 
max(decode(trim(dow),'MONDAY',totalquantity,0)) Mon, 
max(decode(trim(dow),'TUESDAY',totalquantity,0)) Tue, 
max(decode(trim(dow),'WEDNESDAY',totalquantity,0)) Wed, 
max(decode(trim(dow),'THURSDAY',totalquantity,0)) Thu, 
max(decode(trim(dow),'FRIDAY',totalquantity,0)) Fri, 
(
max(decode(trim(dow),'MONDAY',totalquantity,0)) + 
max(decode(trim(dow),'TUESDAY',totalquantity,0)) + 
max(decode(trim(dow),'WEDNESDAY',totalquantity,0)) + 
max(decode(trim(dow),'THURSDAY',totalquantity,0)) + 
max(decode(trim(dow),'FRIDAY',totalquantity,0)) 
) TOTAL 
from 
(
    select clientname, 
    to_char(tradedate, 'DAY') as dow, 
    sum(quantity) as totalquantity 
    from ClientTrade a 
    where a.tradedate >= trunc(sysdate-7,'D') 
    and a.tradedate <= trunc(sysdate-7,'D') + 4 
    group by c.clientshortname, tradedate 
) 
group by clientname 
6

只是簡化有點...

SELECT ClientName, 
     SUM(CASE WHEN to_char(TradeDate,'DY')='MON' THEN Quantity ELSE NULL END) AS Mon, 
     SUM(CASE WHEN to_char(TradeDate,'DY')='TUE' THEN Quantity ELSE NULL END) AS Tue, 
     SUM(CASE WHEN to_char(TradeDate,'DY')='WED' THEN Quantity ELSE NULL END) AS Wed, 
     SUM(CASE WHEN to_char(TradeDate,'DY')='THU' THEN Quantity ELSE NULL END) AS Thu, 
     SUM(CASE WHEN to_char(TradeDate,'DY')='FRI' THEN Quantity ELSE NULL END) AS Fri, 
     SUM(CASE WHEN to_char(TradeDate,'DY')='SAT' THEN Quantity ELSE NULL END) AS Sat, 
     SUM(CASE WHEN to_char(TradeDate,'DY')='SUN' THEN Quantity ELSE NULL END) AS Sun, 
     SUM(Quantity) AS TotalForWeek 
FROM ClientTrade 
GROUP BY ClientName 
0

我嘗試在Oracle 11G:

select clientname, nvl(MON,0) MON, nvl(TUE,0) TUE, nvl(WED,0) WED, nvl(THU,0) THU, nvl(FRI,0) FRI, nvl(SAT,0) SAT, nvl(SUN,0) SUN, 
nvl(MON,0) + nvl(TUE,0) + nvl(WED,0) + nvl(THU,0) + nvl(FRI,0) +nvl(SAT,0) + nvl(SUN,0) TotalForWeek 
from 
(
select clientname,to_char(tradedate,'Dy') dw, sum(quantity) quantity from ClientTrade 
group by clientname, to_char(tradedate,'Dy') 
) 
pivot (sum(quantity) FOR dw in ('Mon' as MON,'Tue' as TUE,'Wed' AS WED,'Thu' AS THU,'Fri' AS FRI,'Sat' AS SAT,'Sun' AS SUN)) 
相關問題