将两个查询合并为一个

时间:2012-03-03 13:59:42

标签: sql-server sql-server-2008

表结构:
tblCustomer

Customer_id  created                 field1            field2        cardno
-------------------------------------------------------------------------------    
1014         2010-05-25 12:51:59.547 Cell Phone        abc@lmn.com   1234567890
1015         2010-08-15 12:51:59.547 Email             abc@xyz.com   2345678891

tbl_TransactionDishout

Trnx_id   offerNo   TerminalID      Created                  VirtualCard
-------------------------------------------------------------------
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 
   CAST(FLOOR(CAST(t.created AS FLOAT )) as Datetime) 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 as t 
   group by t.created 
   order by t.created desc   

哪个显示器:

   create                   Enrolled  Enrolled_as_Email  Enrolled_as_Cell
   ----------------------------------------------------------------------
   2012-03-01 00:00:00.000  3     1              2
   2012-02-29 00:00:00.000  1     0              1  

第二个:

   select CAST(FLOOR(CAST(t.created AS FLOAT )) as Datetime) created,
   count(*) [Deals_Redeemed] from tbl_TransactionDishout t 
   left join tblCustomer c on t.VirtualCard = c.cardno 
   group by CAST(FLOOR(CAST(t.created AS FLOAT )) as Datetime) 
   order by t.created desc

哪个显示器:

   create                   Deals_Redeemed
   ---------------------------------------
   2012-03-02 00:00:00.000  1
   2012-03-01 00:00:00.000  6
   2012-02-28 00:00:00.000  1
   2012-02-27 00:00:00.000  2

现在我想要一个包含两者日期的记录,并且应该合并为一个记录。

但是它给了我仅包含在tblCustomer表中的日期结果..

现在,如何获得Deals_redeemed ..? tbl_transaction和tblCustomer之间的关系具有相同的cardno ...

2 个答案:

答案 0 :(得分:2)

SELECT
    CAST(t1.created AS DATE) created,
    COUNT(t1.field1) Enrolled,
    COUNT(CASE t1.field1 WHEN 'E-mail' THEN 1 END) Enrolled_as_Email,
    COUNT(CASE t1.field1 WHEN 'Cell Phone' THEN 1 END) Enrolled_as_Cell,
    COUNT(t2.created) Deals_Redeemed
FROM tblCustomer AS t1
LEFT JOIN tbl_TransactionDishout t2
    ON t1.cardno = t2.VirtualCard
GROUP BY CAST(t1.created AS DATE)
ORDER BY CAST(t1.created AS DATE) DESC

修改:将条件更改为cardno,并从FULL JOIN更改为LEFT JOIN

答案 1 :(得分:1)

你可以Pivot()

select [create]
, sum([Email]+[Cell Phone]) as [Enrolled]
, max([Email]) as [Enrolled as Email]
, max([Cell Phone]) as [Enrolled as Cell Phone]
, max([Deal Redeemed]) as [Deals Redeemed]
from (
    select [create]=created
    , create1=created
    , field1
    from tblCustomer
    union all
    select [create]=created
    , create1=created
    , field1='Deal Redeemed'
    from tbl_TransactionDishout
) p
pivot (
    count(create1)
    for field1 in ([Cell Phone],[Email],[Deal Redeemed])
) pv
group by [create]
order by [create]