select 商品代码,出货日期,count(*) from 出货表 where 出货日期 between '开始时间' and '结束时间' group by 商品代码,出货日期
解决方案 »
- 如何从本机把数据插入到另一台机器(数据结构中有一个是不同的)
- insert语句的基础问题
- 怎样给存储过程的参数负空值?
- 早上恢复了以前的一个数据库,但是里面的用户下面没了登录名,这样用这个用户是登录不了的,然后我就想把他删除再建个用户,但是也删除不
- 怎样设置可以让SQL Server 2000默认接受dd/mm/yyyy的日期格式?
- 求语句
- ACCESS问题
- 我在ORACLE中建了一个数据库,不过删除时要输入密码, 是怎么回事?密码应输入什么?
- 各位高手请问 --我用属于administrators组的用户登陆到win2000server 但无法使用windows用户验证方式登录到sqlserver2000
- 在SELECT A,B,C FROM TABLE1 WHERE 语句中当满足WHERE 条件时出一个查询结果若要求在不满足WHERE条件时也出查询结果该如何改写SQL语句
- 导出整个数据库,包括表,视图,存储过程,自定义函数,关系,主键,索引等等
- 谁能传MYSQL的安装程序 或者下载地址给我 我分给他
商品代码,
出货日期,
出货次数=count(*)
from
出货表
where
出货日期 between '开始时间' and '结束时间'
group by
商品代码,出货日期
(select 商品代码,出货日期,count(*) as num from 出货表 where 出货日期 between '开始时间' and '结束时间' group by 商品代码,出货日期) T group by 商品代码
where 出货日期 between 开始 and 结束
group by a.商品代码,出货时间
,@endDate datetime
select A.商品代码
,sum(出货次数) as '出货次数'from 库存表 A
left join (
select 商品代码
,货柜代码
,count(1) as '出货次数'
from 出货表
where 出货日期 between @startDate and @endDate
group by 商品代码,货柜代码
)B on A.商品代码=B.商品代码
and A.货柜代码=B.货柜代码
group by A.商品代码
order by A.商品代码
,@endDate datetime --结束时间
--查询
select A.商品代码
,isnull(sum(出货次数),0) as '出货次数'from 库存表 A
left join (
select 商品代码
,货柜代码
,count(1) as '出货次数'
from 出货表
where 出货日期 between @startDate and @endDate
group by 商品代码,货柜代码
)B on A.商品代码=B.商品代码
and A.货柜代码=B.货柜代码
group by A.商品代码
order by A.商品代码
比如已知商品代码和时间在“2005/01/27”、“2005/12/27”之间的数据,假如在这个时间内此商品没有出货,那么次数是零,查询的结果需要出货的商品代码,商品所在的货柜,出货量,库存量等信息
if exists(select 1 from sysobjects where id=object_id(N'p_getResult') and xtype=N'F'
drop function f_getResult
go
create function f_getResult(@goodid varchar(10),@stDate datetime,@enDate datetime)
returns table
as
return(select 商品代码=@goodid
,a.货柜代码
,a.出货次数=(select count(商品代码) from 出货表 b
where b.商品代码=@goodid
and b.货柜代码=a.货柜代码
and b.出货日期 between @stDate and @endDate)
,出货数量=(select sum(出货数量) from 出货表 b
where b.商品代码=@goodid
and b.货柜代码=a.货柜代码
and b.出货日期 between @stDate and @endDate)
,a.库存数量
from 库存表 a
where a.商品代码=@goodid
)
go
select a.商品代码,a.货柜代码,a.出货数量,b.库存数量 from
出货表 a left join 库存表 b on a.商品代码=b.商品代码 and a.货柜代码=b.货柜代码
where a.出货日期>=cast('20050127'as datetime) and a.出货日期<cast('20051228'as datetime)
--执行语句
select * from vShipment
select * from dbo.f_getResult('商品代码','2005/01/27','2005/12/27')
即可
from (select 商品代码,sum(库存数量) as 库存数量 from 库存表 group by 商品代码) 库存表
right join (select 商品代码,sum(出货数量) as 出货数量,sum(次数) as 次数 from (
select 商品代码,sum(出货数量) as 出货数量,1 as 次数 from 出货表 group by 商品代码,出货日期) 出货表
group by 商品代码) 出货表