2012-03-03 3 views
0

테이블 구조에서 하위 쿼리를 추가
tblCustomer기존 하위 쿼리

Customer_id created field1  field2  cardno 
--------------------------------------------------------  
1014  Test1  Cell Phone 123146  1234567890 
1015  Test2  Email  [email protected] 2345678891 

tbl_TransactionDishout

Trnx_id offerNo TerminalID  Created     cardno 
------------------------------------------------------------------- 
1   1014  170924690436418 2010-05-25 12:51:59.547 1234567890 

이 가능 날짜 현명한 기록 아래와 같은 결과를 얻을 :

   Enrolled Enrolled as Email Enrolled as Text Deals Redeemed 
<First Date> 7   5     2    6 
<Next Date> 9   3     6    14 

나의 현재 쿼리는 다음과 같은 것입니다 :

select created, 
count(field1) Enrolled, 
count(case field1 when 'E-mail' then 1 end) Enrolled_as_Email, 
count(case field1 when 'Cell Phone' then 1 end) Enrolled_as_Cell 
from tblCustomer c 
group by created 
order by created desc 

그러나 그것은 어떻게 Deals_redeemed를 얻기 위해 지금 .. 단지 tblCustomer 테이블에 포함 나에게

을 날짜의 결과를주고 ..? tbl_transaction과 tblCustomer 사이의 관계는 동일한 카드 번호를 가지고 있습니다 ...

답변

1

내 이해 테이블 ​​tbl_TransactionDishout은 제안이며, 그 다음에 레코드가 tblCustomers에 삽입됩니다. 그렇지 않으면 아무것도 변하지 않을 것입니다.

select t.created, 
    count(c.field1) Enrolled, 
    count(case c.field1 when 'E-mail' then 1 end) Enrolled_as_Email, 
    count(case c.field1 when 'Cell Phone' then 1 end) Enrolled_as_Cell, 
    count(case when c.field1 is null then 1 end) [Deals Redeemed] 
from tbl_TransactionDishout t left join tblCustomer c 
    on t.cardno = c.cardno 
group by t.created 
order by t.created desc 

편집 : 그래서 구속 거래 주어진 cardno에 대한 tblCustomers에 존재하지 않는 레코드의 개수가 t.created

+0

열로 변경 c.created 'tblCustomer.created'그것 때문에 선택 목록에 유효하지 않습니다 집계 함수 또는 GROUP BY 절에 포함되어 있지 않습니다. –

+0

예, 미안 해요. 편집 됨. –