Sql 返回select where查询的Case语句

Sql 返回select where查询的Case语句,sql,select,case,Sql,Select,Case,我对这个select查询的逻辑有问题 SELECT ISNULL((SELECT cs_facilities.name from cs_facilities where ci_periodicBillings.facility = cs_facilities.guid),'Unknown') as [Care Centre], ISNULL((SELECT cs_clients.title + ' ' + cs_clients.forename + ' ' + cs_clients.surn

我对这个select查询的逻辑有问题

SELECT

ISNULL((SELECT cs_facilities.name from cs_facilities where ci_periodicBillings.facility = cs_facilities.guid),'Unknown') as [Care Centre],

ISNULL((SELECT cs_clients.title + ' ' + cs_clients.forename + ' ' + cs_clients.surname from cs_clients where ci_periodicBillings.client = cs_clients.guid),'Unknown') as [Resident Name],

***CASE WHEN ci_periodicbillings.contributionFunder = '00000000-0000-0000-0000-000000000000' then 'No Funder' ELSE (select ci_contributionFunders.name from ci_contributionFunders where ci_contributionFunders.guid = ci_periodicBillings.contributionFunder) END as [Contribution Funder],***

ISNULL((SELECT cs_clients.ADMISSION from cs_clients where ci_periodicBillings.client = cs_clients.guid),'') as [Admission Date],

ISNULL(ci_periodicbillings.RATE,'') as [Weekly Rate],

CASE WHEN BILLRES = 1 THEN 'Self Payer' ELSE CASE WHEN BILLRES = 2 THEN 'Top up' ELSE 'Other Funder' END END as [Type of Funding],

ISNULL(ci_periodicbillings.LASTBILL,'') as [Billing Period Start Date],

ISNULL(ci_periodicbillings.NEXTBILL,'') as [Billing Period Next Repeat Date],

CASE WHEN invoiceDaysOffset = 22 then 'In Arrears' ELSE CASE WHEN invoicedaysoffset = -6 then 'In Advance' ELSE '' END END as [Advance or Arrears Billing]


from ci_periodicBillings
where facility = '00000000-0000-0000-0000-000000000000'
and ENDBILL = '1900-01-01 00:00:00.000'
order by [Care Centre], [Resident Name]
我相信各位专家会发现,有问题的线条是用三星突出显示的

我希望你能在这里看到我的逻辑。在ci_periodicbillings表中有一列标记为contributionFunder。这是指ci_contributionFunders表中的guid。我想让我的案例陈述说明,如果这是一个空guid,即“00000000-0000-0000-0000-000000000000”,则没有出资人,否则,如果guid与ci_periodicbillings表中的列guid匹配,请返回作为出资人名称的ci_contributionfunders.name值

有人能帮我整理一下吗?

这是哪种方言

您可以在以下情况下使用case。。。然后否则的话。。。然后

但我所知道的任何方言的正确语法都是:

当。。。然后什么时候然后其他的结束


对于您的问题,我建议先左键连接,然后使用isnull函数。

使用您的语法,case语句可以处理:

CASE WHEN ci_periodicbillings.contributionFunder = '00000000-0000-0000-0000-000000000000' then 'No Funder' when ci_periodicbillings.contributionFunder <> '00000000-0000-0000-0000-000000000000' THEN (select ci_contributionFunders.name from ci_contributionFunders where ci_contributionFunders.guid = ci_periodicBillings.contributionFunder) END as [Contribution Funder],

我不知道您的错误消息是什么,也看不出您的查询有任何实际错误,但在整理它时,我会做一些事情

第一种方法是删除所有相关的子查询,改为使用左联接,第二种方法是使用表别名,而不是在整个查询中重复完整的表名。最后,我将简化case语句,而不是嵌套它们:

SELECT  ISNULL(f.Name, 'Unknown') AS [Care Centre],
        ISNULL(c.title + ' ' + c.forename + ' ' + c.surname, 'Unknown') AS [Resident Name],
        ISNULL(cf.Name, 'No Funder') AS [Contribution Funder],
        ISNULL(c.ADMISSION, '') AS [Admission Date],
        ISNULL(pb.RATE,'') AS [Weekly Rate],
        CASE pb.BILLRES
            WHEN 1 THEN 'Self Payer' 
            WHEN 2 THEN 'Top up' 
            ELSE 'Other Funder' 
        END AS [Type of Funding],
        ISNULL(pb.LASTBILL,'') AS [Billing Period Start Date],
        ISNULL(pb.NEXTBILL,'') AS [Billing Period Next Repeat Date],
        CASE pb.invoiceDaysOffset
            WHEN 22 THEN 'In Arrears' 
            WHEN -6 THEN 'In Advance' 
            ELSE ''
        END AS [Advance or Arrears Billing]
FROM    ci_periodicBillings AS pb
        LEFT JOIN cs_facilities AS f
            ON f.guid = pb.facility
        LEFT JOIN cs_clients AS c
            ON c.guid = pb.client
        LEFT JOIN ci_contributionFunders AS cf
            ON cf.guid = pb.contributionFunder
            AND pb.contributionFunder != '00000000-0000-0000-0000-000000000000'
WHERE   pb.facility = '00000000-0000-0000-0000-000000000000'
AND     pb.ENDBILL = '1900-01-01 00:00:00.000';
我最后要做的一件事是切换到SQL Server允许的Alias=语法,而不是作为Alias,但我故意将其分开,因为这是非常主观的。我发现这一点更加清晰,因为您可以通过向下扫描选择列表立即看到您正在查看的列。亚伦·伯特兰写了一篇文章,或多或少地反映了我对这个主题的感受

SELECT  [Care Centre] = ISNULL(f.Name, 'Unknown'),
        [Resident Name] = ISNULL(c.title + ' ' + c.forename + ' ' + c.surname, 'Unknown'),
        [Contribution Funder] = ISNULL(cf.Name, 'No Funder'),
        [Admission Date] = ISNULL(c.ADMISSION, ''),
        [Weekly Rate] = ISNULL(pb.RATE,''),
        [Type of Funding] = CASE pb.BILLRES
                                WHEN 1 THEN 'Self Payer' 
                                WHEN 2 THEN 'Top up' 
                                ELSE 'Other Funder' 
                            END,
        [Billing Period Start Date] = ISNULL(pb.LASTBILL,''),
        [Billing Period Next Repeat Date] = ISNULL(pb.NEXTBILL,''),
        [Advance or Arrears Billing] = CASE pb.invoiceDaysOffset
                                            WHEN 22 THEN 'In Arrears' 
                                            WHEN -6 THEN 'In Advance' 
                                            ELSE ''
                                        END
FROM    ci_periodicBillings AS pb
        LEFT JOIN cs_facilities AS f
            ON f.guid = pb.facility
        LEFT JOIN cs_clients AS c
            ON c.guid = pb.client
        LEFT JOIN ci_contributionFunders AS cf
            ON cf.guid = pb.contributionFunder
            AND pb.contributionFunder != '00000000-0000-0000-0000-000000000000'
WHERE   pb.facility = '00000000-0000-0000-0000-000000000000'
AND     pb.ENDBILL = '1900-01-01 00:00:00.000';

你的问题是什么?问题是,根据带星号的部分,查询失败,这是一个试图返回出资人姓名的案例陈述。你能更清楚地说明查询失败的原因吗?错误信息是什么?嗨,加雷斯-很明显,我并不像我想象的那样清醒,只是结构不好。使用下面我的答案中的case语句,它现在可以正确返回。这是一个永远不会返回NULL值的列,因此不需要ISNULL子句。您好,我已更新以考虑您的语法-当ci_periodicbillings.contributionFunder='00000000-0000-0000-000000000000'时为CASE,当ci_periodicbillings.contributionFunder'00000000-0000-0000-000000000000'时为'No Funder'ci_contributionFunders.name from ci_contributionFunders.guid=ci_periodicBillings.contributionFunder END as[Contribution Funder],-这只是SQL management studio中的选择查询。它仍然返回一个错误,并且所选零件带有下划线。我不知道如何在这里表达join/isnull..1这更适合作为注释。2所有支持case语句的SQL版本都允许嵌套case语句。语法可能是非正统的,但它是正确的。我刚刚意识到我的查询写得很糟糕,并修复了它-很抱歉浪费了你的时间!您好,Gareth-这看起来比我的工作要干净得多,但在切换到连接结构化查询时,有一件事我很头疼,那就是在末尾添加多个WHERE子句。在我最初的查询中,我有大约4个where子句,包括-where pb.ENDBILL='01/01/1900'-您能告诉我如何将这种灵活性应用到您的格式中吗?对不起,我不知道为什么会遗漏它们。我刚刚在答案中加入了它们。WHERE的结构不受连接的影响。一个问题-我可以看到结果,但您能否解释此语法的工作原理:左连接ci_contributionFunders作为cf ON cf.guid=pb.contributionFunder和pb.contributionFunder!='00000000-0000-0000-0000-0000-000000000000'这只是联接中的一个附加子句,表示我不希望任何来自ci_contributionFunders的行,其中pb.contributionFunder='000…'-这样,当pb.contributionFunder='000…,您就不需要执行case saement case,只需使用ISNULLcf.Name,“未知”,因为捐款资助者为0的任何行都不会返回cf.nameHi的结果,我现在正在尝试Alias=Expression,这很顺利,不过还有一些问题。1-SQL中是否有正式采用此开关的设置,因为我在打开一个[尝试引用列/表/db和自动填充的括号]时一直遇到问题。我非常喜欢自动填充功能,因此如果可能的话,希望保留它。2-案例查询对数字不起作用-请看这不起作用:[销售分类账交易类型]=案例id.amount当>0.00时,然后是'Invoice'当<0.00时,然后是'Credit Note'其他'No Value'结束