我在查询此选择查询时遇到问题。
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。我希望我的case语句说明这是否为空guid(即'00000000-0000-0000-0000-000000000000')然后没有资助者,否则如果guid与ci_periodicbillings表中的列guid匹配,则返回ci_contributionfunders .name值,这是资助者的名字。
任何人都可以帮我整理一下吗?
答案 0 :(得分:0)
这是哪种方言?
您使用case when ... then ... else case when ... then ...
但我知道的任何方言中的正确语法是:
case when ... then ... when ... then ... else ... end
对于您的问题,我建议先离开,然后使用isnull
功能。
答案 1 :(得分:0)
使用语法,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],
答案 2 :(得分:0)
我不知道你的错误信息是什么,也不知道你的查询有什么问题,但是在提及整理时,我会做一些事情。
第一个是删除所有相关子查询,而是使用LEFT JOIN
,第二个是使用表别名,而不是在整个查询中重复完整的表名。最后,我会简化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 = <Expression>
语法,而不是<Expression> AS Alias
。我发现这更加清晰,因为您可以通过向下扫描选择列表中的哪一列来查看。 Aaron Bertrand写了good article或多或少地反映了我对这个主题的看法。
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';