多连接查询和子查询

问题描述:

表结构:tbl_transaction和tblCustomer之间
tblCustomer多连接查询和子查询

Customer_id created     field1   field2  cardno  field14 
------------------------------------------------------------------------------------------------  
1014   2010-05-25 12:51:59.547 Cell Phone  [email protected] 1234567890 Test Card 
1015   2010-08-15 12:51:59.547 Email    [email protected] 2345678891 NULL 

tbl_TransactionDishout

Trnx_id offerNo TerminalID  Created     VirtualCard 
------------------------------------------------------------------- 
1   1014  170924690436418 2010-05-25 12:51:59.547 1234567890 

关系是有相同cardno。
是否有可能得到的结果如下日期明智记录:

   Enrolled Enrolled as Email Enrolled as Text Deals Redeemed 
<First Date> 7   5     2    6 
<Next Date> 9   3     6    14 

日期应该是从,即使它具有零条记录这两个表..

Enrolled   - Total No of records that is summation of Enrolled as Email and Enrolled as Text.  
Enrolled as Email - Where field1 = 'Email' from tblCustomer table 
Enrolled as Text - Where field1 = 'cell phone' from tblCustomer table 
Deals Redeemed - Where field14 <> 'Test Card' from tblCustomer table and  
        where DishoutResponsecode = '0000' from tbl_Transaction table 

我现有的查询:

SELECT 
convert(varchar, CAST(ISNULL(t1.created,t2.created) AS DATETIME), 111) created, 
COUNT(CASE WHEN (t1.field1 = 'E-mail' or t1.field1 = 'Cell Phone') and (t1.field14 <> 'Test Card' or t1.field14 is null) THEN 1 END) Enrolled, 
COUNT(CASE WHEN t1.field1 = 'E-mail'   and (t1.field14 <> 'Test Card' and t1.field14 is null) THEN 1 END) Enrolled_as_Email, 
COUNT(CASE WHEN t1.field1 = 'Cell Phone'  and (t1.field14 <> 'Test Card' and t1.field14 is null) THEN 1 END) Enrolled_as_Cell, 
COUNT(CASE WHEN t2.DishoutResponseCode = '0000' and (IsNull(t1.field14, '') <> 'Test Card') THEN 1 END) Deals_Redeemed 
FROM tblCustomer AS t1 
FULL OUTER JOIN 
     tbl_TransactionDishout t2 
ON t1.cardno = t2.VirtualCard and t1.created = t2.created 
GROUP BY 
     convert(varchar, CAST(ISNULL(t1.created,t2.created) AS DATETIME),  111) 
ORDER BY 
     convert(varchar, CAST(ISNULL(t1.created,t2.created) AS DATETIME), 111) DESC   

最后4-5记录tblCustomer表

created     cardno    field14 
---------------------------------------------------------- 
2012-03-07 10:03:00.034 1234007600101240  
2012-03-05 04:02:00.040 1234007600602122  
2012-03-01 06:25:50.400 1234010400972168  Test Card 
2012-03-01 30:05:30.022 555566669999   Test Card 
2012-03-01 50:50:20.450 666677778888   Test Card  

最后4-5记录tbl_TransactionDisout表

created     VirtualCard   DishoutResponseCode 
----------------------------------------------------------------------- 
2012-03-09 13:18:02.703 1234010400972168 0010 
2012-03-09 13:17:35.307 1234010400972168 0002 
2012-03-09 13:17:14.237 1234010400972168 0007 
2012-03-09 13:16:57.650 1234010400972168 0002 
2012-03-08 21:13:57.137 1234010400475686 0000 
2012-03-08 16:50:38.273 1234010400972168 0002 
2012-03-08 16:50:26.070 1234010400972168 0007 
2012-03-08 16:49:49.793 1234010400972168 0002  

所以在这个只有一张卡具有响应码“0000”,也没有“测试卡”。但我我得到所有的代码='0000'的卡,也有'测试卡',这意味着field14无法比较,因为它是从不同的表格和不同的日期。

+0

这是可能的第三天,你问同样的问题。你为什么每天都会开始一个新的问题? – Jaques 2012-03-13 05:46:25

+0

因为我没有得到我想要的回复@Jaques – 2012-03-13 05:51:19

+0

tbl_TransactionDishout表的布局是什么,因为您指定了DishoutResponseCode,但不会在顶部显示它。第二,这是排除时间的日期?所以你想每天分组? – Jaques 2012-03-13 06:29:15

这是我回来的结果在使用您在主要问题中指定的数据时使用此查询:

SELECT 
    convert(varchar, CAST(ISNULL(t1.created,t2.created) AS DATETIME), 111) created, 
    COUNT(CASE WHEN (t1.field1 = 'Email' or t1.field1 = 'Cell Phone') and (t1.field14 <> 'Test Card' or t1.field14 is null) THEN 1 END) Enrolled, 
    COUNT(CASE WHEN t1.field1 = 'Email'   and (IsNull(t1.field14, '') <> 'Test Card') THEN 1 END) Enrolled_as_Email, 
    COUNT(CASE WHEN t1.field1 = 'Cell Phone'  and (IsNull(t1.field14, '') <> 'Test Card') THEN 1 END) Enrolled_as_Cell, 
    COUNT(CASE WHEN t2.DishoutResponseCode = '0000' and (IsNull(t1.field14, '') <> 'Test Card') THEN 1 END) Deals_Redeemed 
FROM 
    tblCustomer AS t1 
FULL OUTER JOIN 
    tbl_TransactionDishout t2 
    ON t1.cardno = t2.VirtualCard 
    AND t1.created = t2.created 
GROUP BY 
    convert(varchar, CAST(ISNULL(t1.created,t2.created) AS DATETIME), 111) 
ORDER BY 
    convert(varchar, CAST(ISNULL(t1.created,t2.created) AS DATETIME), 111) DESC 

结果:

created      Enrolled Enrolled_as_Email Enrolled_as_Cell Deals_Redeemed 
------------------------------ ----------- ----------------- ---------------- -------------- 
2012/03/09      0   0     0    0 
2012/03/08      0   0     0    1 
2012/03/07      1   0     1    0 
2012/03/05      1   0     1    0 
2012/03/01      0   0     0    0 
+0

Now ..it显示DishoutResponsecode ='0000'的所有行,但不检查tblcustomer中的field14 ... 所以我发现的问题是这样的:field14将检查日期也来自tblCustomer表..所以corss检查不工作.. @ Jaques – 2012-03-13 09:15:49

+0

我不明白你的意思是它没有检查tblcustomer中的field14。如果在代码中指定的是正确的?也许你应该发布你的查询,你再次使用。在tblCusomer和tbl_TransactionDishout中创建的字段是否总是相同?你还可以添加一些额外的数据,因为我已经创建了它来测试。 – Jaques 2012-03-13 09:29:44

+0

@VishalSuthar我在我现在创建的表上使用了这个查询,并且得到了预期的结果。请给我一些没有产生预期结果的数据,然后我可以看到什么是错的。 – Jaques 2012-03-13 09:41:49