急!!!这样的SQL语句怎样写???

cybercobra 2000-08-28 03:33:00
各位大虾:
小弟碰到这样一个问题:现在有一个表,其中储存一段时间以来全部的销售记录,现在要作一个明细报表,要求记录每种商品每日的入库、销售记录以及当日结存,当日结存要求输出当日最后一次进、出库的结存,要求在存储过程中完成。
其他的都好办,但对于要找出当日最后一次进、出库的结存,小弟已经花了近一个星期的时间,也没有找到一个好办法,现在用的是作循环的办法,具体如下:
首先将所有需要的记录都写入一个临时表中,然后以日期为循环变量(从起始日期到终止),对每一天都查找当天最后的一条记录,然后对操作时间小于该条记录的当天记录的结存置为0,最后作选择的时候用SUM求得结存的和,则可得出其最后一条记录的结存。但这种方法显然效率太低。
请问各位大虾,用SQL语句实现该功能还有没有更好的办法?多谢,多谢!!!
...全文
318 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
cybercobra 2001-06-25
  • 打赏
  • 举报
回复
对不起,我已离开该项目,所以这个问题到现在才给分,实在很抱歉,不过非常感谢各位大虾的指点,非常非常感谢。
ycp2000 2000-09-25
  • 打赏
  • 举报
回复
喂?
tanghuan 2000-09-06
  • 打赏
  • 举报
回复
本期结存=期初结存+本期入库-本期出库+本期盘存盈亏
从你的描述看,本期盘存盈亏为0,上期结存为0

我给你一个算法(只做数量,金额同):

select 日期,品种,sum(数量) as 数量
into 日库存变化临时表
from (select 日期,品种,-数量 as 数量
from 销售表
union
select 日期,品种,数量
from 入库表
) temp
group by 日期,品种

select a.日期,a.品种,sum(b.数量) as 当日结存数量
/*如果有期初结存:sum(b.数量)+期初结存 as 当日结存数量 */
from 日库存变化临时表 a,
日库存变化临时表 b
where a.日期>=b.日期 and
a.品种=b.品种
group by a.日期,a.品种
guofupei 2000-09-06
  • 打赏
  • 举报
回复
want your answer ?
guofupei 2000-09-06
  • 打赏
  • 举报
回复
decode(assign(a.last_time-b.do_date),+,1,0,1,0)

change last sentences as below

decode(sign(a.last_time-b.do_date),1,1,0,1,0)
guofupei 2000-09-05
  • 打赏
  • 举报
回复
store_id means diffrent code for ur material
guofupei 2000-09-05
  • 打赏
  • 举报
回复
你的问题我想是这样;
结存数因为有历史性,所以你要用STORE PROCEDURE
SELECT 能完成你的要求!
下面的过程就是你的要求结果!!!????
这只是oracle 下的解法:
下面的过SQL 中用了DECODE 和ASSIGN ,请你查一下assign 的用法,我手边无资料
可能连ASSIGN 都不是。这个函数是判断一个数值的正负性的。返回值的表示也请查一
下;decode(assign(a.last_time-b.do_date),+,1,0,1,0) 这一段你可以根据两函
数的用法重写。
1。 我认为你的表结构如下:
main_table :
{do_date date ; //实时
store_id number(n,0) ;
inout number(n,0) // in means + ,out means -?
cost number(n,m),
}

2 。请CREATE A VIEW AS BELOW :

create view last_io_per_date as
select to_char(do_date,'yyyymmdd') as datestring,
store_id,
max(do_date) as last_time
from main_table
group by to_char(do_date,'yyyymmdd'),store_id ;

3. 你想要的:

select a.datestring,a.stor_id,
sum(b.inout*decode(assign(a.last_time-b.do_date),+,1,0,1,0))
as jcnum
from last_io_per_date a ,main_table b
where a.store_id=b.store_id
// and a.datestring between begin_day and end_day
group by a.datestring,a.store_id ;


the query is your wanted ?!!! I think so!!!
注意::
decode(assign(a.last_time-b.do_date),+,1,0,1,0) 这一段请你查一查书;
descible :: +,1 表示 a.last_time-b.do_date> 0, value=1
0,1 表示 a.last_time-b.do_date=0, value =1
0 表示 a.last_time-b.do_date<0, value = 0
Iwant 2000-09-04
  • 打赏
  • 举报
回复
其实关键是要找到每天最后的一笔数据,tirgger 不是个好东东,在业务繁忙时就会降低性能。
在 table 中增加一个 字段,用来记录。
不然在用 sql 不能汇中
cybercobra 2000-08-29
  • 打赏
  • 举报
回复
谢谢各位,不过我要实现的功能不是求得某一天的最后一次的结存,而是要一次性的把一段时间内的每一天的包括进、出库的数量、金额以及结存的数量、金额同时输出,上述各Select好像都无法实现。Trigger我没有试,因为我不太会用 :-)
guofupei 2000-08-28
  • 打赏
  • 举报
回复
select to_char(date,'yyyymmdd') as datestring,jc from my_table having greatest(date)
group by to_char(dte,'yyyymmdd')
guofupei 2000-08-28
  • 打赏
  • 举报
回复
select to_char(date,'yyyymmdd') as datestring,jc having greatest(date)
group by to_char(dte,'yyyymmdd')
guofupei 2000-08-28
  • 打赏
  • 举报
回复
你可以用trigger 来自动实现这一目标!
for each row
after insert or update
please replace ur 结存 with 结存(+/-)in/out number,
你只需要MAX(DATE), 结存既可!
huntout 2000-08-28
  • 打赏
  • 举报
回复
select * from [臨時表] where [操作時間] in (
select max([操作時間]) from [臨時表] group by
datepart(yyyy, [操作時間]),
datepart(mm, [操作時間]),
datepart(d, [操作時間])
)

34,575

社区成员

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

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