嵌套选择中的情况

发布于 2024-12-27 23:36:06 字数 1039 浏览 0 评论 0原文

我无法让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情痴 2025-01-03 23:36:06

我认为您需要在求和子查询之前添加一些选择。

SELECT l.*,
Credit = (
   CASE WHEN ISNULL(M.POSTCODE,'') <> '' THEN
     (SELECT sum(Credit) from Balance cb Where LEFT(LTrim(cb.AccountHolder),4) LIKE LEFT(LTrim(m.Company),4))
   ELSE
     (SELECT 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

您的选择也完全相同,因此实际上不需要案例,除非您希望它们不同。

I think you need to add some selects before your sum subqueries.

SELECT l.*,
Credit = (
   CASE WHEN ISNULL(M.POSTCODE,'') <> '' THEN
     (SELECT sum(Credit) from Balance cb Where LEFT(LTrim(cb.AccountHolder),4) LIKE LEFT(LTrim(m.Company),4))
   ELSE
     (SELECT 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

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.

向地狱狂奔 2025-01-03 23:36:06
SELECT l.*,
      CASE 
         WHEN ISNULL(M.POSTCODE,'') <> '' THEN
             (SELECT sum(Credit) 
             from Balance cb 
             Where LEFT(LTrim(cb.AccountHolder),4) LIKE LEFT(LTrim(m.Company),4))
         ELSE
             (SELECT 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
SELECT l.*,
      CASE 
         WHEN ISNULL(M.POSTCODE,'') <> '' THEN
             (SELECT sum(Credit) 
             from Balance cb 
             Where LEFT(LTrim(cb.AccountHolder),4) LIKE LEFT(LTrim(m.Company),4))
         ELSE
             (SELECT 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文