表如下名称 最新统计时间 剩余数量
苹果 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
苹果 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
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.最新统计时间)
--更多方法搜去除重复记录
(
名称 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 行受影响)
where 最新统计时间=(select max(最新统计时间) from tb where 名称=t.名称)
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
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
-------------------- -----------
香蕉 8
苹果 20
橘子 0(3 行受影响)
(
名称 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
select 名称,剩余数量 from tb t
where 最新统计时间=(select max(最新统计时间) from tb where 名称=t.名称)
(
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
from tb t
where not exists(select 1 from tb where 名称=t.名称 and 最新统计时间>t.最新统计时间)
这个有点看不懂,麻烦哪位好心人解释下,感谢!!
from tb t
where not exists(select 1 from tb where 名称=t.名称 and 最新统计时间>t.最新统计时间)
这个not exists 不太看得懂,麻烦好心人帮忙解释一下,感谢!!!
存在数据的时候(即为1) 则 相当于select *from tb t where 1=2
不存在数据的时候(没有数据存在)则 相当于select *from tb t where 1=1
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
*/
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*/
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
(
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;
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
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.名称)