【发布时间】:2014-10-27 11:14:34
【问题描述】:
我对此选择查询的逻辑有问题。
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 值,即资助者的名称。
谁能帮我整理一下?
【问题讨论】:
-
你有什么问题?
-
问题是基于星号段的查询失败,这是一个试图返回捐款资助者名称的案例语句
-
“查询失败”能否再清楚一点?错误信息是什么?
-
嗨,Gareth - 显然我并没有想象中那么清醒,它只是构造不佳。在下面的答案中使用 case 语句,它现在可以正确返回。这是一个永远不会返回 NULL 值的列,因此不需要 ISNULL 子句。