一表如下
PKID Name quantity Date
1 A 10 2005-01-01
2 B 5 2005-01-01
3 A 12 2005-01-05
4 B 6 2005-01-05
5 A 14 2005-01-12
6 A 15 2005-01-18
7 B 17 2005-01-18
此表表示一个生产进度表
我要统计2005-01-03以前的数量
即10+5我要统计2005-01-06以前的数量
即12+6我要统计2005-01-13以前的数量
即14+6
sql语句怎写,谢谢
PKID Name quantity Date
1 A 10 2005-01-01
2 B 5 2005-01-01
3 A 12 2005-01-05
4 B 6 2005-01-05
5 A 14 2005-01-12
6 A 15 2005-01-18
7 B 17 2005-01-18
此表表示一个生产进度表
我要统计2005-01-03以前的数量
即10+5我要统计2005-01-06以前的数量
即12+6我要统计2005-01-13以前的数量
即14+6
sql语句怎写,谢谢
解决方案 »
- 这个谁帮我解释下 为什么两个的结果不一样
- 关于sql查询,再次提问
- sql 查询 取3位小数
- 无法安装sql server2000企业版!
- 这样的合计怎么做?
- 关于数据库的安装问题,请高手赐教,我在线等候回音。
- 通过端口 1433 连接到主机 127.0.0.1 的 TCP/IP 连接失败。错误:“Connection refused: connect。
- 使用存储过程中,如何将一个表中的记录通循环一条一条地增加到另一个表中?
- 存储过程事务ROLLBACK失败的问题。
- 一个vfp的问题,在线守候,请大侠指点
- 求助-数据库SQL语句中查找操作的文法
- 50分跪求高手或版主推荐几款大家常用的数据库表设计工具!
select sum(quantity) from tablename where date between (指定的日期1)and(指定的日期1),这个语句是把指定日期之间的所有值相加。
declare @t table(RKID int identity(1,1),Name varchar(10),Quantity int ,date datetime)
insert into @t select 'A',10,'2005-01-01'
union all select 'B',5,'2005-01-01'
union all select 'A',12,'2005-01-05'
union all select 'B',6,'2005-01-05'
union all select 'A',14,'2005-01-12'
union all select 'A',15,'2005-01-18'
union all select 'B',17,'2005-01-18'
--测试语句:
--2005-01-03
select sum (t.Quantity) from
(select top 2 Quantity from @t a where date<='2005-01-3' order by RKID desc ) T
--2005-01-06
select sum (t.Quantity) from
(select top 2 Quantity from @t a where date<='2005-01-6' order by RKID desc ) T
--2005-01-13
select sum (t.Quantity) from
(select top 2 Quantity from @t a where date<='2005-01-13' order by RKID desc ) T--结果
/*
-----------
15(1 row(s) affected)
-----------
18(1 row(s) affected)
-----------
20(1 row(s) affected)
*/
insert into @t select 'A',10,'2005-01-01'
union all select 'B',5,'2005-01-01'
union all select 'A',12,'2005-01-05'
union all select 'B',6,'2005-01-05'
union all select 'A',14,'2005-01-12'
union all select 'A',15,'2005-01-18'
union all select 'B',17,'2005-01-18'select sum(x.quantity) from (select name,max(date) date from @t where date<'2005-01-13' group by name) t,
@t x where t.name=x.name and t.date=x.date
我想大概是这样吧,不过具体日期的转换格式可能错了,记不太清了
我终于搞掂了
select quantity from Test where pkid in (select max(pkid) from Test where indate<='2001-1-25' group by name )