2012-02-28 58 views
1

我陷入了一個sql問題。我需要像這樣的東西SQL得到值

M S C 

1 sent 12 
1 checked 15 
1 rejected 9 
2 sent 20 
2 checked 18 
3 rejected 10 

其中M分別代表月份,S代表狀態和C代表計數。我想要實現的是組按月上面的例子

通緝:

MSC

1 sent  12 
    checked 15 
    rejected 9 
2 sent  20 
    checked 18 
    rejected 10 

編輯1

眼下SQL語句如下所示:

select month, status, sum(hit) as count 
from myTable 
where dateletter between "date from" and "date to" 
group by month,status order by month, status 

編輯2

我將如何實現這一目標過於

  1 2 

Sent  12 20 

Checked 15 18 

Rejected 9 10 

其中1,2表示從數據庫中的月份值。

+0

歡迎堆棧溢出。幫助我們幫助你:1)你的桌子是什麼樣的? 2)你目前的查詢是什麼樣的? – YXD 2012-02-28 11:16:24

+0

@MrE我已經把sql語句寫成 – sameer 2012-02-28 11:28:40

+0

很酷 - 謝謝。 – YXD 2012-02-28 11:30:07

回答

3
SELECT month AS M, status AS S, SUM(hit) AS C 
FROM myTable 
WHERE dateletter BETWEEN "date from" AND "date to" 
GROUP BY month, status 
ORDER BY month, status 

在查詢中,您無法進一步分組,您將不得不在報告中執行此操作。該查詢是可以的,可以作爲報告的記錄來源。在這樣的報告中,你會爲每個月插入一個組頭。


您可以但是做一個支點查詢

SELECT * 
FROM 
    (SELECT month AS M, status AS S, SUM(hit) AS C 
    FROM myTable 
    WHERE dateletter BETWEEN "date from" AND "date to" 
    GROUP BY month, status 
) 
    PIVOT (
    SUM(C) 
    FOR S IN ('sent', 'checked', 'rejected') 
); 

這將返回列

 
M sent checked rejected 
1 12 15  9 
2 20 18  10 
+0

這個工作在oracle 11g中嗎? – sameer 2012-02-28 11:36:45

+0

是的,但是我沒有測試過我的例子。 – 2012-02-28 11:39:42

+0

我已經嘗試過,但它不工作。它說 ORA-00936:缺少表達 00936. 00000 - 「失蹤表達」 *原因: *操作:在線路 錯誤:6列:22 – sameer 2012-02-28 11:41:52

1

並不很清楚,你從你的榜樣想要的狀態,但它聽起來像是你想要顯示每個月和每個月的詳細信息,但也希望每個月都有一個總計。

可以使用Oracle中的彙總分析功能做到這一點:

create table tst2 
(
m number, 
s varchar2(10), 
hit number 
); 


insert into tst2 values (1,'sent',3); 
insert into tst2 values (1,'checked',2); 
insert into tst2 values (1,'rejected',4); 
insert into tst2 values (2,'sent',6); 
insert into tst2 values (2,'checked',1); 
insert into tst2 values (2,'rejected',3); 
commit; 

select m,s,sum(hit) as cnt, 
case 
    when (grouping(s) = 1 and grouping(m) = 1) then 'All total' 
    when (grouping(s) = 1 and grouping(m) = 0) then 'Month total' 
    else null 
end as "Comment" 
from tst2 
group by rollup(m,s); 

,你會得到


 1 sent    3   
    1 checked    2   
    1 rejected   4   
    1      9 Month total 
    2 sent    6    
    2 checked    1   
    2 rejected   3   
    2     10 Month total 
          19 All total 

在這裏看到的Oracle文檔RE:analytic and reporting functions

+0

我不希望每個月的總數..而是我想只有月份顯示每行一次,如我的問題所示 – sameer 2012-02-29 05:16:54

1

使用BREAK ON語法SQL *相關加:

> BREAK ON M 

>   SELECT 1 m , 'sent' s , 12 c FROM dual 
2 UNION ALL SELECT 1 , 'checked' , 15 FROM dual 
3 UNION ALL SELECT 1 , 'rejected' , 9 FROM dual 
4 UNION ALL SELECT 2 , 'sent'  , 20 FROM dual 
5 UNION ALL SELECT 2 , 'checked' , 18 FROM dual 
6 UNION ALL SELECT 3 , 'rejected' , 10 FROM dual 

     M S     C 
---------- -------- ---------- 
     1 sent    12 
      checked   15 
      rejected   9 
     2 sent    20 
      checked   18 
     3 rejected   10 

所以,用你的例子:

SET LINESIZE 2000 TRIMSPOOL ON TRIM ON 
BREAK ON MONTH 
SPOOL file.txt 

SELECT month, status, SUM(hit) AS count 
    FROM myTable 
WHERE dateletter BETWEEN "date from" AND "date to" 
GROUP BY month, status 
ORDER BY month, status; 

SPOOL OFF 
+0

如果我不想''發送','檢查'硬編碼,但從數據庫中的值怎麼辦? – sameer 2012-02-28 13:42:51

+0

@sameer:已更新。 – Benoit 2012-02-28 13:49:57

+0

如果我想從數據庫中獲取所有值,那該怎麼辦......那是從db開始的月份,從db開始的狀態以及從db開始的alse總和 – sameer 2012-02-29 05:18:36