我有像这样的SQL查询结果
id | subId | code | name | response
1 | 1 | abc | xyz | "Whatever"
1 | 1 | abc | xyz | "Whatever2"
1 | 2 | def | qrt | "Whatever3"
1 | 2 | def | qrt | "Whatever4"
我需要显示前四列(id,subid,code,name)一次(基于id和subid),其中响应id和响应值就像它一样
id | subId | code | name | responsevalue
1 | 1 | abc | xyz | "Whatever"
| "Whatever2"
1 | 2 | def | qrt | "Whatever3"
| "Whatever4"
答案 0 :(得分:0)
使用ROW_NUMBER()获取组中记录的行数,然后使用CASE语句输出所有非第一条记录(行号<> 1)的NULL。
WITH T1 AS
(
SELECT T.*,
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY responseid ) as rn
FROM T
)
SELECT
CASE WHEN rn=1 THEN ID ELSE NULL END as ID,
CASE WHEN rn=1 THEN SubId ELSE NULL END as SubID,
CASE WHEN rn=1 THEN Code ELSE NULL END as Code,
CASE WHEN rn=1 THEN Name ELSE NULL END as Name,
responseId,
response_value
FROM T1
ORDER BY T1.id,rn