我正在尝试在SQL Server中对一些数据进行分组。但是不确定是否有可以执行此操作的查询。以下是原始数据
Type Number Date Result
Car 6664441111 Feb 22 2016 IVR Detected
Car 6664441111 Feb 22 2016 Answered
Lab 5552221111 Feb 22 2016 No Answer
Lab 5552221111 Feb 22 2016 Hangup
Lab 5552221111 Feb 22 2016 Answered
我想知道一个可以执行此操作的查询:
Type Number Date Result
Car 6664441111 Feb 22 2016 IVR Detected
6664441111 Feb 22 2016 Answered
Lab 5552221111 Feb 22 2016 No Answer
5552221111 Feb 22 2016 Hangup
5552221111 Feb 22 2016 Answered
我正在使用SQL Server。提前谢谢
答案 0 :(得分:1)
确认您的数据已经排序,并且不需要进行排序以使您的逻辑发生,您可以使用row_number
窗口函数:
select
CASE WHEN row_num = 1 THEN type END AS type, number, date, result
from (
select type, number, date, result, row_number() over (partition by type) AS row_num
from t
) x
如果您需要按照示例中的顺序显示数据,则需要在窗口函数中添加order by
。