日期:2014-05-17  浏览次数:20771 次

请大家帮我看夏写的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

------解决方案--------------------
不用数据也会有错,分组了就必须用聚合函数,SQL SERVER会提示你的
------解决方案--------------------
看过了,语法和业务逻辑上,都没什么问题。
--保证:
--#1.部门表只查出一条记录
--#2.一个门店内包含多个商品编码,且门店+商品编码可以唯一确定C表一条记录