34,588
社区成员
发帖
与我相关
我的任务
分享
create view a
as
select isnull(xs.分店名称,th.分店名称) 分店名称,
isnull(xs.货品代码,th.货品代码) 货品代码,
isnull(xs.货品名称,th.货品名称) 货品名称,
isnull(xs.单位,th.单位) 单位,
sum(isnull(xs.数量,0)) 销售数量,
sum(isnull(th.数量,0)) 退货数量,
sum(isnull(xs.数量,0))-sum(isnull(th.数量,0)) 销退差量
from 销售 xs full join 退货 th on xs.分店名称=th.分店名称 and xs.货品代码=th.货品代码 and xs.单位=th.单位 and xs.货品名称=th.货品名称
group by isnull(xs.分店名称,th.分店名称),isnull(xs.货品代码,th.货品代码),isnull(xs.单位,th.单位),isnull(xs.货品名称,th.货品名称)
create view aa
as
select isnull(xs.分店名称,th.分店名称) 分店名称,
isnull(xs.货品代码,th.货品代码) 货品代码,
isnull(xs.货品名称,th.货品名称) 货品名称,
isnull(xs.单位,th.单位) 单位,
isnull(xs.数量,0) 销售数量,
isnull(th.数量,0) 退货数量,
isnull(xs.数量,0)-isnull(th.数量,0) 销退差量
from (select 分店名称,货品代码,货品名称,sum(数量) 数量,单位 from 销售 group by 分店名称,货品代码,货品名称,单位) xs
full join
(select 分店名称,货品代码,货品名称,sum(数量) 数量,单位 from 退货 group by 分店名称,货品代码,货品名称,单位) th
on xs.分店名称=th.分店名称 and xs.货品代码=th.货品代码 and xs.单位=th.单位 and xs.货品名称=th.货品名称
然后验算销售总量和退货总量:
1、select sum(销售数量),sum(退货数量) from a
2、select sum(销售数量),sum(退货数量) from aa
发现1的销售和退货的总数量都是偏大,2的销售和退货的总数量就是对的,我就不明白,一个是先求差再分组,一个是先分组再求差,按道理是一样的,可是结果却不一样,错在哪里?
select *
from 销售 xs full join 退货 th on xs.分店名称=th.分店名称 and xs.货品代码=th.货品代码 and xs.单位=th.单位 and xs.货品名称=th.货品名称
楼主先执行以下上面的代码是不是会有重复的记录?
--注意第一个是"full join 退货 th on "
第二个是"full join
(select 分店名称,货品代码,货品名称,sum(数量) 数量,单位 from 退货 group by 分店名称,货品代码,货品名称,单位) th
on "