2010-06-16 5 views
1

SQL-SERVER 2005를 사용 중입니다.간단한 쿼리가있는 TSQL 도움말

다이어그램, CellularUsers 및 CellularPayments에서 볼 수있는 것처럼 두 개의 테이블이 있습니다. 사용자 ID의 마지막 CellularPayments.paymentSum 8은 모든 사용자 ID 및 userCellularNumbers CellularPayments.date> GETDATE()

  • 을 선택했다

    1. 경우 : 내가 필요 사용자가 몇 가지 규칙이있을 때 부과하는 데 필요한 아이디의 마지막 CellularPayments.paymentSum 18 인 경우 모든 ID와 userCellularNumbers을 선택 여기서 DATEADD (DD, 7 CellularPayments.date)> GETDATE()

    alt text http://img256.imageshack.us/img256/1946/63468037.png 0,123,516

    내가 제대로 논리를 다음과 같은거야 경우 미리 다이어그램에

    click here

    내가 지금 그것을 할 방법하지만 바로 내 사전

  • 답변

    1

    에서

    select cu.userID,cu.userCellularNumber from CellularUsers cu 
    left join CellularPayments cp 
    on cu.userID=cp.userID 
    where cu.isPaymentRecurring=1 
    and isActivated=1 
    and cp.paymentID in (select max(paymentID) from CellularPayments group by userID) 
    

    감사를 보이는하지 않습니다 where 절에 다음 AND 문을 추가해야합니다.

    and dateadd(dd 
          ,case cp.PaymentSum 
           when 8 then 0 
           when 18 then 7 
           else [AppropriateDefaultValue] 
          end 
          ,CellularPayments.date) > getdate() 
    

    (내부 결합도 만들 수 있습니다.)

    +0

    좋습니다! 이러한 경우에 케이스를 사용하기 시작해야합니다. – eugeneK

    +0

    놀라운 유연성을 허용 할 수 있습니다. (그리고 upvotes 감사, 그들은 사용자 페이지 # 42 [2010 년 6 월 17 일]에 나를 착륙했습니다. 그것은 승리 조건이되어야합니다!) –