brokerid zc             date
001      1000.00    20100401
001      1000.00    20100402
002      2000.00    20100403
002      2000.00    20100404需要的结果
brokerid zc             date
001      1000.00    20100401002      2000.00    20100403

解决方案 »

  1.   

    if object_id('tb') is not null drop table tb
    go
    create table tb
    (
     brokerid varchar(10),
     zc decimal(19,2),
     date datetime
    )
    insert into tb select '001',1000.00,'20100401'
    insert into tb select '001',1000.00,'20100402'
    insert into tb select '002',2000.00,'20100403'
    insert into tb select '002',2000.00,'20100404'
    goselect * from tb b 
    where not exists(select * from tb where brokerid=b.brokerid and date<b.date)
    brokerid   zc                                      date
    ---------- --------------------------------------- -----------------------
    001        1000.00                                 2010-04-01 00:00:00.000
    002        2000.00                                 2010-04-03 00:00:00.000(2 行受影响)
      

  2.   

    --1
    select * from tb t where 
    not exists
    (
    select 1 from tb where brokerid=t.brokerid and date<t.date
    )
    --2
    select * from tb t where date=
    (
    select min(date) from tb where brokerid=t.brokerid
    )
      

  3.   

    if object_id('tb') is not null drop table tb
    go
    create table tb
    (
     brokerid varchar(10),
     zc decimal(19,2),
     date datetime
    )
    insert into tb select '001',1000.00,'20100401'
    insert into tb select '001',1000.00,'20100402'
    insert into tb select '002',2000.00,'20100403'
    insert into tb select '002',2000.00,'20100404'
    goSELECT DISTINCT brokerid ,zc ,max(date)OVER(PARTITION BY brokerid )[mdate]
    FROM tb t brokerid   zc                                      mdate
    ---------- --------------------------------------- -----------------------
    001        1000.00                                 2010-04-02 00:00:00.000
    002        2000.00                                 2010-04-04 00:00:00.000(2 row(s) affected)
      

  4.   

    select * from t a 
          not exists (select 1 from t where a.brokerid=brokerid and date<a.date)
      

  5.   


    --inner join
    select a.* from t a inner join (select brokerid,zc,min(date) as mindate) b on a.brokerid=b.brokerid and a.date=b.mindate and a.zc=b.zc
      

  6.   

    select * from tb b 
    where not exists(select * from tb where brokerid=b.brokerid and date<b.date)
      

  7.   


    select * from tb
    where date%2=1
      

  8.   


    SELECT * FROM dbo.TB
    WHERE CAST(date AS int)%2=1
    --或者
    SELECT * FROM dbo.TB
    WHERE CAST(date AS int)%2=0