2012-03-03 9 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に挿入されます。もしそうでなければ、何も変わらないでしょう。だから、償還取引は、与えられたCARDNOため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 

EDIT:t.created

+0

列「tblCustomer.created」に変更c.createdので、選択リストに無効です集計関数またはGROUP BY句には含まれていません。 –

+0

はい、すみません。編集されました。 –

関連する問題