【问题标题】:Include date boundaries to segment select results包括日期边界以细分选择结果
【发布时间】:2014-03-24 11:58:53
【问题描述】:

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

一位同事建议我使用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。我应该怎么做才能克服这个问题?

【问题讨论】:

    标签: sql date case between


    【解决方案1】:

    由于时间重叠,您无法完全执行同事的建议。你可以做不重叠的范围,像这样:

    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)
    

    【讨论】:

      【解决方案2】:

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

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-31
        • 2017-06-28
        相关资源
        最近更新 更多