FULL OUTER JOIN查询中的输出错误

时间:2012-03-09 09:08:49

标签: sql sql-server sql-server-2008

表结构:
tblCustomer

Customer_id  created                 field1            field2        cardno       field14
------------------------------------------------------------------------------------------------    
1014         2010-05-25 12:51:59.547 Cell Phone        abc@lmn.com   1234567890   Test Card
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
  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,
  t1.field14,
  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 (t1.field14 <> 'Test Card'   and t1.field14 is null) 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),
  t1.field14
ORDER BY
  convert(varchar, CAST(ISNULL(t1.created,t2.created) AS DATETIME), 111) DESC

现在,我遇到一个问题,即第四列执行错误,因为我昨天在tbl_TransactionDishout表中有一条记录,其响应码= 0000,也不是“测试卡”,但我仍然得到0计数。

tbl_transaction和tblCustomer之间的关系是同一个cardno ...

1 个答案:

答案 0 :(得分:3)

根据您在评论部分中所说的内容,我认为您需要更改一段代码......

  • 我想要的是该卡不应该field14作为'Test card'


(t1.field14 <> 'Test Card' and t1.field14 is null)

=&GT;

(t1.field14 <> 'Test Card'  OR  t1.field14 is null)


构建逻辑表以检查您是否真的需要AND或者是否需要OR

field14     | (field14 <> 'Test Card') | (t1.field14 is null) | A OR B | A AND B
--------------------------------------------------------------------------------
'Test Card' |            FALSE         |        FALSE         | FALSE  | FALSE
NULL        |            NULL          |        TRUE          | TRUE   | FALSE
'Any Card'  |            TRUE          |        FALSE         | TRUE   | FALSE


编辑跟进评论

ORTRUE时,在上面的代码中使用Field14无法产生'Test Card'。两个测试都会产生FALSE,因此结果必须为FALSE

你需要分阶段分解。调试应该通过一次测试部分来完成,并逐步证明哪些工作可以隔离什么不可以。永远不要试图一下子解决所有事情,有条不紊地处理事情。

运行此测试...

SELECT
  *,
  CASE WHEN field14 <> 'Test Card'                     THEN 1 ELSE 0 END     Test1,
  CASE WHEN field14 IS NULL                            THEN 1 ELSE 0 END     Test2,
  CASE WHEN field14 <> 'Test Card' OR  field14 IS NULL THEN 1 ELSE 0 END     1_OR_2,
  CASE WHEN field14 <> 'Test Card' AND field14 IS NULL THEN 1 ELSE 0 END     1_AND_2
FROM
  tblCustomer