2012-03-03 187 views
0

表結構:
tblCustomer結合了兩種查詢到一個

Customer_id created     field1   field2  cardno 
-------------------------------------------------------------------------------  
1014   2010-05-25 12:51:59.547 Cell Phone  [email protected] 1234567890 
1015   2010-08-15 12:51:59.547 Email    [email protected] 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
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

+0

尼斯(+1),但不會對已登記...查詢2結果在從Deals_Redeemed分開SUM列這些領域NULL結果不存在的領域? – Tahbaza 2012-03-03 14:49:11

+0

增加了'ISNULL'來說明這一點。第二個查詢不需要字段上的「ISNULL」。 – 2012-03-03 15:14:04

+0

是的..你的第二個答案几乎是正確的,除了一個條件是「t1.cardno = t2.VirtualCard」在外部連接條件... 非常感謝@MizardX ... – 2012-03-05 05:21:37

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] 
+1

Msg 325,Level 15,State 1,Line 17 'pivot'附近語法不正確。您可能需要將當前數據庫的兼容級別設置爲更高的值才能啓用此功能。請參閱ALTER DATABASE的SET COMPATIBILITY_LEVEL選項的幫助。 – 2012-03-05 05:00:03