|
本帖最后由 terry367 于 2021-7-14 17:01 编辑
表1和表2都只有200多行的数据求大佬伸出猿手
- select d.款号,d.大类,d.数量,c.店数 from
- /******表1******/
- (SELECT SHANGPIN.spdm as 款号,[FJSX2].sxmc as 大类
- ,COALESCE(sum([SG_Gatherings].[fQuantity]),0) as 数量
- FROM [BS3000+_000_2016].[dbo].[SG_Gatherings]
- left join[BS3000+_000_2016].[dbo].[SG_Gathering] on [SG_Gatherings].[vMBillID]=[SG_Gathering].[vMBillID]
- left join[BS3000+_000_2016].[dbo].KEHU on khdm=[SG_Gatherings].vshop
- left join[BS3000+_000_2016].[dbo].[ShangPin] on vStyle=spdm
- left join[BS3000+_000_2016].[dbo].[FJSX2] on fjsx2=[FJSX2].sxdm
- WHERE bcancel='0' and bputup='0' and FJSX4='g'and fjsx3='2' and fjsx2.sxmc!='配件' AND fjsx2.sxmc!='礼品' and fjsx10!='950'
- and CONVERT(date,dtdate)>=CONVERT(date,getdate()-7) and CONVERT(date,dtdate)<CONVERT(date,getdate())
- group by SHANGPIN.spdm,[FJSX2].sxmc
- )as d
- left join
- /******表2******/
- (
- select b.款 as 款,count(店) as 店数 from(
- select ckmc as 店,款 as 款,sum(数量)as 库存 from(
- SELECT ckmc,shangpin.spdm as 款
- ,COALESCE(sum(SPKCB.[SL]),0) as 数量,数据来源=N'库存'
- FROM [BS3000+_000_2016].[dbo].SPKCB
- left join[BS3000+_000_2016].[dbo].[ShangPin]on SPKCB.spdm=[ShangPin].spdm
- left join[BS3000+_000_2016].[dbo].cangku on cangku.ckdm=SPKCB.ckdm
- where cangku.byzd5='1' and FJSX4='g'and fjsx3='2' and fjsx2!='0' and SL!=0
- group by ckmc,shangpin.spdm
- union all
- /****调出未收****/
- SELECT ckmc, shangpin.spdm
- ,COALESCE(sum(spycdmx.[SL]),0) as 数量,数据来源=N'调出未收'
- FROM [BS3000+_000_2016].[dbo].spycdmx
- left join[BS3000+_000_2016].[dbo].[ShangPin]on spycdmx.spdm=[ShangPin].spdm
- left join[BS3000+_000_2016].[dbo].spycd on spycd.djbh=spycdmx.djbh
- left join[BS3000+_000_2016].[dbo].CANGKU on spycd.dm2=cangku.ckdm
- where cangku.byzd5='1' and FJSX4='g'and fjsx3='2' and fjsx2!='0' and spycd.sh=0
- group by ckmc, shangpin.spdm
- union all
- /****已发未收****/
- SELECT ckmc ,shangpin.spdm
- ,COALESCE(sum(sdphdmx.[SL]),0) as 数量,数据来源=N'已发未收'
- FROM [BS3000+_000_2016].[dbo].sdphdmx
- left join[BS3000+_000_2016].[dbo].[ShangPin] on sdphdmx.spdm=[ShangPin].spdm
- left join[BS3000+_000_2016].[dbo].sdphd on sdphd.djbh=sdphdmx.djbh
- left join[BS3000+_000_2016].[dbo].CANGKU on ckdm=sdphd.DM1
- where cangku.byzd5='1' and FJSX4='g'and fjsx3='2' and fjsx2!='0'
- and sdphd.ys=1 and sdphd.sh=0
- group by ckmc, shangpin.spdm)as a
- group by ckmc,款)as b
- where 库存 >2
- group by b.款
- )as c on d.款号=c.款
复制代码
|
|