简体   繁体   English

在正确的位置添加 CASE 表达式?

[英]Adding CASE expression in the correct spot?

I want to change this query:我想更改此查询:

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...
)

so that c.AccountB displays "X" if it was a "Y".以便 c.AccountB 显示“X”,如果它是“Y”。 So I want to do something like所以我想做类似的事情

CASE WHEN c.AccountB = 'Y' THEN 'X' ELSE 'c.AccountB END

Except I'm having a problem where some data (a.Category) is coming from the table a, and table a doesn't have a record in it equal to "Y", so the join doesn't get the category data from a.除了我遇到一些数据(a.Category)来自表 a 的问题,并且表 a 中没有等于“Y”的记录,因此连接不会从表中获取类别数据一个。 That field is therefore blank.因此该字段为空白。 I'm trying to avoid adding it to that table and would rather change the query.我试图避免将其添加到该表中,而宁愿更改查询。 How can I do this?我怎样才能做到这一点? What I think would work is:我认为可行的是:

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...
)

Where I put the CASE expression in the 7th line here by the asterisks ***我在这里用星号将 CASE 表达式放在第 7 行 ***

Will this return exactly the same records?这会返回完全相同的记录吗? This is a really long running query and difficult to test so I'm trying to run it as few times as possible, would like some input to help me so this doesn't turn into a 6 hour project.这是一个运行时间非常长且难以测试的查询,因此我尝试尽可能少地运行它,希望有一些输入可以帮助我,因此这不会变成一个 6 小时的项目。

EDIT: I had a typo, the first colummns selected were supposed to reference the first table - I changed it (table "t")编辑:我有一个错字,选择的第一列应该引用第一个表 - 我改变了它(表“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'll try: 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 '会尝试:

If you just want to select then:如果你只想 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
...

If instead you want to use this as part of a join you'll have to use it in your join.相反,如果您想将其用作联接的一部分,则必须在联接中使用它。 Since you don't show how "c" is joined, nor how "c" and "a" are related I will try to give an example:由于您没有显示“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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM