又遇Sql查询难题!

wumylove1234 2005-03-14 11:32:53
商品类型表(goodtype)
id parent_id name
1 null 刃具类
2 1 钻头
商品表
id goodtype_id name
1 2 直柄钻头

初始库存表(HisStore)
goodsid goodnum
1 10
入库主表(InM)
in_id rec_date
01 2004-01-01
入库明细表(InD)
in_id goodsid goodsnum
01 1 10

其中商品类型表级别为两级,即:Parent_id为不为Null的可以是商品表中的Goodtype_id
现在要求出某段时间的,商品类型的增加情况,也就是说,打个比方,期初时库存中钻头类有10种钻头,后来经过入库,可能又增加了10种钻头,那么现在要看钻头类的结存应该为20种.
我想了半天也没有想到好的办法.

...全文
147 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wumylove1234 2005-03-15
  • 打赏
  • 举报
回复
上面的问题,只是我报表数据的一小部分,我用上面我试验的方法试了一下,暂时还看不出来哪有问题,也不知道会有什么结果.
现在我将报表的一部分数据在存储过程中生成了,可是超过了8000个字符,怎么办?
  • 打赏
  • 举报
回复
先顶后看。
wumylove1234 2005-03-14
  • 打赏
  • 举报
回复
回复人: ruan_hg(阿阮) ( ) 信誉:100 2005-03-14 12:26:00 得分: 0


用compute count(goodsid)不就解决了吗?



请举个例子,我看了一下帮助,没有看明白.
wumylove1234 2005-03-14
  • 打赏
  • 举报
回复
我建了两个表,结构一样,字段名一样,模拟了一下这种情况
Table1
T
1
2
3

Table2
T
3
4
5

select T1 from
(select A.T as T1,B.T as T2 from table1 A full join table2 B on A.T=B.T) A where T1 is not null and T2 is not null
union
select T1 as T2 from
(select A.T as T1,B.T as T2 from table1 A full join table2 B on A.T=B.T) A where T1 is not null and T2 is null
union
select T2 as T3 from
(select A.T as T1,B.T as T2 from table1 A full join table2 B on A.T=B.T) A where T1 is null and T2 is not null
我用这样的查询查出了所有可能的值.
大侠们帮看一下,我这种方法好不好?你们有什么好的方法没有?
ruan_hg 2005-03-14
  • 打赏
  • 举报
回复
用compute count(goodsid)不就解决了吗?
wumylove1234 2005-03-14
  • 打赏
  • 举报
回复
我考虑的等式是这样的,可是暂时我还没有想出来如何查询:

1,查出所有期初与以后发现的共有的种类T1.
2,查出所有期初有但后来入没有的种类T2.
3,查出所有期初无但后来有的种类T3.

这样当然结余的种类数为:T1+T2+T3
wumylove1234 2005-03-14
  • 打赏
  • 举报
回复
晕了,要的不是数量,是种类.

我的意思是说:
比如期初时,库存中钻头的种类有10种(不是有10个钻头),和以后入库的比较.因为我现在要做的报表是这样的:

类别 品 种
结存 新增
钻头 10 2
丝锥 5 2

结存:就是从期到到统计日期为止和钻头的种类,新增是本期新增的种类

因为情况是这样的,
钻头类,可能有100种这样的商品,
但是在期初时可能只有30种,而后进货后,可能又新增了10种,
这样就是发生过记录的钻头类就是期初30种,到结存为止,为40种,其中本期新增10种
LoveLwn 2005-03-14
  • 打赏
  • 举报
回复
select *
商品 = a.name,
结存 = sum(c.goodnum)
from
goodtype a,
商品表 b,
(select goodsid,goodnum from HisStore
union all
select goodsid,goodsnum from InD) c
where
a.id = b.goodtype_id and b.id = c.goodsid
group by
a.name
子陌红尘 2005-03-14
  • 打赏
  • 举报
回复
select *
商品 = a.name,
结存 = sum(c.goodnum)
from
goodtype a,
商品表 b,
(select goodsid,goodnum from HisStore
union all
select goodsid,goodsnum from InD) c
where
a.id = b.goodtype_id and b.id = c.goodsid
group by
a.name

34,588

社区成员

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

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