T-SQL将多行组合成单行

时间:2010-12-16 06:05:29

标签: sql sql-server tsql sql-server-2008 aggregate-functions

我有这个T-SQL(简化):

select 3.00 as score1, null as score2, null as score3, null as score4
union all
select null as score1, 4.5 as score2, 1.5 as score3, null as score4

产生这个:

score1  score2  score3  score4
------------------------------
3.00    NULL    NULL    NULL
NULL    4.5     1.5     NULL

但我想将它加入一行,如下所示:

score1  score2  score3  score4
------------------------------
3.00    4.5     1.5     NULL

抱歉 - 我画的是空白(当天晚些时候)。

我需要临时桌吗?

我这样做的原因是我有以下关联:

评论1 .. *分数

因此,常规联接为每个分数生成1行 - 但我想插入一条记录(到另一个表中),每条记录都有一列 - 如果你知道我的意思:

INSERT INTO OtherTable (ReviewId, Score1, Score2, Score3, Score4)
????

希望这是有道理的。

修改

根据@OMG Ponies的答案(他刚刚删除),我想出了这个:

SELECT CASE MAX(x.score1) WHEN 0 THEN NULL ELSE MAX(x.score1) END AS score4
       CASE MAX(x.score2) WHEN 0 THEN NULL ELSE MAX(x.score2) END AS score4
       CASE MAX(x.score3) WHEN 0 THEN NULL ELSE MAX(x.score3) END AS score4
       CASE MAX(x.score4) WHEN 0 THEN NULL ELSE MAX(x.score4) END AS score4
  FROM (select 3.00 as score1, 0 as score2, 0 as score3, 0 as score4
        union all
        select 0 as score1, 4.5 as score2, 1.5 as score3, 0 as score4) x

但它非常难看。还有其他想法吗?

4 个答案:

答案 0 :(得分:0)

假设两行中只有一行的值为score1,score2,......否则使用NULLIF函数重写查询。

SELECT
    NULLIF(ISNULL(NULLIF(x.score1, 0), y.score1), 0) score1,
    NULLIF(ISNULL(NULLIF(x.score2, 0), y.score2), 0) score2,
    NULLIF(ISNULL(NULLIF(x.score3, 0), y.score3), 0) score3,
    NULLIF(ISNULL(NULLIF(x.score4, 0), y.score4), 0) score4
FROM (SELECT 3.00 AS score1, 0 AS score2, 0 AS score3, 0 AS score4) x
CROSS JOIN (SELECT 0 AS score1, 4.5 AS score2, 1.5 AS score3, 0 AS score4) y

答案 1 :(得分:0)

当我使用简单的MAX()尝试它(SQL 2008)时,似乎可行:

SELECT 
    MAX(score1),
    MAX(score2),
    MAX(score3),
    MAX(score4)
FROM
    (select 3.00 as score1, null as score2, null as score3, null as score4 
    union all 
    select null as score1, 4.5 as score2, 1.5 as score3, null as score4) s

我跑了这个测试

declare @t table (i int null)
insert @t values (null)
insert @t values (1)
select MAX(i) from @t

这是你想要做的吗?

答案 2 :(得分:0)

@OMG Ponies / RPM1984查询似乎有效:

SELECT MAX(x.score1),
       MAX(x.score2),
       MAX(x.score3),
       MAX(x.score4)
  FROM (select 3.00 as score1, null as score2, null as score3, CAST(null as int) as score4
        union all
        select null as score1, 4.5 as score2, 1.5 as score3, null as score4) x

结果是:

3.00    4.5 1.5 NULL

如果多行定义相同的分数,我不确定你会怎么做。


我必须将CAST(null as int)添加到第四列,因为否则SQL Server无法计算出第四列的类型 - 它只有两个空值,并且null可以是任何类型

答案 3 :(得分:0)

结束我的原始查询(感谢@OMG小马让我走上正轨):

SELECT CASE MAX(x.score1) WHEN 0 THEN NULL ELSE MAX(x.score1) END AS score4
       CASE MAX(x.score2) WHEN 0 THEN NULL ELSE MAX(x.score2) END AS score4
       CASE MAX(x.score3) WHEN 0 THEN NULL ELSE MAX(x.score3) END AS score4
       CASE MAX(x.score4) WHEN 0 THEN NULL ELSE MAX(x.score4) END AS score4
  FROM (select 3.00 as score1, 0 as score2, 0 as score3, 0 as score4
        union all
        select 0 as score1, 4.5 as score2, 1.5 as score3, 0 as score4) x