2017-02-02 4 views
1

다음 조건에 대해 데이터베이스를 쿼리해야한다는 요구 사항이 있습니다.n 번을 사용하여 데이터베이스에서 데이터 가져 오기

데이터베이스 [리포트 당겨] 우리는 내가 지난 30 일 기록을 가지고 있지만 정확히 기록이없는 한 모든 고객을 찾을 필요가있는 2 열
에게 CustomerID를 ReportDt

말을 할 수있다 (오늘 - 30) 다른 조건 중.

Select Condition on [Report Pull] PR 
and cast(PR.ReportDt as Date) = cast(getdate()-30 as date) 
and not exists (
       select PR2.* 
       from [Report Pull] PR2 
       where 1=1 
       and cast(PR2.ReportDt as Date) > cast(getdate()-30 as date) 
       and PR2.CustomerId = PR.CustomerId 
) 

지금 내가 매 30 일이되도록

cast(getdate()-30 as date) is mod30 = 0 
AND at the same time 
cast(PR2.ReportDt as Date) > cast(getdate()-30 as date) 

Next 
cast(getdate()-60 as date) is mod30 = 0 
AND at the same time 
cast(PR2.ReportDt as Date) > cast(getdate()-60 as date) 
that is no report pulled in last 60 days 

등을 고객을 끌어합니다. 이는 db가 여러 개의 보고서 끌어 오기 레코드를 가질 수 있기 때문입니다. 조금 혼란 스럽지만 제발 도와주세요. :)

SQL에서는 변수를 선언 할 수 없습니다. DB는 Salesforce Marketing입니다. CLoud 일명 ExactTarget입니다.

답변

0

당신이하려는 일에 100 % 명확하지는 않지만, 자체 조인이없는 한 번에 테이블을 통과 할 수 있다고 생각합니다. 나는 자기 조인이 나쁘다고 말하는 것이 아닙니다, 당신을 생각해보십시오.

select 
    CustomerID, 
    max (case when cast(ReportDt as Date)=cast(getdate()-30 as date) then 1 else 0 end) EQ30, 
    max (case when cast(ReportDt as Date)>cast(getdate()-30 as date) then 1 else 0 end) GT30, 
    max (case when cast(ReportDt as Date)=cast(getdate()-60 as date) then 1 else 0 end) EQ60, 
    max (case when cast(ReportDt as Date)>cast(getdate()-60 as date) then 1 else 0 end) GT60 
from 
    [Report Pull] 
group by 
    CustomerID 

그것은 말한다 데이터 집합을 초래할한다 :

Customer ID  30 Days ago? Last 30 Days 60 Days ago? Last 60 days 
-----------  ------------ ------------ ------------ ------------ 

경우 사실 1 = 0 = 거짓

이 고려하십시오. 여기에서 다른 시간대로 이것을 확장하고 고객별로 어떤 범주에 있는지 평가하는 것이 매우 쉽습니다.

다시 말하지만 최종 결과물에는 무엇을 원하는지 잘 모르겠습니다. 이것은 그것에 빌려줄 개념입니다.

관련 문제