将此输出提供给一个查询:
ID | Question | Answer
1 | 1 | 2
1 | 2 | 5
1 | 3 | 5
2 | 1 | 2
2 | 2 | 2
3 | 4 | 7
在sql中是否有任何方法可以使用:
ID | Question | Answer
1
| 1 | 2
| 2 | 5
| 3 | 5
2
| 1 | 2
| 2 | 2
3
| 4 | 7
目标是不要一遍又一遍地重复ID,但我没有什么可以分组的,因为我想单独显示所有结果。
我尝试过使用GROUP BY但到目前为止没有太多结果。 我正在使用SQL Server。
答案 0 :(得分:3)
这不是一个SQL问题 是客户端代码显示问题
示例,在Reporting Services或Crystal Reports中,您设置了ID分组
答案 1 :(得分:2)
你需要
示例:
select
-- Show the ID only if it is not a detail row
case when Question is null then ID else null end as ID,
Question,
Answer
From
(
-- Detail rows
select ID, Question, Answer from T1
union all
-- Heading rows
select distinct ID, cast(null as type) as Question, cast(null as type) as Answer from T1
)x
order by
-- In order of ID
x.ID,
-- but make heading rows come first
case when x.Question is null then 0 else 1 end asc,
-- then order by question
x.Question