다른 시간 범위의 특정 측정 값의 결과를 같은 행에 반환하고 싶습니다. 예 : "지난 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
전화. 문제를 극복하기 위해 무엇을해야합니까?