2011-12-22 109 views
0

我遇到了一些麻煩,下面是代碼,我嘗試從兩個不同的表中獲取計數,然後按日期對它們進行分組。SQL Group by Totals issue

declare @fromdate as date 
declare @todate as date 
set @fromdate = CONVERT(date,GETDATE()-3,101) 
set @todate = CONVERT(date,GETDATE(),101) 

USE database 
SELECT 
    CONVERT(date,created_date,101) as OrdDate, 
    col1= (SELECT COUNT(distinct cust_id) 
     FROM table1 
     WHERE created_date BETWEEN @fromdate and @todate), 
    col2= (SELECT COUNT(distinct cust_id) 
     FROM table1 
     WHERE [status] LIKE 'P%' and created_date BETWEEN @fromdate and @todate), 
    col3= (SELECT COUNT(distinct cust_id) 
     FROM table2 
     WHERE created_date BETWEEN @fromdate and @todate), 
    col4= (SELECT COUNT(distinct cust_id) 
     FROM table2 
     WHERE created_date between @fromdate and @todate and result_error like 'FAIL%') 

FROM table1 
WHERE created_date BETWEEN @fromdate and @todate 

GROUP BY CONVERT(date,created_date,101) 
ORDER BY CONVERT(date,created_date,101) 

它在一起計算所有的日子,我試着給每個表達式添加Group,但是沒有去。有什麼建議麼?

OrdDate  col1 col2 col3 col4 
2011-12-19 5000 4000 3000 2000 
2011-12-20 5000 4000 3000 2000 
2011-12-21 5000 4000 3000 2000 

我要的是這樣的事情

OrdDate  col1 col2 col3 col4 
2011-12-19 1500 1500 500  750 
2011-12-20 2500 750  1000 1000 
2011-12-21 1000 1750 1500 250 
+0

如果您通過刪除結果是什麼組? –

+0

如果你在一張桌子上沒有2011-12-20怎麼辦?一般來說,除非你想要一行,否則不要做這種不相交的聚集。 – gbn

+0

這個查詢中實際上有三個表 - table1,customer_order和table2。 –

回答

1

你必須先生成一個日期列表,然後再加入每個計數。

這將消除與不同的行計數問題對於每個聚集

SELECT 
    base.TheDate AS OrdDare, 
    ISNULL(T1.C1, 0) AS col1, 
    ISNULL(T2.C2, 0) AS col2, 
    ISNULL(T3.C3, 0) AS col3, 
    ISNULL(T4.C4, 0) AS col4 
FROM 
    (-- UNION will also DISTINCT 
    SELECT CONVERT(date,created_date,101) AS TheDate FROM table1 
    WHERE created_date BETWEEN @fromdate and @todate 
    UNION 
    SELECT CONVERT(date,created_date,101) FROM table2 
    WHERE created_date BETWEEN @fromdate and @todate 
    ) base 
    LEFT JOIN 
    (
    SELECT CONVERT(date,created_date,101) AS TheDate, COUNT(distinct cust_id) AS C1 
     FROM table1 
     WHERE created_date BETWEEN @fromdate and @todate 
     GROUP BY CONVERT(date,created_date,101) 
    ) T1 ON base.TheDate = T1.TheDate 
    LEFT JOIN 
    (
     SELECT CONVERT(date,created_date,101) AS TheDate, COUNT(distinct cust_id) AS C2 
     FROM table1 
     WHERE WHERE [status] LIKE 'P%' and created_date BETWEEN @fromdate and @todate 
     GROUP BY CONVERT(date,created_date,101) 
    ) T2 ON base.TheDate = T2.TheDate 
    LEFT JOIN 
    (
    SELECT CONVERT(date,created_date,101) AS TheDate, COUNT(distinct cust_id) AS C3 
     FROM table2 
     WHERE created_date BETWEEN @fromdate and @todate 
     GROUP BY CONVERT(date,created_date,101) 
    ) T3 ON base.TheDate = T3.TheDate 
    LEFT JOIN 
    (
    SELECT CONVERT(date,created_date,101) AS TheDate, COUNT(distinct cust_id) AS C4 
     FROM table2 
     WHERE created_date BETWEEN @fromdate and @todate and result_error like 'FAIL%' 
     GROUP BY CONVERT(date,created_date,101) 
    ) T4 ON base.TheDate = T4.TheDate 
ORDER BY 
    base.TheDate; 
+0

gbn - 列沒有標題和所有日期正在呈現,關於如何將結果限制在已定義範圍的任何想法? – Rolymo

+1

將WHERE添加到基準派生表。添加列別名。完成 – gbn

0

你需要在你需要每個聚集查詢做該組。

當前,您只是按table1分組,並且您希望在每個子查詢中按日期分組。你可以在主要查詢中刪除該組,我相信...

0

嘗試:

select CONVERT(date,created_date,101) OrdDate, 
     count(distinct table1_cust) col1, 
     count(distinct table1_cust_p) col2, 
     count(distinct table2_cust) col3, 
     count(distinct table2_fail) col4 
from 
(select distinct 
     created_date, 
     cust_id  table1_cust, 
     case when [status] LIKE 'P%' then cust_id end 
        table1_cust_p, 
     NULL   table2_cust, 
     NULL   table2_fail 
FROM table1 
WHERE created_date BETWEEN @fromdate and @todate 
UNION ALL 
select distinct 
     created_date, 
     NULL   table1_cust, 
     NULL   cust_order, 
     cust_id  table2_cust, 
     case when result_error like 'FAIL%' then cust_id end 
        table2_fail 
FROM table2 
WHERE created_date BETWEEN @fromdate and @todate) v 
group by created_date 

編輯:更新澄清問題後。