从同一个表中分组不同的结果

时间:2011-02-16 08:43:06

标签: sql

我想从表中得到一个结果:

Date    Charges

22/04/2010  1764  
22/04/2010  200  
22/04/2010  761  
22/04/2010  3985  
22/04/2010  473  
22/04/2010  677  
22/04/2010  1361  
22/04/2010  6232  
22/04/2010  4095  
23/04/2010  7224  
23/04/2010  1748  
23/04/2010  1355  
23/04/2010  2095  
23/04/2010  2063   
23/04/2010  2331  
23/04/2010  2331  
23/04/2010  4473  
23/04/2010  478  
23/04/2010  1901  
23/04/2010  1250  
23/04/2010  1743  
24/04/2010  1743  
24/04/2010  3923  
24/04/2010  1575   
24/04/2010  1859  
24/04/2010  2431   
24/04/2010  1208  
24/04/2010  158  
24/04/2010  3246  
24/04/2010  2898  
24/04/2010  1517  
24/04/2010  2368  
24/04/2010  961  
24/04/2010  4111  
24/04/2010  3066  
24/04/2010  740  
25/04/2010  2651  
25/04/2010  2693  
25/04/2010  4847  
25/04/2010  312  
25/04/2010  1247  
25/04/2010  5858  
25/04/2010  1040  
25/04/2010  941  
25/04/2010  942  
25/04/2010  1784  
25/04/2010  418  
25/04/2010  2248  
25/04/2010  1834  
25/04/2010  418  
25/04/2010  2263  
26/04/2010  2746  
26/04/2010  942  
26/04/2010  883  
26/04/2010  3339  
26/04/2010  3517  
26/04/2010  761  
26/04/2010  1738  
26/04/2010  1370  
26/04/2010  1501  
26/04/2010  1197  
26/04/2010  2452  
26/04/2010  209  
26/04/2010  1092  
26/04/2010  4316  
26/04/2010  1208  
26/04/2010  1213  
26/04/2010  2179  
26/04/2010  1213  
26/04/2010  1538  
26/04/2010  1939  
26/04/2010  956  
26/04/2010  10715  
26/04/2010  4321  
26/04/2010  956  
26/04/2010  2975  
26/04/2010  798  
26/04/2010  1738 

显示以下字段:

2010年1月4日至2010年4月30日期间的日期,计数> 2500,总计> 2500,总计数和总计

i.e.
22/4/2010, 3, 14312, 9, 19548  
23/4/2010, 2, 11697, 12, 28992  
24/4/2010, 5, 17244, 15, 31804  
25/4/2010, 4, 16049, 15, 29496  
26/4/2010, 7, 31929, 27, 57812  

...
......

非常感谢所有帮助!提前谢谢。

2 个答案:

答案 0 :(得分:2)

基础是使用SUM和CASE,如:

SELECT
    DATEADD(day,DATEDIFF(day,'20010101',DateTimeActivity),'20010101') as Date,
    SUM(CASE WHEN Charges > 2500 THEN 1 ELSE 0 END) as Count2500,
    SUM(CASE WHEN Charges > 2500 THEN Charges END) as Sum2500,
    COUNT(*) as CountTotal,
    SUM(Charges) as SumTotal
FROM
    AccActivity
WHERE
    DateTimeActivity >= '20100401' and
    DateTimeActivity < '20100501'
GROUP BY
    DATEADD(day,DATEDIFF(day,'20010101',DateTimeActivity),'20010101')

根据您的评论更新,以使用真实的表/列名称。我假设你想要包括4月30日发生的交易。

请注意,我使用的是日期文字的安全日期格式(YYYYMMDD) - 根据服务器上的区域设置,大多数其他格式都不明确。

另外,我正在使用DATEADD(day,DATEDIFF(day,'20010101',DateTimeActivity),'20010101')从日期时间中剥离时间组件 - 它看起来有点时髦,但速度合理,并且可以使用相同的模式相对容易地进行其他日期时间转换(例如,您需要在几个月内进行分组,您只需将day个选项更改为month,并将日期全部设置为各自月份的第一个)

答案 1 :(得分:1)

您可以尝试:

SELECT date,
       count(if(charges>2500, 1, NULL)) as countGt2500,
       sum(if(charges>2500, charges, 0)) as totalGt2500,
       count(charges) as countTotal,
       sum(charges) as sumTotal,
  FROM yourTable
 WHERE date >= '2010/04/01'
   AND date <= '2010/04/30'
GROUP BY date;

如果您在字段日期保存完整日期时间,则必须从日期时间中提取日期部分,为此,您可以通过以下方式使用DATE功能:

SELECT DATE(date) as day,
       count(if(charges>2500, 1, NULL)) as countGt2500,
       sum(if(charges>2500, charges, 0)) as totalGt2500,
       count(charges) as countTotal,
       sum(charges) as sumTotal,
  FROM yourTable
 WHERE date >= '2010/04/01'
   AND date <= '2010/04/30'
GROUP BY day;