帮我看下SQL语句错在哪里select a.comid as 货号,a.barcode as 条码,sum(b.stock_qty) as 总库存,sum(c.sale_qnty) as 总销售fromtemp01 a left join bwsyv9_01.dbo.ic_t_branch_stock b on(a.item_no = b.item_no ) left join bwsyv9_01.dbo.pos_t
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/17 23:36:05
帮我看下SQL语句错在哪里select a.comid as 货号,a.barcode as 条码,sum(b.stock_qty) as 总库存,sum(c.sale_qnty) as 总销售fromtemp01 a left join bwsyv9_01.dbo.ic_t_branch_stock b on(a.item_no = b.item_no ) left join bwsyv9_01.dbo.pos_t
帮我看下SQL语句错在哪里
select a.comid as 货号,a.barcode as 条码,sum(b.stock_qty) as 总库存,sum(c.sale_qnty) as 总销售
from
temp01 a left join bwsyv9_01.dbo.ic_t_branch_stock b on(a.item_no = b.item_no ) left join bwsyv9_01.dbo.pos_t_saleflow c on(a.item_no = c.item_no)
where
a.yesno ='0'
group by
a.comid,a.barcode
select a.comid as 货号,a.barcode as 条码,sum(b.stock_qty) as 总库存数,sum(c.sale_qnty) as 总销售
from
temp01 a,bwsyv9_01.dbo.pos_t_saleflow c,bwsyv9_01.dbo.ic_t_branch_stock b
where
(a.item_no = c.item_no and a.item_no = b.item_no) and a.yesno ='0'
group by
a.comid,a.barcode
这两个语句都有同样一个错误 SUM()函数内统计的数量完全不正确
我用商品资料表同时结联了一个库存与一个销售表 之后查出来就不正确了(SUM统计的库存与销售都是多倍的)
如果我只用一个JOIN(结联一个表)就不会出现这种问题
帮我看下错在哪里?
帮我看下SQL语句错在哪里select a.comid as 货号,a.barcode as 条码,sum(b.stock_qty) as 总库存,sum(c.sale_qnty) as 总销售fromtemp01 a left join bwsyv9_01.dbo.ic_t_branch_stock b on(a.item_no = b.item_no ) left join bwsyv9_01.dbo.pos_t
以你第一个为例,主要是你在销售和库存中有多条同一个item_no的记录,所以造成笛卡尔积
第一个这样
select a.comid as 货号,a.barcode as 条码,b.总库存,c.总销售from temp01 left join
(select item_no,sum(stock_qty) as 总库存 from bwsyv9_01.dbo.ic_t_branch_stock group by item_no) b
on a.item_no = b.item_no
left join
(select item_no,sum(sale_qnty) as 总销售 from bwsyv9_01.dbo.pos_t_saleflow group by item_no) c
on a.item_no = c.item_no
where a.yesno ='0'
你第二个应该也是类似问题