Sql 在现有子查询中添加另一个子查询

Sql 在现有子查询中添加另一个子查询,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,是否可以从相同的表日期记录中获得如下结果: Enrolled Enrolled as Email Enrolled as Text Deals Redeemed <First Date> 7 5 2 6 <Next Date> 9 3 6 14 我当前的查询如

是否可以从相同的表日期记录中获得如下结果:

               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
这对于前三列很好,但是对于“Deals Received”这四列很好,这是来自另一个表的子查询

Select COUNT(*) from tbl_TransactionDishout where DishoutResponseCode = '0000'
表结构如下:
表名为“tbl_TransactionDishout”

你能这样试试吗。(所有行的Deals_赎回值都相同,您说过两个表之间没有关系)


如果您的tbl_TransactionDishout有一个连接到您的tbl客户的连接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

也许是这样的:

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

tbl客户和tbl_交易之间的关系是什么?DishoutResponseCode是responsecode吗?它们之间没有关系..我只希望结果按日期顺序显示..这个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
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