怎样统计这个数据

cupbook 2006-05-02 11:27:33
一表如下
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语句怎写,谢谢
...全文
124 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
rangwoxiao 2006-05-03
  • 打赏
  • 举报
回复
select sum(quantity) from tablename where date in (select max(date) from tablename where date<=(指定日期)),这样子试试。
cupbook 2006-05-03
  • 打赏
  • 举报
回复
谢谢楼上
我想你的方法是得不到我想要的结果

准确地说
我输入一日期
就要得到所有物品在该日期最后一次录入的数据相加值,


cupbook 2006-05-03
  • 打赏
  • 举报
回复
谢谢楼上的各位
我终于搞掂了
select quantity from Test where pkid in (select max(pkid) from Test where indate<='2001-1-25' group by name )
wenjie337 2006-05-03
  • 打赏
  • 举报
回复
select sum(quantity) as 该阶段总和, date as 日期 from [table] where samlldatetime(date) > 2005-01-01 and smalldatetime(date) < 2005-01-03
我想大概是这样吧,不过具体日期的转换格式可能错了,记不太清了
lxzm1001 2006-05-03
  • 打赏
  • 举报
回复
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'

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
zlp321002 2006-05-03
  • 打赏
  • 举报
回复
--测试环境:
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)
*/
rangwoxiao 2006-05-02
  • 打赏
  • 举报
回复
select sum(quantity) from tablename where date<(这里是指定的日期),这个语句是把指定日期之前的所有值相加。
select sum(quantity) from tablename where date between (指定的日期1)and(指定的日期1),这个语句是把指定日期之间的所有值相加。

34,873

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧