包括日期边界以分割选择结果

时间:2014-03-24 11:58:54

标签: sql date case between

我想在同一行中返回特定度量在不同时间范围内的结果。 例如"过去30天","过去7天","过去3天"。 在这样做时,我最初使用了UNION函数,并为每个时间范围创建了多个子查询。这样做的缺点是我收集了相同数字三次,结果导致运行时间增加。

一位同事建议我使用CASE功能来细分时间范围。我最初的想法是按如下方式实施:

select tp.Name, 
    case when pub.Date between DATEADD(day, -31, getdate()) and DATEADD(day, -1, getdate()) 
        then SUM(Impressions) else 0 end 'Last 30 days Impressions',
    case when pub.Date between DATEADD(day, -31, getdate()) and DATEADD(day, -1, getdate()) 
        then SUM(Revenue * rler.Rate) else 0 end 'Last 30 days Revenues',
    case when pub.Date between DATEADD(day, -8, getdate()) and DATEADD(day, -1, getdate()) 
        then SUM(Impressions) else 0 end 'Last 7 days Impressions',
    case when pub.Date between DATEADD(day, -8, getdate()) and DATEADD(day, -1, getdate()) 
        then SUM(Revenue * rler.Rate) else 0 end 'Last 7 days Revenues',
    case when pub.Date between DATEADD(day, -4, getdate()) and DATEADD(day, -1, getdate()) 
        then SUM(Impressions) else 0 end 'Last 3 days Impressions',
    case when pub.Date between DATEADD(day, -4, getdate()) and DATEADD(day, -1, getdate()) 
        then SUM(Revenue * rler.Rate) else 0 end 'Last 3 days Revenues'

from ...

where ...

group by tp.Name, tp.Kind, pub.Date

order by 'Last 30 days Impressions'

不幸的是,这将为每个姓名,种类和日期返回一行,这不是我想要的。我认为这个问题依赖于GROUP BY调用中的pub.Date。我该怎么做才能克服这个问题?

2 个答案:

答案 0 :(得分:0)

由于时间重叠,您无法完全实施同事的建议。您可以执行非重叠范围,如下所示:

select tp.Name, 
        (case when pub.Date between DATEADD(day, -3, getdate()) and DATEADD(day, -1, getdate()) 
              then 'Last 3 days Impressions'
              when pub.Date between DATEADD(day, -7, getdate()) and DATEADD(day, -1, getdate()) 
              then '4-7 days Impressions'
              when pub.Date between DATEADD(day, -31, getdate()) and DATEADD(day, -1, getdate()) 
              then '8-31 days Impressions'
              else 'Older'
         end) as TimeRange,
       SUM(Impressions) as NumImpressions,
       . . . 
from . . .
where . . .
group by tp.Name, 
         (case when pub.Date between DATEADD(day, -3, getdate()) and DATEADD(day, -1, getdate()) 
               then 'Last 3 days Impressions'
               when pub.Date between DATEADD(day, -7, getdate()) and DATEADD(day, -1, getdate()) 
               then '4-7 days Impressions'
               when pub.Date between DATEADD(day, -31, getdate()) and DATEADD(day, -1, getdate()) 
               then '8-31 days Impressions'
               else 'Older'
          end)

答案 1 :(得分:0)

我回答了自己的问题。可以使用以下代码实现结果:

select tp.Name, 
    sum(case when pub.Date between DATEADD(day, -31, getdate()) and DATEADD(day, -1, getdate()) 
        then Impressions else 0 end) 'Last 30 days Impressions',
    sum(case when pub.Date between DATEADD(day, -31, getdate()) and DATEADD(day, -1, getdate()) 
        then (Revenue * rler.Rate) else 0 end) 'Last 30 days Revenues',
    sum(case when pub.Date between DATEADD(day, -8, getdate()) and DATEADD(day, -1, getdate()) 
        then Impressions else 0 end) 'Last 7 days Impressions',
    sum(case when pub.Date between DATEADD(day, -8, getdate()) and DATEADD(day, -1, getdate()) 
        then (Revenue * rler.Rate) else 0 end) 'Last 7 days Revenues',
    sum(case when pub.Date between DATEADD(day, -4, getdate()) and DATEADD(day, -1, getdate()) 
        then Impressions else 0 end) 'Last 3 days Impressions',
    sum(case when pub.Date between DATEADD(day, -4, getdate()) and DATEADD(day, -1, getdate()) 
        then (Revenue * rler.Rate) else 0 end) 'Last 3 days Revenues'

from ...

where ...

group by tp.Name, tp.Kind

order by 'Last 30 days Impressions' desc