嵌套选择中的情况
我无法让 CASE 语句在嵌套选择中工作。我想我已经很接近了,但我无法完全正确地理解语法。到目前为止,我已经尝试过:
SELECT l.*,
Credit = (
CASE WHEN ISNULL(M.POSTCODE,'') <> '' THEN
sum(Credit) from Balance cb Where LEFT(LTrim(cb.AccountHolder),4) LIKE LEFT(LTrim(m.Company),4)
ELSE
sum(Credit) from Balance cb Where LEFT(LTrim(cb.AccountHolder),4) LIKE LEFT(LTrim(m.Company),4)
END
)
FROM live l INNER JOIN master m on m.ClientID = L.ClientID
WHERE ClientID = 12345
而且:
SELECT l.*,
(SELECT
CASE WHEN ISNULL(M.POSTCODE,'') <> '' THEN
sum(Credit) from Balance cb Where LEFT(LTrim(cb.AccountHolder),4) LIKE LEFT(LTrim(m.Company),4)
ELSE
sum(Credit) from Balance cb Where LEFT(LTrim(cb.AccountHolder),4) LIKE LEFT(LTrim(m.Company),4)
END
) AS Credit
FROM live l INNER JOIN master m on m.ClientID = L.ClientID
WHERE ClientID = 12345
这似乎与 http 上的 MSDN 示例相匹配://msdn.microsoft.com/en-us/library/ms181765.aspx。我错过了什么吗?
I'm having trouble getting a CASE statement to work in a nested select. I think I'm close but I can't quite get the syntax right. So far I've tried:
SELECT l.*,
Credit = (
CASE WHEN ISNULL(M.POSTCODE,'') <> '' THEN
sum(Credit) from Balance cb Where LEFT(LTrim(cb.AccountHolder),4) LIKE LEFT(LTrim(m.Company),4)
ELSE
sum(Credit) from Balance cb Where LEFT(LTrim(cb.AccountHolder),4) LIKE LEFT(LTrim(m.Company),4)
END
)
FROM live l INNER JOIN master m on m.ClientID = L.ClientID
WHERE ClientID = 12345
And also:
SELECT l.*,
(SELECT
CASE WHEN ISNULL(M.POSTCODE,'') <> '' THEN
sum(Credit) from Balance cb Where LEFT(LTrim(cb.AccountHolder),4) LIKE LEFT(LTrim(m.Company),4)
ELSE
sum(Credit) from Balance cb Where LEFT(LTrim(cb.AccountHolder),4) LIKE LEFT(LTrim(m.Company),4)
END
) AS Credit
FROM live l INNER JOIN master m on m.ClientID = L.ClientID
WHERE ClientID = 12345
Which seems to match the MSDN examples at http://msdn.microsoft.com/en-us/library/ms181765.aspx . Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要在求和子查询之前添加一些选择。
您的选择也完全相同,因此实际上不需要案例,除非您希望它们不同。
I think you need to add some selects before your sum subqueries.
Your select's are also exactly the same, so there isn't really a need for a case unless of course you intend for them to be different.