年月,   工号,工资
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谢谢!

解决方案 »

  1.   

    select min(年月) as 年月,工号,工资 from 表 group by 工号,工资
      

  2.   


    年月,   工号,工资
    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
    工号,
    工资
      

  3.   

    select min(年月) as 年月,工号,工资 from 表 group by 工号,工资
      

  4.   


    还有个请求,能否把以上数据放横,即:
    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
      

  5.   

    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 1200001,200601,900,001,200605,1000,001,200607,1200
    */