【问题标题】:SQL Select Columns.. IF NULL then Select other ColumnsSQL 选择列.. IF NULL 然后选择其他列
【发布时间】:2016-04-19 09:49:51
【问题描述】:

我有这样的看法:

ID| Key | Product | Item | Block | Source | Title | Text | Type | H1 | H2 | H3 |
-------------------------------------------------------------------------------
1 | 456 | abcd    | def  |  1    |  TP    | QWERT | YUIP | tgr  | A1 | A2 | A3 |
2 | 567 | fhrh    | klo  |  1    |  GT    | TREWQ | ITGF | trp  | A1 | A2 | A3 |
3 | 891 | ufheu   | yut  |  2    |  FR    | WERTY | MNBV | uip  |NULL|NULL|NULL|

我想将其中一些列导出到现有的空表中。我想选择前六列,然后选择其他列,例如从右到左的层次结构。

如果 H1、H2 和 H3 不为空,则它们应该出现在输出中,并且 Title、Text 和 Type 应该为 NULL(即使它们包含值)。

如果 H1、H2 和 H3 为 NULL,我希望标题、文本和类型出现在输出中。

应该是这样的:

ID| Key | Product | Item | Block | Source | Title | Text | Type | H1 | H2 | H3 |
-------------------------------------------------------------------------------
1 | 456 | abcd    | def  |  1    |  TP    | NULL  | NULL | NULL | A1 | A2 | A3 |
2 | 567 | fhrh    | klo  |  1    |  GT    | NULL  | NULL | NULL | A1 | A2 | A3 |
3 | 891 | ufheu   | yut  |  2    |  FR    | WERTY | MNBV | uip  |NULL|NULL|NULL|

谁能帮我解决这个问题?非常感谢您的帮助!

【问题讨论】:

  • 如果 H1 不为空但 H2 和 H3 为空怎么办?
  • 你的逻辑很难理解。例如,您想要六个额外的列还是只需要三个?当某些列为 NULL 但不是全部时,您想做什么?

标签: sql tsql select


【解决方案1】:

如果要逐列比较,请使用coalesce()

select ID, Key, Product, Item, Block, Source,
       (case when h1 is not null then null else title end) as title,
       (case when h2 is not null then null else text end) as text,
       (case when h3 is not null then null else type end) as type,
       coalesce(h1, title) as h1,
       coalesce(h2, text) as h2,
       coalesce(h3, type) as h3
from t;

但是,我不确定您是否同时指代所有三列:

select ID, Key, Product, Item, Block, Source,
       (case when h1 is null and h2 is null and h3 is null then title end) as title,
       (case when h1 is null and h2 is null and h3 is null then text end) as text,
       (case when h1 is null and h2 is null and h3 is null then type end) as type,
       (case when h1 is null and h2 is null and h3 is null then NULL else h1 end) as h1,
       (case when h1 is null and h2 is null and h3 is null then NULL else h2 end) as h2,
       (case when h1 is null and h2 is null and h3 is null then NULL else h3 end) as h3
from t;

【讨论】:

  • 是的,我同时需要所有列,除了列别名(标题、文本、类型)外,效果很好,但这没什么。非常感谢您的帮助!
【解决方案2】:

好的,我已将所有列名包含在 [方括号] 中,因为您使用的是保留名称(键、文本、类型)并且我喜欢一致性,值得尽快改掉这个习惯。

如果您的标准是所有三列(H1、H2、H3)都需要为 NULL,那么您会想要这样的东西;

SELECT [ID]
    ,[key]
    ,[Product]
    ,[Item]
    ,[Block]
    ,[Source]
    ,CASE 
        WHEN H1 IS NULL
            AND H2 IS NULL
            AND H3 IS NULL
            THEN [Title]
        ELSE NULL
        END AS [Title]
    ,CASE 
        WHEN H1 IS NULL
            AND H2 IS NULL
            AND H3 IS NULL
            THEN [Text]
        ELSE NULL
        END AS [Text]
    ,CASE 
        WHEN H1 IS NULL
            AND H2 IS NULL
            AND H3 IS NULL
            THEN [Type]
        ELSE NULL
        END AS [Type]
    ,H1
    ,H2
    ,H3
FROM DataTable

【讨论】:

    【解决方案3】:

    试试这个(如果任何值为空,连接将返回空)

    --When H1,H2 and H3 Null
    Declare  @H1 AS Varchar(50)=NULL
            ,@H2 AS Varchar(50)=NULL
            ,@H3 AS Varchar(50)=NULL
            ,@Title AS Varchar(50)='Title'
            ,@Text  AS Varchar(50)='Text'
            ,@Type  AS Varchar(50)='Type'
    
    SELECT ISNULL(@H1+@H2+@H3,@Title) Title
           ,ISNULL(@H1+@H2+@H3,@Text) [Text]
           ,ISNULL(@H1+@H2+@H3,@Type) [Type]
           ,@H1 H1
           ,@H2 H2
           ,@H3 H3
    

    当H1,H2 OR H3有值时

    Declare  @H1 AS Varchar(50)='H1'
            ,@H2 AS Varchar(50)='H2'
            ,@H3 AS Varchar(50)='H3'
            ,@Title AS Varchar(50)='Title'
            ,@Text  AS Varchar(50)='Text'
            ,@Type  AS Varchar(50)='Type'
    
    SELECT  CASE ISNULL(@H1+@H2+@H3,'') WHEN '' THEN @Title ELSE Null END Title
           ,CASE ISNULL(@H1+@H2+@H3,'') WHEN '' THEN @Text ELSE Null END [Text] 
           ,CASE ISNULL(@H1+@H2+@H3,'') WHEN '' THEN @Type ELSE Null END [Type]
           ,@H1 H1
           ,@H2 H2
           ,@H3 H3                  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-05
      • 2023-03-17
      • 2021-11-12
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多