如何将这两个查询连接在一起?

时间:2017-01-27 17:47:41

标签: sql oracle join sum

有没有办法将这两个查询连接在一起

select --STR_NBR "Store",
DAY_DATE "Date",
b.whse_prod_vendor_nbr,
PLN_NBR,
sum(LO_IOH_UNITS) "STORE INVENTORY ON HAND"
--sum(LO_IOH_ORDER_QTY) "STORE ORDERS"

from MSS_OWNER.FCT_IOH_DAY_STR_PLN a
  right join dim_prod_pln b
        on a.prod_id = b.prod_id
  right join dim_period c
        on a.per_id = c.per_id
  inner join dim_location d
        on a.loc_id = d.str_nbr

Where DAY_DATE >= to_date ('1/19/2017','MM/DD/YYYY') --- ENTER THE DATE
and DAY_DATE <= to_date ('1/19/2017','MM/DD/YYYY') --- ENTER THE SAME DATE OR A RANGE
and (LO_IOH_UNITS) >0
--and str_nbr in ()
and PLN_NBR in ('40000278132')
--and b.whse_prod_vendor_nbr in ('040683')


group by -- STR_NBR,
DAY_DATE,
b.whse_prod_vendor_nbr,
PLN_NBR

第二次查询

    select DAY_DATE,
pln_nbr,
sum(a.TOT_REG_IOH_UNITS+a.TOT_DISTR_IOH_UNITS) "DC OH HAND Units", -- This is the total on Hand. It includes Reg OH and Dist OH
sum(a.TOT_REG_ordr_UNITS+a.TOT_DISTR_ordr_UNITS) "DC Dist OO "

From MSS_OWNER.FCT_DA_DAY_DC_VEND_WIC a, dim_prod_wic b, dim_period c

where a.PROD_ID = b.PROD_ID
and a.PER_ID = c.PER_ID
and DAY_DATE >= to_date ('1/26/2017','MM/DD/YYYY') --- ENTER THE DATE
and DAY_DATE <= to_date ('1/26/2017','MM/DD/YYYY') --- ENTER THE SAME DATE OR A RANGE
and (TOT_REG_IOH_UNITS+TOT_DISTR_IOH_UNITS+TOT_REG_ordr_UNITS+TOT_DISTR_ordr_UNITS)>0
--and b. PLN_NBR in ('40000278132')
and b.whse_prod_vendor_nbr in ('040683')

group by
DAY_DATE,
PLN_NBR

目标是拥有6个日期日期,供应商nbr,pln nbr,商店库存,dc ioh,dc ioo 有任何想法吗 ?

1 个答案:

答案 0 :(得分:2)

我认为这是您想要的,但不确定dc ioh,dc ioo列是否正确但您可以根据需要进行修改:

SELECT     B.DAY_DATE AS [Day date]
        , b.whse_prod_vendor_nbr AS [vendor nbr]
        , A.PLN_NBR AS [pln nbr]
        , [STORE INVENTORY ON HAND]
        , [DC OH HAND Units] AS [dc ioh]
        , [DC Dist OO] AS [dc ioo]
FROM
         (SELECT --STR_NBR "Store",
          DAY_DATE "Date"
        , b.whse_prod_vendor_nbr
        , PLN_NBR
        , SUM(LO_IOH_UNITS) "STORE INVENTORY ON HAND"
          --sum(LO_IOH_ORDER_QTY) "STORE ORDERS"
          FROM  MSS_OWNER.FCT_IOH_DAY_STR_PLN a
               RIGHT JOIN dim_prod_pln b ON a.prod_id = b.prod_id
               RIGHT JOIN dim_period c ON a.per_id = c.per_id
               INNER JOIN dim_location d ON a.loc_id = d.str_nbr
          WHERE DAY_DATE >= to_date
               ('1/19/2017', 'MM/DD/YYYY') --- ENTER THE DATE
               AND DAY_DATE <= to_date
               ('1/19/2017', 'MM/DD/YYYY') --- ENTER THE SAME DATE OR A RANGE
               AND (LO_IOH_UNITS) > 0
               --and str_nbr in ()
               AND PLN_NBR IN('40000278132')
          --and b.whse_prod_vendor_nbr in ('040683')
          GROUP BY -- STR_NBR,
          DAY_DATE
        , b.whse_prod_vendor_nbr
        , PLN_NBR) AS A
         INNER JOIN
                 (SELECT DAY_DATE
                      , pln_nbr
                      , SUM(a.TOT_REG_IOH_UNITS + a.TOT_DISTR_IOH_UNITS) "DC OH HAND Units" -- This is the total on Hand. It includes Reg OH and Dist OH
                      , SUM(a.TOT_REG_ordr_UNITS + a.TOT_DISTR_ordr_UNITS) "DC Dist OO "
                  FROM   MSS_OWNER.FCT_DA_DAY_DC_VEND_WIC a
                      , dim_prod_wic b
                      , dim_period c
                  WHERE  a.PROD_ID = b.PROD_ID
                        AND a.PER_ID = c.PER_ID
                        AND DAY_DATE >= to_date
                       ('1/26/2017', 'MM/DD/YYYY') --- ENTER THE DATE
                        AND DAY_DATE <= to_date
                       ('1/26/2017', 'MM/DD/YYYY') --- ENTER THE SAME DATE OR A RANGE
                        AND (TOT_REG_IOH_UNITS + TOT_DISTR_IOH_UNITS + TOT_REG_ordr_UNITS + TOT_DISTR_ordr_UNITS) > 0
                        --and b. PLN_NBR in ('40000278132')
                        AND b.whse_prod_vendor_nbr IN('040683')
                  GROUP BY DAY_DATE
                        , PLN_NBR) AS B ON A.PLN_NBR = B.PLN_NBR
                                       AND A.[Date] = B.DAY_DATE;