• 两个表横向合并


    在销售出库毛利表报表中遇到了一个情况:

      1、分别查询销售订单和调拨订单的数据

        (1)、销售订单

        

    SELECT distinct
    tbm.fnumber 物料编码
    ,tbm.fname_l2 物料名称
    ,tbm.fmodel 物料规格型号
    ,sum(tsse.fqty) 订单总数量
    FROM T_IM_SaleIssueBill tis
    left JOIN T_IM_SaleIssueEntry tise on tise.fparentid = tis.fid
    right join T_SD_SaleOrder tss on tss.fnumber = tise.FSaleOrderNumber--销售订单
    right join T_SD_SaleOrderEntry tsse on tsse.fparentid = tss.fid--销售订单分录
    left join T_ORG_Storage tos on tos.fid = tise.FStorageOrgUnitID--库存组织
    left join T_BD_Material tbm on tbm.fid = tise.FMaterialID
    left join T_SCM_BillType tcb on tcb.fid = tise.FCoreBillTypeID
    left join T_IM_TransferOrderBill tit on tit.fnumber = tise.fsaleordernumber--调拨订单
    left join T_IM_TransferOrderBillEntry tite on tite.fparentid = tit.fid--调拨订单分录
    where to_char(tis.fbizdate,'yyyy-MM-dd') <= '2018-07-04'
    and to_char(tis.fbizdate,'yyyy-MM-dd') >= '2018-01-04'
    and tos.fname_l2 = '电子器件厂'
    and tis.fbasestatus ='4'
    and tcb.fname_l2 in('销售订单')
    --and tite.fqty is not null
    --or tcb.fname_l2 in('调拨订单')
    group by tbm.fnumber,tbm.fname_l2,tbm.fmodel

              

                  (2)、调拨订单

    SELECT distinct
    tbm.fnumber 物料编码
    ,tbm.fname_l2 物料名称
    ,tbm.fmodel 物料规格型号
    ,sum(tite.fqty) 订单总数量
    FROM T_IM_SaleIssueBill tis
    left JOIN T_IM_SaleIssueEntry tise on tise.fparentid = tis.fid
    left join T_SD_SaleOrder tss on tss.fnumber = tise.FSaleOrderNumber--销售订单
    left join T_SD_SaleOrderEntry tsse on tsse.fparentid = tss.fid--销售订单分录
    left join T_ORG_Storage tos on tos.fid = tise.FStorageOrgUnitID--库存组织
    inner join T_BD_Material tbm on tbm.fid = tise.FMaterialID
    inner join T_SCM_BillType tcb on tcb.fid = tise.FCoreBillTypeID
    left join T_IM_TransferOrderBill tit on tit.fnumber = tise.fsaleordernumber--调拨订单
    left join T_IM_TransferOrderBillEntry tite on tite.fparentid = tit.fid--调拨订单分录
    where to_char(tis.fbizdate,'yyyy-MM-dd') <= '2018-07-04'
    and to_char(tis.fbizdate,'yyyy-MM-dd') >= '2018-01-04'
    and tos.fname_l2 = '电子器件厂'
    and tis.fbasestatus ='4'
    --and tcb.fname_l2 in('销售订单')
    and tcb.fname_l2 in('调拨订单')
    --and tite.fqty is not null
    group by tbm.fnumber,tbm.fname_l2,tbm.fmodel

        

        (3)、将两张表用union连接起来

    SELECT distinct
    tbm.fnumber 物料编码
    ,tbm.fname_l2 物料名称
    ,tbm.fmodel 物料规格型号
    ,sum(tsse.fqty) 订单总数量
    FROM T_IM_SaleIssueBill tis
    left JOIN T_IM_SaleIssueEntry tise on tise.fparentid = tis.fid
    right join T_SD_SaleOrder tss on tss.fnumber = tise.FSaleOrderNumber--销售订单
    right join T_SD_SaleOrderEntry tsse on tsse.fparentid = tss.fid--销售订单分录
    left join T_ORG_Storage tos on tos.fid = tise.FStorageOrgUnitID--库存组织
    left join T_BD_Material tbm on tbm.fid = tise.FMaterialID
    left join T_SCM_BillType tcb on tcb.fid = tise.FCoreBillTypeID
    left join T_IM_TransferOrderBill tit on tit.fnumber = tise.fsaleordernumber--调拨订单
    left join T_IM_TransferOrderBillEntry tite on tite.fparentid = tit.fid--调拨订单分录
    where to_char(tis.fbizdate,'yyyy-MM-dd') <= '2018-07-04'
    and to_char(tis.fbizdate,'yyyy-MM-dd') >= '2018-01-04'
    and tos.fname_l2 = '电子器件厂'
    and tis.fbasestatus ='4'
    and tcb.fname_l2 in('销售订单')
    --and tite.fqty is not null
    --or tcb.fname_l2 in('调拨订单')
    group by tbm.fnumber,tbm.fname_l2,tbm.fmodel

    union all



    SELECT distinct
    tbm.fnumber 物料编码
    ,tbm.fname_l2 物料名称
    ,tbm.fmodel 物料规格型号
    ,sum(tite.fqty) 订单总数量
    FROM T_IM_SaleIssueBill tis
    left JOIN T_IM_SaleIssueEntry tise on tise.fparentid = tis.fid
    left join T_SD_SaleOrder tss on tss.fnumber = tise.FSaleOrderNumber--销售订单
    left join T_SD_SaleOrderEntry tsse on tsse.fparentid = tss.fid--销售订单分录
    left join T_ORG_Storage tos on tos.fid = tise.FStorageOrgUnitID--库存组织
    inner join T_BD_Material tbm on tbm.fid = tise.FMaterialID
    inner join T_SCM_BillType tcb on tcb.fid = tise.FCoreBillTypeID
    left join T_IM_TransferOrderBill tit on tit.fnumber = tise.fsaleordernumber--调拨订单
    left join T_IM_TransferOrderBillEntry tite on tite.fparentid = tit.fid--调拨订单分录
    where to_char(tis.fbizdate,'yyyy-MM-dd') <= '2018-07-04'
    and to_char(tis.fbizdate,'yyyy-MM-dd') >= '2018-01-04'
    and tos.fname_l2 = '电子器件厂'
    and tis.fbasestatus ='4'
    --and tcb.fname_l2 in('销售订单')
    and tcb.fname_l2 in('调拨订单')
    --and tite.fqty is not null
    group by tbm.fnumber,tbm.fname_l2,tbm.fmodel
    ) a
    group by 物料编码

        (4)、然后再进行分组

    select *from 

    (

     select 物料编码,sum(订单总数量) 订单总数量 from 

     (

     SELECT distinct 

     tbm.fnumber 物料编码

     ,tbm.fname_l2 物料名称

     ,tbm.fmodel   物料规格型号

     ,sum(tsse.fqty) 订单总数量

     FROM T_IM_SaleIssueBill tis 

     left JOIN   T_IM_SaleIssueEntry tise on tise.fparentid = tis.fid

     right join    T_SD_SaleOrder          tss  on tss.fnumber = tise.FSaleOrderNumber--销售订单

     right join    T_SD_SaleOrderEntry     tsse on tsse.fparentid = tss.fid--销售订单分录

     left join    T_ORG_Storage   tos  on tos.fid = tise.FStorageOrgUnitID--库存组织

     left join    T_BD_Material           tbm  on tbm.fid = tise.FMaterialID

     left join    T_SCM_BillType          tcb  on tcb.fid  = tise.FCoreBillTypeID

     left join   T_IM_TransferOrderBill  tit  on tit.fnumber = tise.fsaleordernumber--调拨订单

     left join    T_IM_TransferOrderBillEntry  tite on tite.fparentid = tit.fid--调拨订单分录

     where      to_char(tis.fbizdate,'yyyy-MM-dd')  <= '2018-07-04'

     and        to_char(tis.fbizdate,'yyyy-MM-dd')  >= '2018-01-04'

     and        tos.fname_l2 = '电子器件厂'

     and        tis.fbasestatus ='4'

     and        tcb.fname_l2 in('销售订单')

     --and        tite.fqty  is not null

     --or         tcb.fname_l2 in('调拨订单')

     group by tbm.fnumber,tbm.fname_l2,tbm.fmodel

     union all

     SELECT distinct 

     tbm.fnumber 物料编码

     ,tbm.fname_l2 物料名称

     ,tbm.fmodel   物料规格型号

     ,sum(tite.fqty) 订单总数量

     FROM T_IM_SaleIssueBill tis 

     left JOIN   T_IM_SaleIssueEntry tise on tise.fparentid = tis.fid

     left join    T_SD_SaleOrder          tss  on tss.fnumber = tise.FSaleOrderNumber--销售订单

     left join    T_SD_SaleOrderEntry     tsse on tsse.fparentid = tss.fid--销售订单分录

     left join    T_ORG_Storage   tos  on tos.fid = tise.FStorageOrgUnitID--库存组织

     inner join    T_BD_Material           tbm  on tbm.fid = tise.FMaterialID

     inner join    T_SCM_BillType          tcb  on tcb.fid  = tise.FCoreBillTypeID

     left join   T_IM_TransferOrderBill  tit  on tit.fnumber = tise.fsaleordernumber--调拨订单

     left join    T_IM_TransferOrderBillEntry  tite on tite.fparentid = tit.fid--调拨订单分录

     where      to_char(tis.fbizdate,'yyyy-MM-dd')  <= '2018-07-04'

     and        to_char(tis.fbizdate,'yyyy-MM-dd')  >= '2018-01-04'

     and        tos.fname_l2 = '电子器件厂'

     and        tis.fbasestatus ='4'

     --and      tcb.fname_l2 in('销售订单')

     and        tcb.fname_l2 in('调拨订单')

     --and tite.fqty  is not null

     group by tbm.fnumber,tbm.fname_l2,tbm.fmodel

     ) a

     group by 物料编码

     ) taq

    select *from ( select 物料编码,sum(订单总数量) 订单总数量 from  ( SELECT distinct  tbm.fnumber 物料编码 ,tbm.fname_l2 物料名称 ,tbm.fmodel   物料规格型号 ,sum(tsse.fqty) 订单总数量 FROM T_IM_SaleIssueBill tis  left JOIN   T_IM_SaleIssueEntry tise on tise.fparentid = tis.fid right join    T_SD_SaleOrder          tss  on tss.fnumber = tise.FSaleOrderNumber--销售订单 right join    T_SD_SaleOrderEntry     tsse on tsse.fparentid = tss.fid--销售订单分录 left join    T_ORG_Storage   tos  on tos.fid = tise.FStorageOrgUnitID--库存组织 left join    T_BD_Material           tbm  on tbm.fid = tise.FMaterialID left join    T_SCM_BillType          tcb  on tcb.fid  = tise.FCoreBillTypeID left join   T_IM_TransferOrderBill  tit  on tit.fnumber = tise.fsaleordernumber--调拨订单 left join    T_IM_TransferOrderBillEntry  tite on tite.fparentid = tit.fid--调拨订单分录 where      to_char(tis.fbizdate,'yyyy-MM-dd')  <= '2018-07-04' and        to_char(tis.fbizdate,'yyyy-MM-dd')  >= '2018-01-04' and        tos.fname_l2 = '电子器件厂' and        tis.fbasestatus ='4' and        tcb.fname_l2 in('销售订单') --and        tite.fqty  is not null --or         tcb.fname_l2 in('调拨订单') group by tbm.fnumber,tbm.fname_l2,tbm.fmodel  union all    SELECT distinct  tbm.fnumber 物料编码 ,tbm.fname_l2 物料名称 ,tbm.fmodel   物料规格型号 ,sum(tite.fqty) 订单总数量 FROM T_IM_SaleIssueBill tis  left JOIN   T_IM_SaleIssueEntry tise on tise.fparentid = tis.fid left join    T_SD_SaleOrder          tss  on tss.fnumber = tise.FSaleOrderNumber--销售订单 left join    T_SD_SaleOrderEntry     tsse on tsse.fparentid = tss.fid--销售订单分录 left join    T_ORG_Storage   tos  on tos.fid = tise.FStorageOrgUnitID--库存组织 inner join    T_BD_Material           tbm  on tbm.fid = tise.FMaterialID inner join    T_SCM_BillType          tcb  on tcb.fid  = tise.FCoreBillTypeID left join   T_IM_TransferOrderBill  tit  on tit.fnumber = tise.fsaleordernumber--调拨订单 left join    T_IM_TransferOrderBillEntry  tite on tite.fparentid = tit.fid--调拨订单分录 where      to_char(tis.fbizdate,'yyyy-MM-dd')  <= '2018-07-04' and        to_char(tis.fbizdate,'yyyy-MM-dd')  >= '2018-01-04' and        tos.fname_l2 = '电子器件厂' and        tis.fbasestatus ='4' --and      tcb.fname_l2 in('销售订单') and        tcb.fname_l2 in('调拨订单') --and tite.fqty  is not null group by tbm.fnumber,tbm.fname_l2,tbm.fmodel ) a group by 物料编码 ) taq

  • 相关阅读:
    验证授权【msdn】
    实战 Comet 应用程序开发
    ASP.NET Forms验证 实现子域名(SubDomain)共享登陆下的缺陷 [转]
    分享WordPress博客搜索引擎优化的六点经验 博客园 cnbogs
    支持支付宝(Alipay)付款的三个美国主机商
    认证,授权2
    登录代码,程序不是作文
    Google 的PageRank值对网站成功有多重要
    SQL Server 2005 Service Broker 初探 [摘抄]
    jQuerySelectors(选择器)的使用(四五、内容篇&可见性篇) cnblogs zhuan
  • 原文地址:https://www.cnblogs.com/ygzs007/p/9275336.html
Copyright © 2020-2023  润新知