2014-03-24 2 views
0

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

답변

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 
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)