Sql 包括日期边界以分段选择结果
我想在同一行中返回不同时间范围内特定度量的结果。 例如“最后30天”、“最后7天”、“最后3天”。 为此,我最初使用了Sql 包括日期边界以分段选择结果,sql,date,case,between,Sql,Date,Case,Between,我想在同一行中返回不同时间范围内特定度量的结果。 例如“最后30天”、“最后7天”、“最后3天”。 为此,我最初使用了UNION函数,并为每个时间范围创建了多个子查询。这样做的缺点是,我收集了三次相同的数据,从而增加了运行时间 一位同事建议我使用CASE函数对时间范围进行分段。我最初的想法是按如下方式实施: select tp.Name, case when pub.Date between DATEADD(day, -31, getdate()) and DATEADD(day, -
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'
不幸的是,这将为每个名称、种类和日期返回一行,这不是我想要的。我认为这个问题取决于
通话组中的发布日期。我该怎么做才能克服这个问题?你不能完全执行同事的建议,因为时间重叠。您可以执行非重叠范围,如下所示:
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)
我在回答我自己的问题。可使用以下代码实现此结果:
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