表結構:
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之間 關係......
尼斯(+1),但不會對已登記...查詢2結果在從Deals_Redeemed分開SUM列這些領域NULL結果不存在的領域? – Tahbaza 2012-03-03 14:49:11
增加了'ISNULL'來說明這一點。第二個查詢不需要字段上的「ISNULL」。 – 2012-03-03 15:14:04
是的..你的第二個答案几乎是正確的,除了一個條件是「t1.cardno = t2.VirtualCard」在外部連接條件... 非常感謝@MizardX ... – 2012-03-05 05:21:37