2012-03-01 1 views
0

内の別のサブクエリは、それがレコードを同じテーブルの日付ごとに以下のような結果を得ることは可能です:Ading既存のサブクエリ

Customer_id, field1, field2, responsecode, created_date 

マイ:

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

表の構造は以下のようになり現在のクエリは次のようなものです:

select created_date, 
    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, 
    count(responsecode) Deals_Redeemed 
    from tblCustomer 
    group by created_date 
    order by created_date 

他のテーブルからのサブクエリである「償還された(Deals redeemed)」である4つのカラム。

Select COUNT(*) from tbl_TransactionDishout where DishoutResponseCode = '0000' 

テーブルの構造は以下の通りです:
テーブル名は "tbl_TransactionDishout" あなたはこのようにしてみてくださいすることができ

[Trnx_ID] [int] IDENTITY(1,1) NOT NULL,  
    [OfferNo] [nvarchar](50) NULL, 
    [MerchantID] [nvarchar](50) NULL,  
    [TerminalID] [nvarchar](50) NULL,  
    [DishoutResponseCode] [nvarchar](50) NULL,  
    [Created] [datetime] NULL  
+0

tblCustomerとtbl_TransactionDishout間の関係は何ですか?それはDishoutResponseCodeのレスポンスコードですか? – arunes

+0

それらの間の関係はありません。結果は日付順に表示されるだけです。 –

答えて

1

です。 (Deals_Redeemed値は、すべての行で同じになり、2つのテーブル間には関係がないと述べた)

select created_date, 
    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, 
    (Select COUNT(*) from tbl_TransactionDishout where DishoutResponseCode = '0000') as Deals_Redeemed 
from tblCustomer 
    group by created_date 
    order by created_date 
0

ごtbl_TransactionDishoutは、次のような別のクエリを行うことができるはずあなたtblCustomerに参加するID列を持っている場合

select created_date, 
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, 
cnt 
from tblCustomer t, 
(select id, count(1) as cnt 
from tbl_transactiondishout 
group by id) tt 
where t.id = tt.id 
group by created_date 
order by created_date 
+0

それは働いていません。 –

+0

を修正しました。もう一度やり直してください:) – jroyce

+0

このIDは何ですか.. ??それらの間に関係はありません.. –

0

たぶん、このような何か:

select 
    created_date, 
    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, 
    (
     Select COUNT(*) 
     from tbl_TransactionDishout 
     where DishoutResponseCode = tblCustomer.responsecode 
    ) as Deals_Redeemed 
from 
    tblCustomer 
group by 
    created_date 
order by 
    created_date