求一条SQL语句,谢谢

cnidb 2009-02-08 06:27:34
表1 (进货日志)
ID,
PID,商品ID
PNAME,品名
PNUM,数量
PTIME,时间

表2 (销售日志)
Buy_ID,
Buy_PID,商品ID
Buy_PNAME,品名
Buy_PNUM,销售数量
Buy_PTIME,销售时间

查询条件:WHERE 表1.PTIME BETWEEN '2009-02-07 08:00:00' AND '2009-02-08 07:59:59'

希望显示的结果包括:前一天的结余,当天的进货量,当天的销售量
例:
------------------------------------------------
品名 | 前一天结余 | 当天进货 | 当天销售 |
------------------------------------------------
商品a | 5 | 6 | 8 |
------------------------------------------------
商品b | 1 | 9 | 7 |




...全文
266 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
y_dong119 2009-02-15
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 cnidb 的回复:]
楼主基本能实现,谢谢!

但是有一个问题,当销售量为0,或者进货量为0时,显示的数据不全;

[/Quote]

看了下问题,应该不止楼主说的问题.

库存的结余=进货日志(<当前时间)-销售日志(<当前时间)

而上面的实例明显没有这样判断.
cnidb 2009-02-14
  • 打赏
  • 举报
回复
当销售量为0,或者进货量为0时,显示的数据不全;
cnidb 2009-02-11
  • 打赏
  • 举报
回复
.......
from @a cc
join @b dd on cc.PID=dd.Buy_PID and cc.PNAME=dd.Buy_PNAME 估计是这里出现了问题
......
claro 2009-02-11
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 cnidb 的回复:]
10楼的方法基本能实现,但当销售量为0,或者进货量为0时,显示的数据完整;
[/Quote]

基本是那样实现的。因为没有乘除关系,建议用ISNULL(?,0)方式实现。
kenny_appleso 2009-02-11
  • 打赏
  • 举报
回复
declare @a table (ID int IDENTITY,PID int,PNAME varchar(20),PNUM int,PTIME datetime)
insert @a
select 100001,'AA',100,getdate()-1 Union ALL
select 100002,'AB',100,getdate()-1 Union ALL
select 100001,'AA',200,getdate()-2 Union ALL
select 100002,'AB',200,getdate()-2 Union ALL
select 100001,'AA',300,getdate()-3 Union ALL
select 100002,'AB',300,getdate()-3 Union ALL
select 100001,'AA',400,getdate()-4 Union ALL
select 100002,'AB',400,getdate()-4 Union ALL
select 100001,'AA',500,getdate()-5 Union ALL
select 100002,'AB',500,getdate()-5

declare @b table (Buy_ID int IDENTITY,Buy_PID int,Buy_PNAME varchar(20),Buy_PNUM int,Buy_PTIME datetime)
insert @b
select 100001,'AA',10,getdate()-1 Union ALL
select 100002,'AB',10,getdate()-1 Union ALL
select 100001,'AA',20,getdate()-2 Union ALL
select 100002,'AB',20,getdate()-2 Union ALL
select 100001,'AA',30,getdate()-3 Union ALL
select 100002,'AB',30,getdate()-3 Union ALL
select 100001,'AA',40,getdate()-4 Union ALL
select 100002,'AB',40,getdate()-4 Union ALL
select 100001,'AA',50,getdate()-5 Union ALL
select 100002,'AB',50,getdate()-5

declare @z1 datetime
declare @z2 datetime
set @z1='2009-02-07 08:00:00'
set @z2='2009-02-08 07:59:59'

select * from @a
select * from @b
select a.PNAME 品名,
ISNULL(sum(e.zt),0) 前一天结余,
sum(a.PNUM) 当天进货,sum(b.Buy_PNUM) 当天销售
from @a a
join @b b
on a.PID=b.Buy_PID and a.PNAME=b.Buy_PNAME
left join (
select PID,PNAME,sum(cc.PNUM)-sum(dd.Buy_PNUM) zt
from @a cc
join @b dd on cc.PID=dd.Buy_PID and cc.PNAME=dd.Buy_PNAME
where (cc.PTIME BETWEEN @z1-1 AND @z2-1) and (dd.Buy_PTIME BETWEEN @z1-1 AND @z2-1)
group by cc.PID,cc.PNAME
) e
on a.PID=e.PID and a.PNAME=e.PNAME
where (a.PTIME BETWEEN @z1 AND @z2) and (b.Buy_PTIME BETWEEN @z1 AND @z2)
group by a.PNAME

/****
/*
--表1
ID PID PNAME PNUM PTIME
3 100001 AA 200 2009-02-07 12:40:05.793
4 100002 AB 200 2009-02-07 12:40:05.793
5 100001 AA 300 2009-02-06 12:40:05.793
6 100002 AB 300 2009-02-06 12:40:05.793

--表2
Buy_ID Buy_PID Buy_PNAME Buy_PNUM Buy_PTIME
3 100001 AA 20 2009-02-07 12:40:05.793
4 100002 AB 20 2009-02-07 12:40:05.793
5 100001 AA 30 2009-02-06 12:40:05.793
6 100002 AB 30 2009-02-06 12:40:05.793

--结果
品名 前一天结余 当天进货 当天销售
AA 270 200 20
AB 270 200 20
*/
kkk33181102 2009-02-11
  • 打赏
  • 举报
回复
你这个表结余是出不来的,只是还有有期初
结余=期初+进货数量-销售数量
cnidb 2009-02-10
  • 打赏
  • 举报
回复
10楼的方法基本能实现,但当销售量为0,或者进货量为0时,显示的数据完整;
cnidb 2009-02-10
  • 打赏
  • 举报
回复
楼上的方法无法统计"前一天的结余"
yygyogfny 2009-02-10
  • 打赏
  • 举报
回复
学习
claro 2009-02-09
  • 打赏
  • 举报
回复


declare @a table (ID int IDENTITY,PID int,PNAME varchar(20),PNUM int,PTIME datetime)
insert @a
select 100001,'AA',100,getdate()-1 Union ALL
select 100002,'AB',100,getdate()-1 Union ALL
select 100001,'AA',200,getdate()-2 Union ALL
select 100002,'AB',200,getdate()-2 Union ALL
select 100001,'AA',300,getdate()-3 Union ALL
select 100002,'AB',300,getdate()-3 Union ALL
select 100001,'AA',400,getdate()-4 Union ALL
select 100002,'AB',400,getdate()-4 Union ALL
select 100001,'AA',500,getdate()-5 Union ALL
select 100002,'AB',500,getdate()-5

declare @b table (Buy_ID int IDENTITY,Buy_PID int,Buy_PNAME varchar(20),Buy_PNUM int,Buy_PTIME datetime)
insert @b
select 100001,'AA',10,getdate()-1 Union ALL
select 100002,'AB',10,getdate()-1 Union ALL
select 100001,'AA',20,getdate()-2 Union ALL
select 100002,'AB',20,getdate()-2 Union ALL
select 100001,'AA',30,getdate()-3 Union ALL
select 100002,'AB',30,getdate()-3 Union ALL
select 100001,'AA',40,getdate()-4 Union ALL
select 100002,'AB',40,getdate()-4 Union ALL
select 100001,'AA',50,getdate()-5 Union ALL
select 100002,'AB',50,getdate()-5

declare @z1 datetime
declare @z2 datetime
set @z1='2009-02-07 08:00:00'
set @z2='2009-02-08 07:59:59'

select * from @a
select * from @b
select a.PNAME 品名,
ISNULL(sum(e.zt),0) 前一天结余,
sum(a.PNUM) 当天进货,sum(b.Buy_PNUM) 当天销售
from @a a
join @b b
on a.PID=b.Buy_PID and a.PNAME=b.Buy_PNAME
left join (
select PID,PNAME,sum(cc.PNUM)-sum(dd.Buy_PNUM) zt
from @a cc
join @b dd on cc.PID=dd.Buy_PID and cc.PNAME=dd.Buy_PNAME
where (cc.PTIME BETWEEN @z1-1 AND @z2-1) and (dd.Buy_PTIME BETWEEN @z1-1 AND @z2-1)
group by cc.PID,cc.PNAME
) e
on a.PID=e.PID and a.PNAME=e.PNAME
where (a.PTIME BETWEEN @z1 AND @z2) and (b.Buy_PTIME BETWEEN @z1 AND @z2)
group by a.PNAME

/****
/*
--表1
ID PID PNAME PNUM PTIME
3 100001 AA 200 2009-02-07 12:40:05.793
4 100002 AB 200 2009-02-07 12:40:05.793
5 100001 AA 300 2009-02-06 12:40:05.793
6 100002 AB 300 2009-02-06 12:40:05.793

--表2
Buy_ID Buy_PID Buy_PNAME Buy_PNUM Buy_PTIME
3 100001 AA 20 2009-02-07 12:40:05.793
4 100002 AB 20 2009-02-07 12:40:05.793
5 100001 AA 30 2009-02-06 12:40:05.793
6 100002 AB 30 2009-02-06 12:40:05.793

--结果
品名 前一天结余 当天进货 当天销售
AA 270 200 20
AB 270 200 20
*/

悔说话的哑巴 2009-02-09
  • 打赏
  • 举报
回复
我正要写这样的。。。。关注。。。。
csjtxy 2009-02-09
  • 打赏
  • 举报
回复
select PName as 品名,
ISNULL(表1.PNUM-表2.Buy_PNUM,0) 前一天的结余,
PNUM as 当天的进货量,
Buy_PNUM as 当天的销售量
from 表1 inner join
表2 on 表1.PID=表2.Buy_PID
where 表1.PTIME Between '2009-02-07 08:00:00' AND '2009-02-08 07:59:59'
cnidb 2009-02-09
  • 打赏
  • 举报
回复
更正:10楼 基本能实现,谢谢!
cnidb 2009-02-09
  • 打赏
  • 举报
回复
楼主基本能实现,谢谢!

但是有一个问题,当销售量为0,或者进货量为0时,显示的数据不全;
cnidb 2009-02-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 cnidb 的回复:]
引用 4 楼 dawugui 的回复:
你的时间跨天,表二的时间又怎么算?
前一天什么概念?'2009-02-06 08:00:00' AND '2009-02-07 07:59:59'?
[/Quote]
不好意思,上面错了。
当天数据:'2009-02-07 08:00:00' AND '2009-02-08 07:59:59'
前一天即:日期 < '2009-02-07 08:00:00' 的数据;

表二的时间和表一对应
ws_hgo 2009-02-08
  • 打赏
  • 举报
回复
关注...
cnidb 2009-02-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 dawugui 的回复:]
你的时间跨天,表二的时间又怎么算?
前一天什么概念?'2009-02-06 08:00:00' AND '2009-02-07 07:59:59'?

请给出表结构,测试数据,相关算法和需要的结果.谢谢!
[/Quote]
当天数据:'2009-02-07 08:00:00' AND '2009-02-08 07:59:59'
前一天即:'2009-02-06 08:00:00' AND '2009-02-07 07:59:59'

表结构:

CREATE TABLE [表1] (
[ID] [int] IDENTITY (1, 1) NOT NULL,
[PID] [int] NOT NULL,
[PName] [nvarchar] (50) NOT NULL,
[PNum] [int] NOT NULL),
[PTime] [datetime] NOT NULL DEFAULT (getdate())

CREATE TABLE [表2] (
[Buy_ID] [int] IDENTITY (1, 1) NOT NULL,
[Buy_PID] [int] NOT NULL,
[Buy_PName] [nvarchar] (250) NULL,
[Buy_PNum] [int] NOT NULL,
[Buy_PTime] [datetime] NOT NULL DEFAULT (getdate())

claro 2009-02-08
  • 打赏
  • 举报
回复
用循环给他写一个吧,估计也没什么什么概念的.
否则嵌套一个表给定前一天时间就可以了.
dawugui 2009-02-08
  • 打赏
  • 举报
回复
[Quote=引用楼主 cnidb 的帖子:]
表1 (进货日志)
ID,
PID,商品ID
PNAME,品名
PNUM,数量
PTIME,时间

表2 (销售日志)
Buy_ID,
Buy_PID,商品ID
Buy_PNAME,品名
Buy_PNUM,销售数量
Buy_PTIME,销售时间

查询条件:WHERE 表1.PTIME BETWEEN '2009-02-07 08:00:00' AND '2009-02-08 07:59:59'

希望显示的结果包括:前一天的结余,当天的进货量,当天的销售量
例:
------------------------------------------------
品名 | 前一天结余 | 当天进货 | 当天销售 |
------------------------------------------------
商品a | 5 | 6 | 8 |
------------------------------------------------
商品b | 1 | 9 | 7 |

[/Quote]

你的时间跨天,表二的时间又怎么算?
前一天什么概念?'2009-02-06 08:00:00' AND '2009-02-07 07:59:59'?


请给出表结构,测试数据,相关算法和需要的结果.谢谢!
肥龙上天 2009-02-08
  • 打赏
  • 举报
回复

给的数据少了点,理解也知道这里了
select 前一天的结余 = ((select sum(pnum) from 表1 where ptime < convert(nvarchar(11),getdate(),120) and pid = a.pid)
- (select sum(pnum) from 表2 where ptime < convert(nvarchar(11),getdate(),120) and buy_pid = a.pid))
,当天进货 = (select sum(pnum) from 表1 where pid = a.pid and convert(nvarchar(11),ptime ,120) = a.convert(nvarchar(11),ptime ,120))
,当天销售 = (select sum(pnum) from 表2 where buy_pid = a.pid and convert(nvarchar(11),buy_ptime ,120) = a.convert(nvarchar(11),ptime ,120))
select convert(nvarchar(11),getdate(),120)
加载更多回复(2)

34,590

社区成员

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

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