表如下名称 最新统计时间   剩余数量
苹果 2012-2-7       20
苹果 2012-2-5       50
苹果 2012-2-1       40
橘子 2012-2-2       0
橘子 2012-2-1       30
橘子 2012-1-31      10
香蕉 2011-12-1      8.
菠萝
...
想统计各个水果类型最新日期的剩余数量,得到如下数据苹果 20
橘子 0
香蕉 8
菠萝 5

解决方案 »

  1.   

    select *
    from tb t
    where 最新统计时间=(select max(最新统计时间)
                       from tb 
                       where 名称=t.名称)
    --or
    select *
    from tb t
    where not exists(select 1 from tb where 名称=t.名称 and 最新统计时间>t.最新统计时间)
    --更多方法搜去除重复记录
      

  2.   

    create table  tb
    (
    名称 nvarchar(20),
    最新统计时间 datetime,
    剩余数量 int 
    )insert into tb values('苹果' ,'2012-2-7', 20)
    insert into tb values('苹果' ,'2012-2-5', 50)
    insert into tb values('苹果' ,'2012-2-1', 40)
    insert into tb values('橘子' ,'2012-2-2', 0)
    insert into tb values('橘子' ,'2012-2-1', 30)
    insert into tb values('橘子' ,'2012-1-31', 10)
    insert into tb values('香蕉' ,'2011-12-1', 8)
    select * from tb  
    where not exists (select 1 from tb t where tb.名称 = t.名称 and tb.最新统计时间< t.最新统计时间)
    /*
    名称,最新统计时间,剩余数量
    苹果,2012-02-07 00:00:00.000,20
    橘子,2012-02-02 00:00:00.000,0
    香蕉,2011-12-01 00:00:00.000,8(3 行受影响)
      

  3.   

    select 名称,剩余数量 from tb t
    where 最新统计时间=(select max(最新统计时间) from tb where 名称=t.名称)
      

  4.   

    declare @t table(name nvarchar(10),time1 date,number int)
    insert into @t
    select '苹果' ,'2012-2-7', 20 union
    select '苹果' ,'2012-2-5', 50 union
    select '苹果' ,'2012-2-1', 40 union
    select '橘子' ,'2012-2-2', 0 union
    select '橘子' ,'2012-2-1', 30 union
    select '橘子' ,'2012-1-31', 10 union
    select '香蕉' ,'2011-12-1', 8 union
    select '菠萝','2011-12-1',5
    --select * from @t 
    ;with t
    as(
    select ID=ROW_NUMBER() over(partition by name order by time1 desc ) ,a.* from @t a)select name,number from t where ID=1菠萝 5
    橘子 0
    苹果 20
    香蕉 8
      

  5.   

    IF OBJECT_ID('tb') is not null 
    drop table tb
    go 
    create table  tb
    (
        名称    nvarchar(20),
        最新统计时间 datetime,
        剩余数量 int 
    )insert into tb values('苹果' ,'2012-2-7', 20)
    insert into tb values('苹果' ,'2012-2-5', 50)
    insert into tb values('苹果' ,'2012-2-1', 40)
    insert into tb values('橘子' ,'2012-2-2', 0)
    insert into tb values('橘子' ,'2012-2-1', 30)
    insert into tb values('橘子' ,'2012-1-31', 10)
    insert into tb values('香蕉' ,'2011-12-1', 8)
    --想统计各个水果类型最新日期的剩余数量,得到如下数据--苹果 20
    --橘子 0
    --香蕉 8
    --菠萝 5
    ;with ct
    as
    (select *,rn=ROW_NUMBER()over(partition by 名称 order by getdate()) from tb)
    select 名称,剩余数量 from ct where rn=1 名称                   剩余数量
    -------------------- -----------
    橘子                   0
    苹果                   20
    香蕉                   8
      

  6.   

    select 名称,剩余数量 from tb  t where 最新统计时间=(select MAX(最新统计时间) from tb where t.名称=名称)名称                   剩余数量
    -------------------- -----------
    香蕉                   8
    苹果                   20
    橘子                   0(3 行受影响)
      

  7.   

    create table  tb
    (
        名称    nvarchar(20),
        最新统计时间 datetime,
        剩余数量 int 
    )insert into tb values('苹果' ,'2012-2-7', 20)
    insert into tb values('苹果' ,'2012-2-5', 50)
    insert into tb values('苹果' ,'2012-2-1', 40)
    insert into tb values('橘子' ,'2012-2-2', 0)
    insert into tb values('橘子' ,'2012-2-1', 30)
    insert into tb values('橘子' ,'2012-1-31', 10)
    insert into tb values('香蕉' ,'2011-12-1', 8)
    select * from tb  
    where not exists (select 1 from tb t where tb.名称 = t.名称 and tb.最新统计时间< t.最新统计时间)
    /*
    名称,最新统计时间,剩余数量
    苹果,2012-02-07 00:00:00.000,20
    橘子,2012-02-02 00:00:00.000,0
    香蕉,2011-12-01 00:00:00.000,8
      

  8.   

    还是俺家叶子的看着顺眼,
    select 名称,剩余数量 from tb t
    where 最新统计时间=(select max(最新统计时间) from tb where 名称=t.名称)
      

  9.   

    create table #tb
    (
     id       int,
     name     varchar(20),
     date     datetime,
     num      int  
    )insert into #tb(id,name,date,num)
    select 1,'苹果','2012-02-07',20
    union all
    select 2,'苹果','2012-02-05',50
    union all
    select 3,'苹果','2012-02-01',40
    union all
    select 4,'橘子','2012-02-02',0
    union all
    select 5,'橘子','2012-02-01',30
    union all
    select 6,'橘子','2012-01-31',10
    union all
    select 7,'香蕉','2011-12-01',8select a.name,#tb.num from #tb,
    (select MAX(date) a,name from #tb
    group by name)a
    where a.name=#tb.name
    and a.a=#tb.date
      

  10.   

    select *
    from tb t
    where not exists(select 1 from tb where 名称=t.名称 and 最新统计时间>t.最新统计时间)
      这个有点看不懂,麻烦哪位好心人解释下,感谢!!
      

  11.   

    select *
    from tb t
    where not exists(select 1 from tb where 名称=t.名称 and 最新统计时间>t.最新统计时间)
       
       这个not exists 不太看得懂,麻烦好心人帮忙解释一下,感谢!!! 
      

  12.   

    其实就是说当 select 1 from tb where 名称=t.名称 and 最新统计时间>t.最新统计时间 
    存在数据的时候(即为1) 则 相当于select *from tb t where 1=2
    不存在数据的时候(没有数据存在)则 相当于select *from tb t where 1=1
      

  13.   

    /*
    ID 单别 单号 金额 赋值金额 赋值单号 检索条件
    01 1 1 50.00 50.00 1-1 NULL
    02 2 2 100.00 100.00 2-2 NULL
    */
    --楼主的条件不怎么明白/*
    表如下名称 最新统计时间 剩余数量
    苹果 2012-2-7 20
    苹果 2012-2-5 50
    苹果 2012-2-1 40
    橘子 2012-2-2 0
    橘子 2012-2-1 30
    橘子 2012-1-31 10
    香蕉 2011-12-1 8.
    菠萝想统计各个水果类型最新日期的剩余数量,得到如下数据苹果 20
    橘子 0
    香蕉 8
    菠萝 5
    */
    go
    if OBJECT_ID('tbl') is not null
    drop table tbl
    go
    create table tbl(
    name nvarchar(10),
    [time] date,
    number int
    )
    go
    insert into tbl
    select '苹果' ,'2012-2-7', 20 union
    select '苹果' ,'2012-2-5', 50 union
    select '苹果' ,'2012-2-1', 40 union
    select '橘子' ,'2012-2-2', 0 union
    select '橘子' ,'2012-2-1', 30 union
    select '橘子' ,'2012-1-31', 10 union
    select '香蕉' ,'2011-12-1', 8 union
    select '菠萝','2011-12-1',5;with T
    as
    (
    select * from tbl a
    where [time] =
    (select MAX([time]) from tbl b where a.name=b.name)
    )
    select *from T/*
    结果:
    name time number
    香蕉 2011-12-01 8
    苹果 2012-02-07 20
    橘子 2012-02-02 0
    菠萝 2011-12-01 5
    */
      

  14.   


    declare @T table 
    (
    名称 varchar(10),
    最新统计时间 datetime,
    剩余数量 int
    )insert into @T output inserted.*
    select '苹果','2012-2-7',20 union all
    select '苹果','2012-2-5',50 union all
    select '苹果','2012-2-1',40 union all
    select '橘子','2012-2-2',0 union all
    select '橘子','2012-2-1',30 union all
    select '橘子','2012-1-31',10 union all
    select '香蕉','2011-12-1',8 union all
    select '菠萝','2012-3-7',20select 名称,剩余数量,最新统计时间 from  @T t 
    where 最新统计时间=(select max(最新统计时间) from @T where 名称=t.名称)select 名称,剩余数量 from  @T as t 
    where not exists(select 1 from @T where  名称=t.名称 and 最新统计时间>t.最新统计时间)
    /*
    名称         最新统计时间                  剩余数量
    ---------- ----------------------- -----------
    苹果         2012-02-07 00:00:00.000 20
    苹果         2012-02-05 00:00:00.000 50
    苹果         2012-02-01 00:00:00.000 40
    橘子         2012-02-02 00:00:00.000 0
    橘子         2012-02-01 00:00:00.000 30
    橘子         2012-01-31 00:00:00.000 10
    香蕉         2011-12-01 00:00:00.000 8
    菠萝         2012-03-07 00:00:00.000 20
    名称         剩余数量        最新统计时间
    ---------- ----------- -----------------------
    香蕉         8           2011-12-01 00:00:00.000
    苹果         20          2012-02-07 00:00:00.000
    橘子         0           2012-02-02 00:00:00.000
    菠萝         20          2012-03-07 00:00:00.000
    名称         剩余数量
    ---------- -----------
    苹果         20
    橘子         0
    香蕉         8
    菠萝         20*/
      

  15.   

    好心人,太感谢了。我刚入门,连基本的都不太懂,想问下: 1 是从哪里出来的? 然后tb 是表名的话? 语句中的 t 又是啥意思??麻烦再指导一下 
      

  16.   

    create table t1(名称 varchar(10),最新统计时间 datetime, 剩余数量 int)
    insert t1
    select '苹果', '2012-2-7', 20 union all
    select '苹果', '2012-2-5', 50 union all
    select '苹果', '2012-2-1', 40 union all
    select '橘子', '2012-2-2', 0 union all
    select '橘子', '2012-2-1', 30 union all
    select '橘子', '2012-1-31', 10 union all
    select '香蕉', '2011-12-1', 8
    go
    select a.名称,a.剩余数量 from t1 a
    where not exists(select 1 from t1 where a.名称=名称 and a.最新统计时间<最新统计时间)
    go
    drop table t1
      

  17.   

    其实就是取得“最新统计时间”的最大值。not exists 后面的 条件(select 1 from tb where 名称=t.名称 and 最新统计时间>t.最新统计时间)如果返回空,则表示tb t的该最新统计时间是最大值
      

  18.   

    create table  SHUIGUO
    (
        name    nvarchar(20),
        newDate datetime,
        lastCount int 
    )insert into SHUIGUO values('苹果' ,'2012-2-7', 20)
    insert into SHUIGUO values('苹果' ,'2012-2-5', 50)
    insert into SHUIGUO values('苹果' ,'2012-2-1', 40)
    insert into SHUIGUO values('橘子' ,'2012-2-2', 0)
    insert into SHUIGUO values('橘子' ,'2012-2-1', 30)
    insert into SHUIGUO values('橘子' ,'2012-1-31', 10)
    insert into SHUIGUO values('香蕉' ,'2011-12-1', 8)SELECT aa.name ,aa.lastDate,bb.lastCount
    from 
    (
    select name,MAX(newDate)lastDate
    from SHUIGUO
    GROUP BY name
    )aa
    left join SHUIGUO bb on aa.name=bb.name and aa.lastDate=bb.newDate;
      

  19.   

    create table ttt(name nvarchar(10),time1 date,number int)
    insert into ttt
    select '苹果' ,'2012-2-7', 20 union
    select '苹果' ,'2012-2-5', 50 union
    select '苹果' ,'2012-2-1', 40 union
    select '橘子' ,'2012-2-2', 0 union
    select '橘子' ,'2012-2-1', 30 union
    select '橘子' ,'2012-1-31', 10 union
    select '香蕉' ,'2011-12-1', 8 union
    select '菠萝','2011-12-1',5select a.name, b.number from
    (select name,MAX(time1)[time] from ttt group by name) a 
    left join ttt b on a.name=b.name and a.time=b.time1
    结果为:
    name number
    菠萝 5
    橘子 0
    苹果 20
    香蕉 8
      

  20.   


    declare @tb table
    (
        名称    nvarchar(20),
        最新统计时间 datetime,
        剩余数量 int 
    )insert into @tb values('苹果' ,'2012-2-7', 20)
    insert into @tb values('苹果' ,'2012-2-5', 50)
    insert into @tb values('苹果' ,'2012-2-1', 40)
    insert into @tb values('橘子' ,'2012-2-2', 0)
    insert into @tb values('橘子' ,'2012-2-1', 30)
    insert into @tb values('橘子' ,'2012-1-31', 10)
    insert into @tb values('香蕉' ,'2011-12-1', 8)select [名称],[剩余数量]from @tb a where [最新统计时间]=(select  max([最新统计时间])  from @tb where [名称]=a.名称)