日期:2014-05-17 浏览次数:20427 次
if OBJECT_ID('tempdb..#temp', 'u') is not null drop table #temp;
go
create table #temp( [课程编号] varchar(100), [星期] varchar(100), [移动电话] varchar(100), [姓名] varchar(100));
insert #temp
select '20001','1','13808140811','张三' union all
select '20001','2','13808140811','张三' union all
select '20003','1','13808140813','张三' union all
select '20004','3','13808140814','张四' union all
select '20004','5','13808140814','张四' union all
select '20004','1','13808140814','张四' union all
select '20005','1','13808140815','张五'
--SQL:
;WITH cte AS
(
select rowid=ROW_NUMBER() OVER(PARTITION BY [课程编号] ORDER BY 星期), * from #temp
)
SELECT * FROM cte
WHERE rowid = 1
--方法3
SELECT b.* FROM
(SELECT DISTINCT [课程编号] FROM #temp) a
CROSS APPLY
(SELECT TOP(1) * FROM #temp m WHERE m.[课程编号] =a.[课程编号] ORDER BY 星期) b
create table #tb
(课程编号 varchar(10), 星期 int, 移动电话 varchar(20), 姓名 varchar(10))
insert into #tb
select '20001', 1, '13808140811', '张三' union all
select '20001', 2, '13808140811', '张三' union all
select '20003', 1, '13808140813', '张三' union all
select '20004', 3, '13808140814', '张四' union all