【问题标题】:Case Statements using variables not working使用变量的案例陈述不起作用
【发布时间】:2013-12-12 08:04:44
【问题描述】:

编辑:使查询更简单,同样的问题。

我基本上有两个声明,使用给定的帐户 ID 和申报期限来选择一个人是否已故。

当输入一个联名账户持有人的账户时,两个查询(pr 和 jo)都会返回值和 case 语句中显示的正确指示符。

当第二个查询没有返回值时(因为没有联名账户持有人被指示活着或已故),那么 case 语句似乎不起作用并且没有返回值。

为什么会发生这种情况,即使第二个表不返回值,如何让 case 语句仍然返回值?

谢谢!

SELECT 
CASE
            WHEN    pr.fintPriDeceased=0 and (jo.fintJointDeceased=0 or jo.fintJointDeceased='')
            THEN    0
            ELSE    1
            END AS fintDeceased
FROM

(SELECT a.FLNGCUSTOMERKEY as flngPrimaryCustomerKey,
        a.flngAccountKey as flngPrimaryAccountKey,
        CASE
            WHEN ci.fdtmCease<>'12-31-9999' 
            THEN    1
            ELSE    0
            END AS fintPriDeceased
FROM    tblAccount a,
        tblPeriod p,
        tblCustomerInfo ci
WHERE   a.flngAccountKey    = @plngAccountKey and
        p.fdtmFilingPeriod  = @pdtmFilingPeriod and
        a.flngAccountKey    = p.flngAccountKey and
        a.FLNGCUSTOMERKEY   = ci.flngCustomerKey) pr

(SELECT a.FLNGCUSTOMERKEY as flngJointCustomerKey,
        p.FLNGACCOUNTKEY as flngJointAccountKey,
        CASE
            WHEN ci.fdtmCease<>'12-31-9999' 
            THEN    1
            ELSE    0
            END AS fintJointDeceased
FROM    tblAccount a,
        tblPeriod p,
        tblCustomerInfo ci
WHERE   p.FLNGJOINTACCOUNTKEY   = @plngAccountKey and
        p.fdtmFilingPeriod      = @pdtmFilingPeriod and
        a.flngAccountKey        = p.flngAccountKey and
        a.FLNGCUSTOMERKEY       = ci.flngCustomerKey) jo

【问题讨论】:

  • -1:这个查询太混乱了,我并不惊讶它不起作用。抱歉,但我相信它无法修复。
  • p.FLNGJOINTACCOUNTKEY 引用的列的数据类型是什么?
  • 现在编辑了这个查询,使其更具可读性,希望我遇到的问题会更清楚
  • pr 和 jo 查询之间的联系在哪里?
  • pr 拉取主账户持有人信息,jo 拉取联名账户持有人信息。 pr 总是被填充。 jo 有时不是,当 jo 未填充时,case 语句不会返回值

标签: sql sql-server case


【解决方案1】:

由于您的最后一条评论说“公关总是被填充。乔有时不是”......
这意味着您需要两者之间的LEFT OUTER JOIN(可选连接)...
这需要明确您的 JOIN:您必须有一个 ON 子句,准确说明哪些列是相等/比较的。

然后,您还需要在您的 CASE 声明中检查IS NULL,以防找不到联名账户。

SELECT 
CASE
            WHEN    pr.fintPriDeceased=0 
            and     (jo.fintJointDeceased IS NULL 
                    OR jo.fintJointDeceased=0)
            THEN    0
            ELSE    1
            END AS fintDeceased

FROM
(SELECT a.FLNGCUSTOMERKEY as flngPrimaryCustomerKey,
        a.flngAccountKey as flngPrimaryAccountKey,
        CASE
            WHEN ci.fdtmCease<>'12-31-9999' 
            THEN    1
            ELSE    0
            END AS fintPriDeceased
FROM    tblAccount a,
        tblPeriod p,
        tblCustomerInfo ci
WHERE   a.flngAccountKey    = @plngAccountKey and
        p.fdtmFilingPeriod  = @pdtmFilingPeriod and
        a.flngAccountKey    = p.flngAccountKey and
        a.FLNGCUSTOMERKEY   = ci.flngCustomerKey) pr

LEFT OUTER JOIN 
(SELECT a.FLNGCUSTOMERKEY as flngJointCustomerKey,
        p.FLNGACCOUNTKEY as flngJointAccountKey,
        CASE
            WHEN ci.fdtmCease<>'12-31-9999' 
            THEN    1
            ELSE    0
            END AS fintJointDeceased
FROM    tblAccount a,
        tblPeriod p,
        tblCustomerInfo ci
WHERE   p.FLNGJOINTACCOUNTKEY   = @plngAccountKey and
        p.fdtmFilingPeriod      = @pdtmFilingPeriod and
        a.flngAccountKey        = p.flngAccountKey and
        a.FLNGCUSTOMERKEY       = ci.flngCustomerKey) jo
ON pr.flngPrimaryCustomerKey = jo.flngJointCustomerKey
AND pr.flngPrimaryAccountKey = jo.flngJointAccountKey

(您可能需要更改上面 LEFT OUTER JOIN 底部的 ON 子句...我不知道您的 customerkey 和 accountkey 在两个 SELECT 之间是否通常匹配。)

希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多