我是SQL服务器的新手,我想在单行中组合多行,但我无法做到。任何人都可以帮助我吗?
输入:
Id |RED |BUY |BSW
------------------------------------
1328 NULL NULL 0.05
1328 NULL 0.06 NULL
1328 0.01 NULL NULL
1328 0.05 NULL NULL
1329 NULL NULL 0.05
1329 NULL 0.05 NULL
1329 0.05 NULL NULL
输出
Id |RED |BUY |BSW
------------------------------------
1328 0.01 0.06 0.05
1328 0.05 NUll NULL
1329 0.05 0.05 0.05
编辑数据以删除SUM()
冲突。
答案 0 :(得分:0)
试试这个
SELECT id,
Sum(Isnull(red, 0))
OVER (
partition BY id
ORDER BY id),
Sum(Isnull(buy, 0))
OVER (
partition BY id
ORDER BY id),
Sum(Isnull(bsw, 0))
OVER (
partition BY id
ORDER BY id)
FROM Table1
GROUP BY id
答案 1 :(得分:0)
首先将字段默认设置更改为零而不是null,它将提高处理速度,然后尝试此
SELECT id,
Sum((red, 0))
OVER (
partition BY id
ORDER BY id),
Sum((buy, 0))
OVER (
partition BY id
ORDER BY id),
Sum((bsw, 0))
OVER (
partition BY id
ORDER BY id)
GROUP BY id