有没有会oracle的大神,帮忙看看怎么合并数据

怎么把表1,2,3合并之后得到最后的表,我用left join on 或者right join on??具体怎么写呢。我自己写了好几遍,都不行

select c.orgcode as 组织,b.barcode as 条码,a.pinlei as 品类,a.plucode as 商品编码,a.pluname as 名称,

nvl(b.xscount,0) as 销量,nvl(b.hxtotal,0) as 销售额,c.kccount as 库存 from 

(select  plucode,pluname,pinlei from plucaiji) a  

left  join 

(select  ts.orgcode ,ts.barcode, ts.plucode,sum(ts.xscount) as xscount,sum(ts.hxtotal) as hxtotal

from rhz_xsmx ts where ts.rptdate between '2021-12-18' and '2021-12-31'  

GROUP BY  ts.orgcode,ts.plucode,ts.barcode ,ts.pluname ) b

   on a.plucode=b.plucode

   

left join

( select m.orgcode,n.plucode,n.pluname,  sum(m.KcCount) as kccount 

        from tStkLsKc m ,tSkuPlu n where m.PluID = n.PluID 

   group by m.orgcode,n.plucode,n.pluname) c 

     on  b.orgcode=c.orgcode and a.plucode=c.plucode

   where  c.orgcode in ('0001') and a.pinlei in ('福临门') order by a.plucode;

image.png

image.png

image.png

FineReport 15900334107 发布于 2022-1-5 17:04 (编辑于 2022-1-5 17:29)
1min目标场景问卷 立即参与
回答问题
悬赏:3 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共1回答
最佳回答
0
CD20160914Lv8专家互助
发布于2022-1-5 17:06(编辑于 2022-1-5 17:38)

这是不是最基础的三个表关联吗。。。

select 

c.orgcode as 组织,

b.barcode as 条码,

a.pinlei as 品类,

a.plucode as 商品编码,

a.pluname as 名称, 

nvl(b.xscount,0) as 销量,

nvl(b.hxtotal,0) as 销售额,

c.kccount as 库存 

from plucaiji  a 

left join (select ts.orgcode 

,ts.barcode

, ts.plucode

,sum(ts.xscount) as xscount

,sum(ts.hxtotal) as hxtotal 

from rhz_xsmx ts 

where ts.rptdate between '2021-12-18' and '2021-12-31' 

GROUP BY ts.orgcode,ts.plucode

,ts.barcode ,ts.pluname ) b on a.plucode=b.plucode 

left join ( 

select m.orgcode

,n.plucode

,n.pluname

, sum(m.KcCount) as kccount 

from tStkLsKc m 

,tSkuPlu n 

where m.PluID = n.PluID

and m.orgcode in ('0001') 

group by m.orgcode

,n.plucode

,n.pluname

) c on b.orgcode=c.orgcode and a.plucode=c.plucode 

where 1=1

and a.pinlei in ('福临门') 

order by a.plucode

image.png

这里的限制移动到这个箭头的后面,不要放在最外面where 

select 

c.orgcode as 组织,

b.barcode as 条码,

a.pinlei as 品类,

a.plucode as 商品编码,

a.pluname as 名称, 

nvl(b.xscount,0) as 销量,

nvl(b.hxtotal,0) as 销售额,

c.kccount as 库存 

from (

select 

plucode,

pluname

,pinlei 

from plucaiji) a 

left join (select ts.orgcode 

,ts.barcode

, ts.plucode

,sum(ts.xscount) as xscount

,sum(ts.hxtotal) as hxtotal 

from rhz_xsmx ts where 

ts.rptdate between '2021-12-18' and '2021-12-31' 

GROUP BY ts.orgcode,ts.plucode

,ts.barcode ,ts.pluname ) b on a.plucode=b.plucode 

left join ( 

select m.orgcode

,n.plucode

,n.pluname

, sum(m.KcCount) as kccount 

from tStkLsKc m 

,tSkuPlu n where m.PluID = n.PluID

and m.orgcode in ('0001')  

group by m.orgcode

,n.plucode

,n.pluname

) c on b.orgcode=c.orgcode and a.plucode=c.plucode 

where 1=1

and a.pinlei in ('福临门') 

order by a.plucode

  • 15900334107 15900334107(提问者) 我也是这么写的。后面加了个where a.pinlei=\'食品\' 然后 就不行了
    2022-01-05 17:20 
  • 15900334107 15900334107(提问者) 这么一关联。a表本来有27条商品。查询出来之后 就只有23条了
    2022-01-05 17:21 
  • CD20160914 CD20160914 回复 15900334107(提问者) 你把你的语句复制到最上面。用回复的时候它会加\\上去。。。
    2022-01-05 17:24 
  • 15900334107 15900334107(提问者) 回复 CD20160914 已经弄到上面了。然后数据源的那个表也改了。刚是我随便做的表。有重复数据,正确的表是没有重复数据的
    2022-01-05 17:27 
  • CD20160914 CD20160914 回复 15900334107(提问者) 上面的语句改了。你限制c的条件写在了where的最后要。这样是全局限制了。。。
    2022-01-05 17:31 
  • 2关注人数
  • 374浏览人数
  • 最后回答于:2022-1-5 17:38
    请选择关闭问题的原因
    确定 取消
    返回顶部