2012-03-01 63 views
0

另一個子查詢是否有可能從同一個表日期明智的記錄得到如下結果:Ading在現有子查詢

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

表結構是這個樣子:

Customer_id, field1, field2, responsecode, created_date 

我當前的查詢是這樣的:

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 

這對於前三列,但對日工作正常e四列是「Deals redeemed」,這是來自另一個表的子查詢。

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 2012-03-01 07:11:08

+0

它們之間沒有任何關係..我只希望結果以日期順序顯示.. – 2012-03-01 07:14:16

回答

1

你可以嘗試這樣的。 (該Deals_Redeemed值將是相同的所有行,你說有兩個表之間沒有關係)

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有一個連接ID列到你的tblCustomer你應該能夠做一個單獨的查詢,如

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

它不工作.. – 2012-03-01 07:10:23

+0

修復它。再試一次.. :) – jroyce 2012-03-01 07:17:35

+0

什麼是這個ID .. ??這些之間沒有任何關係.. – 2012-03-01 07:21:44

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