在 SQL 语句中使用 CASE WHEN

发布于 2024-11-02 22:55:32 字数 2099 浏览 0 评论 0原文

我必须通过比较两个 ID 列中的数据来显示信息,一列包含“ALL”和数字作为 ID,而另一列中只有数字作为 ID。我的问题是,我无法将字符和数字列与数字列进行比较。所以我使用的是 CASE WHEN。

如果值为“ALL”,则在输出中显示“ALL”,否则显示匹配记录的名称。

这是代码:

CASE
  WHEN secorg.org_id = 'ALL' THEN 'ALL'
  WHEN secorg.org_id = progmap.org_id THEN secorg.org_name
END AS org_name,

条件是这样的:“secorg.org_id = progmap.org_id”,它基于 id,如果 id 相同,我必须显示 secorg.org_name。

这是整个查询:

SELECT distinct program_id,
                prog_name,
                case
                  when Eitc_Active_Switch = '1' then 'ON'
                  when Eitc_Active_Switch = '0'then 'OFF'
                End as Prog_Status,
                progmap.client_id,
                case
                  when secorg.org_id = 'ALL' then 'ALL'
                  --when secorg.org_id = progmap.org_id then secorg.org_name
                  else secorg.org_name
                end as org_name,
                case
                  when prog.has_post_calc_screen = 'True' then 'True'
                  Else 'False'
                End as Referal_ID,
                case
                  when progmap.program_ID IN ( 'AMC1931', 'AMCABD', 'AMCMNMI',
                                               'AMC' )
                       And sec.calwinexists_ind = '1' then 'Yes'
                  when progmap.program_ID IN ( 'AMC1931', 'AMCABD', 'AMCMNMI',
                                               'AMC' )
                       And sec.calwinexists_ind = '0'then 'No'
                  when progmap.program_ID NOT IN (
                       'AMC1931', 'AMCABD', 'AMCMNMI', 'AMC' ) then
                  'N/A'
                End as calwin_interface,
                sec.Client_name
FROM   ref_programs prog (nolock)
       LEFT OUTER JOIN ref_county_program_map progmap (nolock)
         ON progmap.program_id = prog.prog_id
            AND progmap.CLIENT_ID = prog.CLIENT_ID
       INNER join sec_clients sec (nolock)
         on sec.client_id = progmap.Client_id
       Inner join sec_organization secorg (nolock)
         on secorg.org_id = progmap.org_id  

I have to display information by comparing data in two ID columns, one column has 'ALL' and numbers as ID's while the other has only numbers in it as ID's. My problem is, I cannot compare character and number columns with a number column. So I am using CASE WHEN.

If the value is 'ALL' then display 'ALL' in the output, else display name for the matching records.

Here is the code:

CASE
  WHEN secorg.org_id = 'ALL' THEN 'ALL'
  WHEN secorg.org_id = progmap.org_id THEN secorg.org_name
END AS org_name,

the condition is this: 'secorg.org_id = progmap.org_id' which is based on the id and I have to display secorg.org_name if the id's are same.

Here is the entire query:

SELECT distinct program_id,
                prog_name,
                case
                  when Eitc_Active_Switch = '1' then 'ON'
                  when Eitc_Active_Switch = '0'then 'OFF'
                End as Prog_Status,
                progmap.client_id,
                case
                  when secorg.org_id = 'ALL' then 'ALL'
                  --when secorg.org_id = progmap.org_id then secorg.org_name
                  else secorg.org_name
                end as org_name,
                case
                  when prog.has_post_calc_screen = 'True' then 'True'
                  Else 'False'
                End as Referal_ID,
                case
                  when progmap.program_ID IN ( 'AMC1931', 'AMCABD', 'AMCMNMI',
                                               'AMC' )
                       And sec.calwinexists_ind = '1' then 'Yes'
                  when progmap.program_ID IN ( 'AMC1931', 'AMCABD', 'AMCMNMI',
                                               'AMC' )
                       And sec.calwinexists_ind = '0'then 'No'
                  when progmap.program_ID NOT IN (
                       'AMC1931', 'AMCABD', 'AMCMNMI', 'AMC' ) then
                  'N/A'
                End as calwin_interface,
                sec.Client_name
FROM   ref_programs prog (nolock)
       LEFT OUTER JOIN ref_county_program_map progmap (nolock)
         ON progmap.program_id = prog.prog_id
            AND progmap.CLIENT_ID = prog.CLIENT_ID
       INNER join sec_clients sec (nolock)
         on sec.client_id = progmap.Client_id
       Inner join sec_organization secorg (nolock)
         on secorg.org_id = progmap.org_id  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

陌生 2024-11-09 22:55:32

为什么不将数字列转换为 varchar 列?

如果您使用的是 SQL SERVER,您可以这样做:

CONVERT(VARCHAR,secorg.org_id) = CONVERT(VARCHAR,progmap.org_id)

当同时为“ALL”和数字的列为“All”时,您必须执行外连接,因为它无法进行内连接到另一张桌子。

对于基于上面代码的快速修复,您只需将第二个 WHEN 子句更改为如下所示(再次假设您使用的是 MS SQL SERVER):

WHEN CONVERT(VARCHAR,secorg.org_id) = CONVERT(VARCHAR,progmap.org_id) THEN secorg.org_name

尝试将其作为查询:

SELECT DISTINCT 
    program_id, 
    prog_name,
    CASE Eitc_Active_Switch
        WHEN '1' THEN 'ON'
        ELSE 'OFF'
    END AS Prog_Status,
    progmap.client_id,
    ISNULL(secorg.org_name,'ALL') AS org_name,
    CASE prog.has_post_calc_screen
        WHEN 'True' THEN 'True'
        ELSE 'False'
    END AS Referal_ID,
    CASE WHEN progmap.program_ID IN ('AMC1931','AMCABD','AMCMNMI','AMC') AND sec.calwinexists_ind = '1' THEN
        'Yes'
        WHEN progmap.program_ID IN ('AMC1931','AMCABD','AMCMNMI','AMC') AND sec.calwinexists_ind = '0' THEN
        'No'
        WHEN progmap.program_ID NOT IN ('AMC1931','AMCABD','AMCMNMI','AMC') THEN
        'N/A'
    END AS calwin_interface,
    sec.Client_name
FROM
    ref_programs prog (nolock)
LEFT OUTER JOIN ref_county_program_map progmap (nolock) ON progmap.program_id = prog.prog_id AND progmap.CLIENT_ID = prog.CLIENT_ID
INNER JOIN sec_clients sec (nolock) ON sec.client_id = progmap.Client_id
LEFT OUTER JOIN sec_organization secorg (nolock) ON CONVERT(VARCHAR,secorg.org_id) = CONVERT(VARCHAR,progmap.org_id)

Why not cast the number columns to varchar columns?

If you're using SQL SERVER you can do that like so:

CONVERT(VARCHAR,secorg.org_id) = CONVERT(VARCHAR,progmap.org_id)

You'll have to do an outer join for instances when the column that is both 'ALL' and numbers is 'All' as it won't be able to inner join to the other table.

For the quick fix based on your code above you can just change the second WHEN clause to look like so (again assuming you're using MS SQL SERVER):

WHEN CONVERT(VARCHAR,secorg.org_id) = CONVERT(VARCHAR,progmap.org_id) THEN secorg.org_name

Try this as your query:

SELECT DISTINCT 
    program_id, 
    prog_name,
    CASE Eitc_Active_Switch
        WHEN '1' THEN 'ON'
        ELSE 'OFF'
    END AS Prog_Status,
    progmap.client_id,
    ISNULL(secorg.org_name,'ALL') AS org_name,
    CASE prog.has_post_calc_screen
        WHEN 'True' THEN 'True'
        ELSE 'False'
    END AS Referal_ID,
    CASE WHEN progmap.program_ID IN ('AMC1931','AMCABD','AMCMNMI','AMC') AND sec.calwinexists_ind = '1' THEN
        'Yes'
        WHEN progmap.program_ID IN ('AMC1931','AMCABD','AMCMNMI','AMC') AND sec.calwinexists_ind = '0' THEN
        'No'
        WHEN progmap.program_ID NOT IN ('AMC1931','AMCABD','AMCMNMI','AMC') THEN
        'N/A'
    END AS calwin_interface,
    sec.Client_name
FROM
    ref_programs prog (nolock)
LEFT OUTER JOIN ref_county_program_map progmap (nolock) ON progmap.program_id = prog.prog_id AND progmap.CLIENT_ID = prog.CLIENT_ID
INNER JOIN sec_clients sec (nolock) ON sec.client_id = progmap.Client_id
LEFT OUTER JOIN sec_organization secorg (nolock) ON CONVERT(VARCHAR,secorg.org_id) = CONVERT(VARCHAR,progmap.org_id)
故人爱我别走 2024-11-09 22:55:32

case 语句很好,它的字段别名很糟糕,它应该是

     END As org_name

像 secorg.org_name 这样的多部分别名不起作用

The case statement is fine its the field alias thats bad it shoud be

     END As org_name

A multipart alias like secorg.org_name won't work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文