[英]Adding CASE expression in the correct spot?
我想更改此查询:
select
t.AccountA
,t.AccountB
,t.totalNumber
,a.Category
from TableA t
left join Accounts a
on t.ActNum = a.ActNum
left join
(select distinct
s.col1
from (
select ....
from Table
group by...
) st
left join (select S....
group by..
) g on...
left join (select... on ...
) t on ...
where...
)
以便 c.AccountB 显示“X”,如果它是“Y”。 所以我想做类似的事情
CASE WHEN c.AccountB = 'Y' THEN 'X' ELSE 'c.AccountB END
除了我遇到一些数据(a.Category)来自表 a 的问题,并且表 a 中没有等于“Y”的记录,因此连接不会从表中获取类别数据一个。 因此该字段为空白。 我试图避免将其添加到该表中,而宁愿更改查询。 我怎样才能做到这一点? 我认为可行的是:
select
t.AccountA
,t.AccountB
,t.totalNumber
,a.Category
from TableA t
left join ****** (Select CASE WHEN t.AccountB = 'Y' THEN 'X' ELSE 't.AccountB END Accounts a)
on t.ActNum = a.ActNum
left join
(select distinct
col1
from (
select ....
from Table
group by...
) sta
left join (select S....
group by..
) g on...
left join (select... on ...
) t on ...
where...
)
我在这里用星号将 CASE 表达式放在第 7 行 ***
这会返回完全相同的记录吗? 这是一个运行时间非常长且难以测试的查询,因此我尝试尽可能少地运行它,希望有一些输入可以帮助我,因此这不会变成一个 6 小时的项目。
编辑:我有一个错字,选择的第一列应该引用第一个表 - 我改变了它(表“t”)
First, this might be as simple as getting rid of the single quote before c.AccountB CASE WHEN c.AccountB = 'Y' THEN 'X' ELSE c.AccountB END
Otherwise I'm not quite sure I understand what you want but I '会尝试:
如果你只想 select 那么:
select
c.AccountA
,CASE WHEN c.AccountB = 'Y' THEN 'X' ELSE c.AccountB END AccountB
,totalNumber
,a.Category
from TableA t
left join Accounts a
on t.ActNum = a.ActNum
left join
...
相反,如果您想将其用作联接的一部分,则必须在联接中使用它。 由于您没有显示“c”是如何连接的,也没有显示“c”和“a”是如何相关的,我将尝试举一个例子:
select
c.AccountA
,CASE WHEN c.AccountB = 'Y' THEN 'X' ELSE c.AccountB END AccountB
,totalNumber
,a.Category
from CheckRegister c
left join Accounts a
on a.ActNum = c.AccountA
left join Accounts b
on b.ActNum = CASE WHEN c.AccountB = 'Y' THEN 'X' ELSE c.AccountB END
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.