请大家帮我看夏写的SQL大概对不对
因为表太多,列太多,没法贴太多请大家帮看下对不对。
目前4个表:机构表、门店表、批次进价表、采购进价表
要求:查出 采购进价>批次进价的(商品、部门、机构、当前库存、当前批次进价、当前门店进价、门店为1034课的)
因为对超市行业也不了解,每次写的东西数据出来了,约束力不强。请各位高手粗略看下对不对
/*任务*/
select a.部门名,b.门店名字,c.商品编码,c.剩余库存,c.当前进价,d.当前采购价 from
(select c_adno as 部门,c_name 部门名 from tb_depart where c_adno =1304) a,
(select c_id as 门店,c_name as 门店名字 from tb_store) b,
(select c_guid as 进货批次,
c_store_id as 门店,
c_gcode as 商品编码,
c_n_remain as 剩余库存,
c_at_in as 当前进价
from tb_batch_in (nolock)
where c_n_remain >0
group by c_guid,c_store_id,c_gcode,c_n_remain,c_at_in) c,
(select c_gcode as 商品编码,c_store_id as 机构,c_pt_in as 当前采购价 from tb_gdsprovider (nolock)
group by c_store_id,c_gcode,c_pt_in
) d
where c.门店=b.门店 and d.机构=b.门店 and c.商品编码=d.商品编码
/*任务*/ 库
[解决办法]
select a.部门名,b.门店名字,c.商品编码,c.剩余库存,c.当前进价,d.当前采购价 from
(select c_adno as 部门,c_name 部门名 from tb_depart where c_adno =1304) a,
(select c_id as 门店,c_name as 门店名字 from tb_store) b,
(select c_guid as 进货批次,
c_store_id as 门店,
c_gcode as 商品编码,
c_n_remain as 剩余库存,
c_at_in as 当前进价
from tb_batch_in (nolock)
where c_n_remain >0
group by c_guid,c_store_id,c_gcode,c_n_remain,c_at_in) c,
(select c_gcode as 商品编码,c_store_id as 机构,c_pt_in as 当前采购价 from tb_gdsprovider (nolock) -->这里的括号你用全角括号,我改了
group by c_store_id,c_gcode,c_pt_in
) d
where c.门店=b.门店 and d.机构=b.门店 and c.商品编码=d.商品编码
--还有,没有聚合函数你用了group by ,不合适.有group by 就有count(*),sum(字段),否同用distinct