SQL Group by Totals问题

时间:2011-12-22 10:56:22

标签: sql date grouping

我遇到了一些真正的问题,下面是代码,我尝试从两个不同的表中获取计数,然后按日期对它们进行分组。

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 by添加到每个表达式,没有去。有什么建议吗?

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

3 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

您需要在所需的每个聚合查询中执行该组。

目前,您只是table1的组,并且您希望在每个子查询中按日期分组。你可以在我认为的主查询中删除该组...

答案 2 :(得分: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

编辑:在澄清问题后更新。

相关问题