SQL Select Columns .. IF NULL然后选择其他列

时间:2016-04-19 09:49:51

标签: sql tsql select

我有这样的观点:

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|

有人可以帮我这个吗?非常感谢帮助!

3 个答案:

答案 0 :(得分:3)

如果您想逐列比较,请使用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;

答案 1 :(得分:0)

好吧,我已经在[方括号]中包装了所有列名,因为你使用了保留名称(Key,Text,Type),我喜欢一致性,所以尽快打破这个习惯。

如果你的标准是所有三列(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

答案 2 :(得分:0)

尝试使用此功能(如果任何值为null,则连接将返回null)

--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或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