日期:2014-05-19  浏览次数:20468 次

如何取重复数据中的第一条.详细请看例子
年月,       工号,工资
200601,     001,900
200602,     001,900
200603,     001,900
200604,     001,900
200605,     001,1000
200606,     001,1000
200607,     001,1200

需求结果
200601,     001,900
200605,     001,1000
200607,     001,1200

还有个请求,能否把以上数据放横,即:
001,200601,900,200605,1000,200607,1200

谢谢!




------解决方案--------------------
select min(年月) as 年月,工号,工资 from 表 group by 工号,工资
------解决方案--------------------

年月, 工号,工资
200601, 001,900
200602, 001,900
200603, 001,900
200604, 001,900
200605, 001,1000
200606, 001,1000
200607, 001,1200

需求结果
200601, 001,900
200605, 001,1000
200607, 001,1200

--------

Select
Min(年月) As 年月,
工号,
工资
From
TableName
Group By
工号,
工资
------解决方案--------------------
select min(年月) as 年月,工号,工资 from 表 group by 工号,工资

------解决方案--------------------

还有个请求,能否把以上数据放横,即:
001,200601,900,200605,1000,200607,1200

--------

--如果只有這麼多數據

Declare @S Nvarchar(1000)
Select @S = ' '
Select @S = @S + ', ' + 工号 + ', ' + Min(年月) + ', ' + Rtrim(工资) From TableName Group By 工号,工资
Select @S = Stuff(@S, 1, 1, ' ')
Select @S
------解决方案--------------------
Create Table TEST
(年月 Varchar(10),
工号 Varchar(10),
工资 Int)
Insert TEST Select '200601 ', '001 ',900
Union All Select '200602 ', '001 ',900
Union All Select '200603 ', '001 ',900
Union All Select '200604 ', '001 ',900
Union All Select '200605 ', '001 ',1000
Union All Select '200606 ', '001 ',1000
Union All Select '200607 ', '001 ',1200
GO
Select
Min(年月) As 年月,
工号,
工资
From
TEST
Group By
工号,
工资

Declare @S Nvarchar(1000)
Select @S = ' '
Select @S = @S + ', ' + 工号 + ', ' + Min(年月) + ', ' + Rtrim(工资) From TEST Group By 工号,工资
Select @S = Stuff(@S, 1, 1, ' ')
Select @S
GO
Drop Table TEST
--Result
/*
年月 工号 工资
200601 001 900
200605 001 1000
200607 001 1200

001,200601,900,001,200605,1000,001,200607,1200
*/


我的异常网推荐解决方案:软件开发者薪资,http://www.aiyiweb.com/other/1391128.html