2012-03-01 60 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