搞错了,是下面的内容:
表t1内容:
    ID   PROJECT VALUE   DATE
    0     1        30     2004-02-13
    1     1        34     2004-02-27
    2     2        43     2004-02-16
    3     2        23     2004-02-30
          。
          。 
          。
请问如何写一条语句把每月每个项目最大日期的记录挑出来。
我要的结果是:
    1     1        34     2004-02-27
    3     2        23     2004-02-30

解决方案 »

  1.   

    select a.* from t1 a inner join (select max(DATE) as DATE from t1 group by convert(char(7),DATE,120)) b on a.DATE = b.DATE order by a.ID
      

  2.   

    create table t1(ID  int, VALUE  int, DATE datetime)
    insert into t1 select    0,     30,     '2004-02-13'
    insert into t1 select    1,     34,     '2004-02-27'
    insert into t1 select    2,     43,     '2004-03-16'
    insert into t1 select    3,     23,     '2004-03-30'
    select * from t1 where date in(select max(date)as date from t1 group by convert(varchar(7),date,120))
    drop table t1ID          VALUE       DATE                                                   
    ----------- ----------- ------------------------------------------------------ 
    1           34          2004-02-27 00:00:00.000
    3           23          2004-03-30 00:00:00.000(所影响的行数为 2 行)
      

  3.   

    select * from tl 
    where DATE in 
    (
    select max(DATE)
    from tl 
    group by datepart(year,DATE),datepart(month,DATE)
    )
      

  4.   

    select * from t1 a where not exists 
                       (select 1 from t1 where 
                              convert(char(7),[DATE],120)=convert(char(7),a.[DATE],120)
                                    and [DATE]>a.[DATE] 
                                                 and id=a.id )
      

  5.   

    --咋变掉了
    select * from t1 a where not exists 
           (select 1 from t1 where 
                  convert(char(7),[DATE],120)=convert(char(7),a.[DATE],120)
                              and [DATE]>a.[DATE] 
                                                 and id=a.id )
      

  6.   

    --示例代码:create table tb1(id int ,value int,[date] datetime)
    insert into tb1 
    select     0,     30,     '004-02-13'union all
    select     1,     34,     '2004-02-27'union all
    select     2,     43,     '2004-03-16'union all
    select     3,     23,     '2004-03-30'
    select * into #1 from tb1
    alter table #1 add char_date char (7)
    go
    update #1 set char_date=convert(char(7),[date],120)
    select a.id,a.value,b.max_date from (select max_date=max(date) from #1 group by char_date)b left join tb1 a on a.date=b.max_date
    drop table #1,tb1/*
    id          value       max_date                                               
    ----------- ----------- ------------------------------------------------------ 
    1           34          2004-02-27 00:00:00.000
    3           23          2004-03-30 00:00:00.000(所影响的行数为 2 行)
    */
      

  7.   

    --用一句:
    create table tb1(id int ,value int,[date] datetime)
    insert into tb1 
    select     0,     30,     '004-02-13'union all
    select     1,     34,     '2004-02-27'union all
    select     2,     43,     '2004-03-16'union all
    select     3,     23,     '2004-03-30'
    select a.id,a.value,b.date from (select date=max(date) from tb1 group by convert(varchar(7),date,120))b left join tb1 a on a.date=b.date
    drop table tb1/*
    id          value       max_date                                               
    ----------- ----------- ------------------------------------------------------ 
    1           34          2004-02-27 00:00:00.000
    3           23          2004-03-30 00:00:00.000(所影响的行数为 2 行)
    */
      

  8.   

    select * from tl where CreateDate in (select max(CreateDate) from tl group by convert(varchar(7),CreateDate,120))
      

  9.   

    select * from t1 a inner join (select max(date)as date from t1 group by convert(varchar(6),date,112),PROJECT) b on a.date=b.date
      

  10.   

    少写了一点select * from t1 a inner join (select max(date)as date,project from t1 group by convert(varchar(6),date,112),PROJECT) b on a.date=b.date
      

  11.   

    楼主要的应该是这个吧declare @a table(id int,project int,value int,date datetime)
    insert into @a values (0,1,30,'2004-02-13')
    insert into @a values (1,1,34,'2004-02-27')
    insert into @a values (2,1,34,'2004-03-17')
    insert into @a values (3,2,43,'2004-02-16')
    insert into @a values (4,2,23,'2004-03-25')select * from @a where date in(select max(date) from @a group by project,Year(date),Month(date))显示结果
    id          project     value       date                                                   
    ----------- ----------- ----------- ------------------------------------------------------ 
    1           1           34          2004-02-27 00:00:00.000
    2           1           34          2004-03-17 00:00:00.000
    3           2           43          2004-02-16 00:00:00.000
    4           2           23          2004-03-25 00:00:00.000
    先按project分组,再按date分组
      

  12.   


    select * from t1 a where 
    (select count(*) from t1 where month(a.date)=month(date) and a.date<date)=0