ORA-01427 with table alias used in Procedure - oracle

I have the following snippet within a stored procedure:
(Note, this is not the original snippet, but exactly similar to it except that table and column names have been replaced with more generic ones for better understanding as well as to prevent giving out client code.)
UPDATE table_Orders c SET order_type =
(
SELECT 'PE' FROM
table_Orders A,
table_Orders b,
(SELECT DISTINCT order_id, cust_id, order_date FROM table_Orders WHERE shipment_type = 'T' AND order_availability = 'P' AND order_amt > 500
AND cust_category = 'PREMIUM' ) d
WHERE
A.order_id = d.order_id AND A.order_date = d.order_date AND A.cust_id= d.cust_id
AND A.order_id = b.order_id AND A.order_date = b.order_date AND A.cust_id= b.cust_id
AND A.shipment_type = b.shipment_type AND A.order_availability = b.order_availability AND A.product_id <> b.product_id
AND ((A.order_amt > 500 AND b.shipment_amt > 50) OR (A.shipment_amt > 50 AND b.order_amt > 500))
AND A.order_id = c.order_id AND A.order_date = c.order_date AND A.cust_id = c.cust_id
AND A.shipment_type = c.shipment_type AND A.order_availability = c.order_availability AND A.product_id = c.product_id
);
COMMIT;
Now, my issue is that i am getting the error ORA-01427 single-row subquery returns more than one row while executing the procedure. I tried putting the subquery at the end of the statement using an IN clause as below, but even that did not work:
UPDATE table_Orders c SET order_type =
(
SELECT 'PE' FROM
table_Orders A,
table_Orders b
WHERE
A.order_id = d.order_id AND A.order_date = d.order_date AND A.cust_id= d.cust_id
AND A.order_id = b.order_id AND A.order_date = b.order_date AND A.cust_id= b.cust_id
AND A.shipment_type = b.shipment_type AND A.order_availability = b.order_availability AND A.product_id <> b.product_id
AND ((A.order_amt > 500 AND b.shipment_amt > 50) OR (A.shipment_amt > 50 AND b.order_amt > 500))
AND A.order_id = c.order_id AND A.order_date = c.order_date AND A.cust_id = c.cust_id
AND A.shipment_type = c.shipment_type AND A.order_availability = c.order_availability AND A.product_id = c.product_id
AND (A.order_id, A.cust_id, A.order_date) IN
(SELECT DISTINCT order_id, cust_id, order_date FROM table_Orders
WHERE shipment_type = 'T' AND order_availability = 'P' AND order_amt > 500 AND cust_category = 'PREMIUM')
);
COMMIT;
Could someone kindly guide me in the right direction, as to where I'm bungling this up, or how the subquery is already messed up ?
Code With JOIN --> This gives missing right parenthesis at c.product_Id just before the "JOIN" statement
UPDATE table_Orders c SET order_type =
(
SELECT 'PE' FROM
table_Orders A,
table_Orders b
WHERE
A.order_id = d.order_id AND A.order_date = d.order_date AND A.cust_id= d.cust_id
AND A.order_id = b.order_id AND A.order_date = b.order_date AND A.cust_id= b.cust_id
AND A.shipment_type = b.shipment_type AND A.order_availability = b.order_availability AND A.product_id <> b.product_id
AND ((A.order_amt > 500 AND b.shipment_amt > 50) OR (A.shipment_amt > 50 AND b.order_amt > 500))
AND A.order_id = c.order_id AND A.order_date = c.order_date AND A.cust_id = c.cust_id
AND A.shipment_type = c.shipment_type AND A.order_availability = c.order_availability AND A.product_id = c.product_id
JOIN
SELECT DISTINCT order_id, cust_id, order_date FROM table_Orders
WHERE shipment_type = 'T' AND order_availability = 'P' AND order_amt > 500 AND cust_category = 'PREMIUM' d
ON A.order_id = d.order_id AND A.order_date = d.order_date AND A.cust_id= d.cust_id
COMMIT;

The subquery is probably returning more than one row for some c.order_id, c.order_date, c.cust_id, c.shipment_type, c.order_availability, c.product_id
Without knowing your table structure and data it's hard for us to tell.
But you could do something like this:
UPDATE table_Orders c SET order_type = 'PE'
WHERE (c.order_id, c.order_date, c.cust_id, c.shipment_type, c.order_availability, c.product_id) IN
(
SELECT A.order_id, A.order_date, A.cust_id, A.shipment_type, A.order_availability, A.product_id
FROM
table_Orders A,
table_Orders b,
(SELECT DISTINCT order_id, cust_id, order_date FROM table_Orders WHERE shipment_type = 'T' AND order_availability = 'P' AND order_amt > 500
AND cust_category = 'PREMIUM' ) d
WHERE
A.order_id = d.order_id AND A.order_date = d.order_date AND A.cust_id= d.cust_id
AND A.order_id = b.order_id AND A.order_date = b.order_date AND A.cust_id= b.cust_id
AND A.shipment_type = b.shipment_type AND A.order_availability = b.order_availability AND A.product_id <> b.product_id
AND ((A.order_amt > 500 AND b.shipment_amt > 50) OR (A.shipment_amt > 50 AND b.order_amt > 500))
);
COMMIT;
BTW, why don't you use the "JOIN Syntax" ?
UPDATE The same solution but with ANSI join syntax:
UPDATE table_Orders c
SET order_type = 'PE'
WHERE (c.order_id, c.order_date, c.cust_id, c.shipment_type, c.order_availability, c.product_id) IN
(
SELECT A.order_id, A.order_date, A.cust_id, A.shipment_type, A.order_availability, A.product_id
FROM
table_Orders A
JOIN
table_Orders b ON A.order_id = b.order_id AND A.order_date = b.order_date AND A.cust_id= b.cust_id AND A.shipment_type = b.shipment_type AND A.order_availability = b.order_availability AND A.product_id <> b.product_id
JOIN
(SELECT DISTINCT order_id, cust_id, order_date FROM table_Orders WHERE shipment_type = 'T' AND order_availability = 'P' AND order_amt > 500
AND cust_category = 'PREMIUM' ) d ON A.order_id = d.order_id AND A.order_date = d.order_date AND A.cust_id= d.cust_id
WHERE ((A.order_amt > 500 AND b.shipment_amt > 50) OR (A.shipment_amt > 50 AND b.order_amt > 500))
);

Related

How to write clickhouse SQL correctly?

SQL can be execute on Oracle, but not on clickhouse:
SELECT *
FROM PART, PARTSUPP
WHERE P_PARTKEY = PS_PARTKEY
AND PS_SUPPLYCOST = (
SELECT MIN(PS_SUPPLYCOST)
FROM PARTSUPP
WHERE P_PARTKEY = PS_PARTKEY
)
Execption:
Missing columns: 'P_PARTKEY' while processing query: 'SELECT min(PS_SUPPLYCOST)...
any help will be appreciated.
thank you.
correlated subquery SQL:
SELECT
*
FROM
(
SELECT
S_ACCTBAL,
S_NAME,
N_NAME,
P_PARTKEY,
P_MFGR ,
S_ADDRESS,
S_PHONE,
S_COMMENT
FROM
PART,
SUPPLIER,
PARTSUPP,
NATION,
REGION
WHERE
P_PARTKEY = PS_PARTKEY
AND S_SUPPKEY = PS_SUPPKEY
AND P_SIZE = 25
AND P_TYPE LIKE '%COPPER'
AND S_NATIONKEY = N_NATIONKEY
AND N_REGIONKEY = R_REGIONKEY
AND R_NAME = 'ASIA'
AND PS_SUPPLYCOST = (
SELECT
MIN(PS_SUPPLYCOST)
FROM
PARTSUPP,
SUPPLIER,
NATION,
REGION
WHERE
P_PARTKEY = PS_PARTKEY
AND S_SUPPKEY = PS_SUPPKEY
AND S_NATIONKEY = N_NATIONKEY
AND N_REGIONKEY = R_REGIONKEY
AND R_NAME = 'ASIA' )
ORDER BY
S_ACCTBAL DESC,
N_NAME,
S_NAME,
P_PARTKEY )
WHERE
ROWNUM <= 100;
for Clickhouse:
SELECT
*
from
(
SELECT
s.S_ACCTBAL AS S_ACCTBAL,
s.S_NAME AS S_NAME,
n.N_NAME AS N_NAME,
p.P_PARTKEY AS P_PARTKEY,
p.P_MFGR AS P_MFGR,
s.S_ADDRESS AS S_ADDRESS,
s.S_PHONE AS S_PHONE,
s.S_COMMENT AS S_COMMENT
FROM
PART AS p,
PARTSUPP AS ps,
SUPPLIER AS s,
NATION AS n,
REGION AS r,
(
SELECT
P_PARTKEY,
MIN(PS_SUPPLYCOST) AS PS_SUPPLYCOST
FROM
PARTSUPP,
PART,
SUPPLIER,
NATION,
REGION
WHERE
P_PARTKEY = PS_PARTKEY
AND S_SUPPKEY = PS_SUPPKEY
AND S_NATIONKEY = N_NATIONKEY
AND N_REGIONKEY = R_REGIONKEY
AND R_NAME = 'ASIA'
GROUP BY
P_PARTKEY) pps
WHERE
p.P_PARTKEY = pps.P_PARTKEY
AND ps.PS_SUPPLYCOST = pps.PS_SUPPLYCOST
AND p.P_PARTKEY = ps.PS_PARTKEY
AND s.S_SUPPKEY = ps.PS_SUPPKEY
AND p.P_SIZE = 25
AND p.P_TYPE LIKE '%COPPER'
AND s.S_NATIONKEY = n.N_NATIONKEY
AND n.N_REGIONKEY = r.R_REGIONKEY
AND r.R_NAME = 'ASIA')
ORDER BY
S_ACCTBAL DESC,
N_NAME,
S_NAME,
P_PARTKEY
LIMIT 100;

when i pass emplid it runs but when i pass campus_id it is taking infinite time

When I pass emplid it runs but when I pass campus_id it is taking infinite time
WITH aa
AS (
SELECT DISTINCT emplid
,strm
,catalog_nbr
,COUNT(ATTEND_PRESENT) OVER (
PARTITION BY emplid
,strm
,catalog_nbr
) AS present_days
FROM ps_SRM_ATT_2_VW
WHERE ATTEND_PRESENT = 'Y'
)
,bb
AS (
SELECT DISTINCT emplid
,strm
,catalog_nbr
,COUNT(ATTEND_PRESENT) OVER (
PARTITION BY emplid
,strm
,catalog_nbr
) AS total_days
FROM ps_SRM_ATT_2_VW
)
,cc
AS (
SELECT DISTINCT emplid
,strm
,catalog_nbr
,nvl(sum(days) OVER (
PARTITION BY emplid
,strm
,catalog_nbr
), 0) AS relax_days
FROM PS_SRM_RELAX_TBL
)
,kk
AS (
SELECT DISTINCT emplid
,strm
,days
,CATALOG_NBR
FROM ps_srm_relax_tbl
)
,hh
AS (
SELECT DISTINCT a.emplid
,b.strm
,a.campus_id
,b.CATALOG_NBR
FROM ps_personal_data a
,ps_SRM_ATT_2_VW b
WHERE a.emplid = b.emplid
ORDER BY a.emplid
)
SELECT DISTINCT AA.emplid
,gg.name_display
,GG.campus_id
,nvl(kk.days, 0) AS relax_lectures
,(
CASE
WHEN tt.ssr_component = 'LEC'
OR tt.ssr_component = 'TUT'
THEN 'Theory'
ELSE 'Practical'
END
) AS component1
,nvl(round((
(
nvl(aa.present_days, 0) + nvl((
SELECT relax_days
FROM cc
WHERE emplid = aa.emplid
AND strm = aa.strm
AND catalog_nbr = aa.catalog_nbr
), 0)
) / nvl(bb.total_days, 1)
) * 100, 2), 0) AS total
,dd.acad_career
,dd.acad_prog
,ee.acad_plan
,aa.strm
,ff.ACAD_LEVEL_BOT
,bb.total_days AS total_lecture
,aa.present_days AS Lecture_attend
,pp.subject || ' ' || pp.catalog_nbr AS SubjectCatalog
,tt.COURSE_TITLE_LONG
FROM aa
LEFT JOIN cc ON aa.emplid = cc.emplid
AND aa.strm = cc.strm
AND aa.catalog_nbr = cc.catalog_nbr
Here is the ps_personal_data gg and the campus id prompt that causing problem is associated with this
LEFT JOIN ps_personal_data gg ON AA.EMPLID = GG.EMPLID
LEFT JOIN hh ON aa.emplid = hh.emplid
AND aa.strm = hh.strm
AND aa.catalog_nbr = hh.catalog_nbr
LEFT JOIN kk ON cc.emplid = kk.emplid
AND cc.strm = kk.strm
AND cc.catalog_nbr = kk.catalog_nbr
LEFT JOIN ps_crse_offer pp ON aa.CATALOG_NBR = pp.catalog_nbr
LEFT JOIN ps_crse_catalog tt ON pp.crse_id = tt.crse_id
,bb
,PS_ACAD_PROG dd
,PS_ACAD_PLAN ee
,PS_STDNT_CAR_TERM ff
WHERE dd.effdt = (
SELECT max(effdt)
FROM ps_acad_prog
WHERE emplid = dd.emplid
)
AND dd.effseq = (
SELECT max(effseq)
FROM ps_acad_prog
WHERE emplid = dd.emplid
AND effdt = dd.effdt
)
AND ee.effdt = (
SELECT max(effdt)
FROM ps_acad_prog
WHERE emplid = ee.emplid
)
AND ee.effseq = (
SELECT max(effseq)
FROM ps_acad_prog
WHERE emplid = ee.emplid
AND effdt = ee.effdt
)
AND TT.EFFDT = (
SELECT max(effdt)
FROM ps_CRSE_CATALOG
WHERE CRSE_ID = PP.CRSE_ID
)
AND dd.emplid = aa.emplid
AND aa.emplid = hh.emplid
AND ee.emplid = aa.emplid
AND aa.emplid = ff.emplid
AND aa.emplid = ee.emplid
AND aa.emplid = bb.emplid
AND
--GG.CAMPUS_ID=HH.CAMPUS_ID AND
aa.catalog_nbr = hh.catalog_nbr
AND aa.strm = bb.strm
AND tt.eff_status = 'A'
AND aa.catalog_nbr = bb.catalog_nbr
AND aa.strm = ff.strm
AND tt.effdt = (
SELECT max(effdt)
FROM ps_crse_catalog
WHERE crse_id = pp.crse_id
)
AND AA.STRM = '1901'
AND
--AA.EMPLID='00000009724';
If I hide the gg.campus_id and pass aa.emplid it will run smoothly within 3-4 seconds.
Here is the prompt when I give campus id it is taking infinite time
GG.CAMPUS_id = : 1;

How to assign aliases to multiple select statements

My issue is the right join is not working, I am guessing due to aliases used.
I have commented the lines using -- to indicate the lines that most probably are causing the issue. If I run my Cross Join part of the code separately it works fine.
If I add the cross join as show below, and then do a right join with the table final_leg a I am receiving an error saying 'relation "k" does not exist'.
k is the alias I am using in the last select statement to call the CROSS join part of the table.
WITH final_leg AS(
SELECT y.*
FROM
(
SELECT
y.shipment_id,
y.route_id,
max(leg_sequence_id) max_leg_sequence_id
FROM posimorders.sc_execution_eu.o_detailed_routes_v2 y
group by
1,2
) AS x
INNER JOIN posimorders.sc_execution_eu.o_detailed_routes_v2 y
on x.route_id = y.route_id and x.shipment_id = y.shipment_id and y.leg_sequence_id = x.max_leg_sequence_id
),
dest_leg AS(
SELECT y.*
FROM
(
SELECT
y.shipment_id,
y.route_id,
min(leg_sequence_id) max_leg_sequence_id
FROM
posimorders.sc_execution_eu.o_detailed_routes_v2 y
LEFT JOIN warehouse_attributes w -- Joining to add origin country of origin FC
ON w.warehouse_id = y.leg_warehouse_id
group by
1,2
) x
INNER JOIN posimorders.sc_execution_eu.o_detailed_routes_v2 y
on x.route_id = y.route_id and x.shipment_id = y.shipment_id and y.leg_sequence_id = x.max_leg_sequence_id
),
list_legs_ds AS(
SELECT t1.*, t2.* FROM
(SELECT leg_warehouse_id, SUM(pck_count) AS total_packages
FROM posimorders.sc_execution_eu.o_detailed_routes_v2
WHERE trunc(cpt_date) between '2019-06-16' and '2019-06-22'
and leg_sequence_id = 0
and leg_warehouse_id not like 'X%'
and right(leg_warehouse_id,1) in ('1','2','3','4','5','6','7','8','9') --Only SC and not Airports
group by 1
having sum(pck_count) > 50000
) t1
CROSS JOIN
(select distinct leg_warehouse_id AS lm_ds , destination_country_code
from posimorders.sc_execution_eu.o_detailed_routes_v2
where trunc(cpt_date) BETWEEN '2019-06-16' and '2019-06-22'
and leg_ship_method LIKE 'AMZN_%_PRIME'
) t2
)
SELECT
a.route_warehouse_id,
--k.leg_warehouse_id leg_ware,
--k.leg_warehouse_id lm_ds,
from
final_leg a
inner join dest_leg b
on a.shipment_id = b.shipment_id and a.route_id = b.route_id
--RIGHT JOIN list_legs_ds k
--on a.leg_warehouse_id = k.leg_ware -- and a.leg_ship_method = k.last_ds
The issue is most likely because you haven't aliased the columns in your list_legs_ds subquery, meaning you have at least two columns with the same name.
This is a good example of why you shouldn't, in general, use select *... and why you should be explicit in the columns you're selecting. Try the following instead:
WITH final_leg AS
(SELECT y.*
FROM (SELECT y.shipment_id,
y.route_id,
MAX(leg_sequence_id) max_leg_sequence_id
FROM posimorders.sc_execution_eu.o_detailed_routes_v2 y
GROUP BY 1,
2) AS x
INNER JOIN posimorders.sc_execution_eu.o_detailed_routes_v2 y
ON x.route_id = y.route_id
AND x.shipment_id = y.shipment_id
AND y.leg_sequence_id = x.max_leg_sequence_id),
dest_leg AS
(SELECT y.*
FROM (SELECT y.shipment_id,
y.route_id,
MIN(leg_sequence_id) max_leg_sequence_id
FROM posimorders.sc_execution_eu.o_detailed_routes_v2 y
LEFT JOIN warehouse_attributes w -- Joining to add origin country of origin FC
ON w.warehouse_id = y.leg_warehouse_id
GROUP BY 1,
2) x
INNER JOIN posimorders.sc_execution_eu.o_detailed_routes_v2 y
ON x.route_id = y.route_id
AND x.shipment_id = y.shipment_id
AND y.leg_sequence_id = x.max_leg_sequence_id),
list_legs_ds AS
(SELECT t1.leg_warehouse_id AS leg_ware,
t1.total_packages,
t2.leg_warehouse_id AS last_ds,
t2.destination_country_code
FROM (SELECT leg_warehouse_id AS leg_ware,
SUM(pck_count) AS total_packages
FROM posimorders.sc_execution_eu.o_detailed_routes_v2
GROUP BY 1
HAVING SUM(pck_count) > 50000) t1
CROSS JOIN (SELECT DISTINCT leg_warehouse_id AS last_ds,
destination_country_code
FROM posimorders.sc_execution_eu.o_detailed_routes_v2) t2)
SELECT a.route_warehouse_id,
k.leg_ware,
k.last_ds lm_ds
-- should there be something to aggregate here?
FROM final_leg a
INNER JOIN dest_leg b
ON a.shipment_id = b.shipment_id
AND a.route_id = b.route_id
RIGHT JOIN list_legs_ds k
ON a.leg_warehouse_id = k.leg_ware -- and a.leg_ship_method = k.lm_ds
GROUP BY 1,
2,
3;

I want to add a subquery with a date parameter to return a calculated value but my query is not working

I have a query that fetches data from oracle inventory and purchasing. Now I want to add a subquery from po_line_locations_all with a date parameter to return a calculated value as if the value lies in the subquery show it else display zero, But in my case the query shows nothing if subquery returns null.
I want to show the result of the main query even if subquery returns null.
SELECT distinct msib.segment1 Item_Code,
MSIB.inventory_item_id,
MSIB.organization_id ORG_ID,
msib.description Item_Description,
msib.primary_unit_of_measure UOM,
ph.attribute1 Item_Type,
SUM(plla.quantity) - SUM(plla.quantity_received) On_Order,
ph.currency_code currency,
max(pl.unit_price) FOB_Value_in_FCY,
max(ph.rate_date),
max(ph.rate) forex_rate,
(
SELECT SUM (moq.transaction_quantity)
FROM mtl_system_items_b msi, mtl_onhand_quantities moq
WHERE moq.organization_id(+) = msi.organization_id
AND moq.inventory_item_id(+) = msi.inventory_item_id
and moq.ORGANIZATION_ID =MSIB.organization_id
and msi.inventory_item_id = MSIB.inventory_item_id
GROUP BY msi.segment1, msi.organization_id
) CURR_STOCK,
(
SELECT NVL(ABS(sum(mtmt.transaction_quantity)),0) from MTL_MATERIAL_TRANSACTIONS mtmt
WHERE 1=1
AND mtmt.inventory_item_id = MSIB.inventory_item_id --4018
AND mtmt.organization_id = MSIB.organization_id--499
and mtmt.TRANSACTION_ACTION_ID NOT IN (24, 30)
AND to_date(mtmt.transaction_date) >= to_date(&Roll_back_date,'DD-MON-RRRR')
)RB_TRANSACTIONS,
(
select ABS(SUM(mmt.transaction_quantity))
from MTL_MATERIAL_TRANSACTIONS mmt
where mmt.TRANSACTION_ACTION_ID NOT IN (24, 30)
and (mmt.ORGANIZATION_ID = MSIB.organization_id --499--579
)
and (mmt.INVENTORY_ITEM_ID = MSIB.inventory_item_id --4128 --4165
and mmt.TRANSACTION_TYPE_ID in (33, 52)
)
and (mmt.transaction_date between
to_date(add_months(&CONS_f_DATE, -12),'DD-MON-YYYY')
AND to_date(&CONS_f_DATE, 'DD-MON-YYYY')
)
AND (mmt.parent_transaction_id IS NULL)
) annual_Consumption,
(
select ABS(SUM(mmt.transaction_quantity) / 4)
FROM MTL_MATERIAL_TRANSACTIONS mmt
WHERE mmt.TRANSACTION_ACTION_ID NOT IN (24, 30)
and (mmt.ORGANIZATION_ID = MSIB.organization_id --499--579
)
and (mmt.INVENTORY_ITEM_ID = MSIB.inventory_item_id --4128 --4165
AND mmt.TRANSACTION_TYPE_ID in (33, 52)
)
and (mmt.transaction_date between
to_date(add_months(&CONS_f_DATE, -12),
'DD-MON-YYYY') AND
to_date(&CONS_f_DATE, 'DD-MON-YYYY'))
AND (mmt.parent_transaction_id IS NULL)
) months_Consumption,
(
select ABS((SUM(mmt.transaction_quantity) / 4) / 3)
FROM MTL_MATERIAL_TRANSACTIONS mmt
WHERE mmt.TRANSACTION_ACTION_ID NOT IN (24, 30)
and (mmt.ORGANIZATION_ID = MSIB.organization_id --499--579
)
and (mmt.INVENTORY_ITEM_ID = MSIB.inventory_item_id --4128 --4165
AND mmt.TRANSACTION_TYPE_ID in (33, 52))
and (mmt.transaction_date between
to_date(add_months(&CONS_f_DATE, -12),
'DD-MON-YYYY') AND
to_date(&CONS_f_DATE, 'DD-MON-YYYY'))
AND (mmt.parent_transaction_id IS NULL)
) monthly_Average,
(
select MATERIAL_COST
FROM CST_ITEM_COST_TYPE_V vw
WHERE vw.organization_id = MSIB.organization_id
AND - 1 = -1
and (vw.INVENTORY_ITEM_ID = MSIB.inventory_item_id)
) Unit_Cost, --new
sum(quan.t_quantity) - sum(r_quantity) finala
FROM mtl_system_items_b MSIB,
PO_HEADERS_ALL ph,
Po_Lines_All pl,
PO_LINE_LOCATIONS_ALL PLLA,
-------------------SUBQUERY---------------------------------------
(select nvl(sum(subplla.quantity),0) t_quantity, nvl(sum(subplla.quantity_received),0) r_quantity ,subpl.item_id
from po_headers_all subph,
po_lines_all subpl,
po_line_locations_all subplla
where subph.po_header_id = subpl.po_header_id
and subplla.po_header_id = subph.po_header_id
and subpl.po_line_id = subplla.po_line_id
and subplla.org_id = subpl.org_id
and to_date(subplla.creation_date) >= to_date(&Roll_back_date,'DD-MON-RRRR')
group by subph.attribute1, subph.currency_code, subpl.item_id
) quan
-------------------SUBQUERY---------------------------------------
WHERE 1=1
and ph.po_header_id = pl.po_header_id
and msib.inventory_item_id (+) = pl.item_id
and pl.item_id (+) = quan.item_id
and plla.po_header_id = ph.po_header_id
and pl.po_line_id = plla.po_line_id
and plla.org_id = pl.org_id
and msib.organization_id in
(select haou.organization_id
from hr_organization_information hoi,
hr_all_organization_units haou
where haou.organization_id = hoi.organization_id
and hoi.org_information1 = 'INV'
and hoi.org_information2 = 'Y'
and haou.name like '%HEIS%')
and MSIB.Inventory_Item_Id=NVL(&ITEM,MSIB.Inventory_Item_Id)
and MSIB.organization_id = nvl(&P_ORGI, MSIB.organization_id)
AND to_date(plla.creation_date) BETWEEN
to_date(add_months(&Roll_back_date, -12),'DD-MON-YYYY') AND
to_date(&Roll_back_date,'DD-MON-YYYY')
GROUP BY msib.segment1,
MSIB.inventory_item_id,
msib.description,
MSIB.organization_id,
msib.primary_unit_of_measure,
ph.attribute1,
ph.currency_code
My guess is that your problem is simply using old-syntax joins instead of something that has been around for a really long time.
SELECT DISTINCT msib.segment1 Item_Code,
MSIB.inventory_item_id,
MSIB.organization_id ORG_ID,
msib.description Item_Description,
msib.primary_unit_of_measure UOM,
ph.attribute1 Item_Type,
SUM(plla.quantity) - SUM(plla.quantity_received) On_Order,
ph.currency_code currency,
max(pl.unit_price) FOB_Value_in_FCY,
max(ph.rate_date),
max(ph.rate) forex_rate,
(
SELECT SUM(moq.transaction_quantity)
FROM mtl_system_items_b msi
RIGHT JOIN mtl_onhand_quantities moq ON moq.organization_id = msi.organization_id
AND moq.inventory_item_id = msi.inventory_item_id
WHERE moq.ORGANIZATION_ID = MSIB.organization_id
AND msi.inventory_item_id = MSIB.inventory_item_id
GROUP BY msi.segment1,
msi.organization_id
) CURR_STOCK,
(
SELECT NVL(ABS(sum(mtmt.transaction_quantity)), 0)
FROM MTL_MATERIAL_TRANSACTIONS mtmt
WHERE 1 = 1
AND mtmt.inventory_item_id = MSIB.inventory_item_id --4018
AND mtmt.organization_id = MSIB.organization_id --499
AND mtmt.TRANSACTION_ACTION_ID NOT IN (24,30)
AND to_date(mtmt.transaction_date) >= to_date(&Roll_back_date, 'DD-MON-RRRR')
) RB_TRANSACTIONS,
mmt.annual_Consumption annual_Consumption,
mmt.annual_Consumption / 4 months_Consumption,
mmt.annual_Consumption / 12 monthly_Average,
(
SELECT MATERIAL_COST
FROM CST_ITEM_COST_TYPE_V vw
WHERE vw.organization_id = MSIB.organization_id
AND vw.INVENTORY_ITEM_ID = MSIB.inventory_item_id
) Unit_Cost, --new
sum(quan.t_quantity) - sum(r_quantity) finala
FROM mtl_system_items_b MSIB
LEFT JOIN PO_HEADERS_ALL ph ON msib.inventory_item_id = pl.item_id
INNER JOIN Po_Lines_All pl ON ph.po_header_id = pl.po_header_id
INNER JOIN PO_LINE_LOCATIONS_ALL PLLA ON plla.po_header_id = ph.po_header_id AND pl.po_line_id = plla.po_line_id AND plla.org_id = pl.org_id
LEFT JOIN
-------------------SUBQUERY---------------------------------------
(
SELECT nvl(sum(subplla.quantity), 0) t_quantity,
nvl(sum(subplla.quantity_received), 0) r_quantity,
subpl.item_id
FROM po_headers_all subph
INNER JOIN po_lines_all subpl ON subph.po_header_id = subpl.po_header_id
INNER JOIN po_line_locations_all subplla ON subplla.po_header_id = subph.po_header_id
AND subpl.po_line_id = subplla.po_line_id
AND subplla.org_id = subpl.org_id
WHERE to_date(subplla.creation_date) >= to_date(&Roll_back_date, 'DD-MON-RRRR')
GROUP BY subph.attribute1,
subph.currency_code,
subpl.item_id
) quan ON pl.item_id = quan.item_id
-------------------SUBQUERY---------------------------------------
LEFT JOIN (
SELECT mmt.ORGANIZATION_ID,
mmt.INVENTORY_ITEM_ID,
ABS(SUM(mmt.transaction_quantity)) AS annual_Consumption
FROM MTL_MATERIAL_TRANSACTIONS mmt
WHERE mmt.TRANSACTION_ACTION_ID NOT IN (24,30)
AND mmt.TRANSACTION_TYPE_ID IN (33,52)
AND mmt.transaction_date BETWEEN to_date(add_months(&CONS_f_DATE, - 12), 'DD-MON-YYYY')
AND to_date(&CONS_f_DATE, 'DD-MON-YYYY')
AND mmt.parent_transaction_id IS NULL
) mmt ON mmt.ORGANIZATION_ID = MSIB.organization_id --499--579
AND mmt.INVENTORY_ITEM_ID = MSIB.inventory_item_id --4128 --4165
WHERE msib.organization_id IN (
SELECT haou.organization_id
FROM hr_organization_information hoi
JOIN hr_all_organization_units haou ON haou.organization_id = hoi.organization_id
WHERE hoi.org_information1 = 'INV'
AND hoi.org_information2 = 'Y'
AND haou.name LIKE '%HEIS%'
)
AND MSIB.Inventory_Item_Id = NVL(&ITEM, MSIB.Inventory_Item_Id)
AND MSIB.organization_id = nvl(&P_ORGI, MSIB.organization_id)
AND to_date(plla.creation_date) BETWEEN to_date(add_months(&Roll_back_date, - 12), 'DD-MON-YYYY')
AND to_date(&Roll_back_date, 'DD-MON-YYYY')
GROUP BY msib.segment1,
MSIB.inventory_item_id,
msib.description,
MSIB.organization_id,
msib.primary_unit_of_measure,
ph.attribute1,
ph.currency_code;
Here is the query in its simplest form, The main query is working good without the subquery. But when subquery returns null (i.e. no output in the date range) the whole query returns nothing. I want it to display results regardless the subquery results. My guess is something is wrong with the JOIN –
SELECT distinct msib.segment1 Item_Code,
MSIB.inventory_item_id,
MSIB.organization_id ORG_ID,
msib.description Item_Description,
msib.primary_unit_of_measure UOM,
ph.attribute1 Item_Type,
ph.currency_code currency,
max(pl.unit_price) FOB_Value_in_FCY,
max(ph.rate_date),
max(ph.rate) forex_rate,
sum(quan.t_quantity) - sum(r_quantity) finala
FROM mtl_system_items_b MSIB,
PO_HEADERS_ALL ph,
Po_Lines_All pl,
PO_LINE_LOCATIONS_ALL PLLA,
------------SUBQUERY-------------------------------
(select nvl(sum(subplla.quantity),0) t_quantity, nvl(sum(subplla.quantity_received),0) r_quantity ,subpl.item_id
from po_headers_all subph,
po_lines_all subpl,
po_line_locations_all subplla
where subph.po_header_id = subpl.po_header_id
and subplla.po_header_id = subph.po_header_id
and subpl.po_line_id = subplla.po_line_id
and subplla.org_id = subpl.org_id
and to_date(subplla.creation_date) >= to_date(&Roll_back_date,'DD-MON-RRRR')
group by subph.attribute1, subph.currency_code, subpl.item_id
) quan
------------SUBQUERY-------------------------------
WHERE 1=1
and ph.po_header_id = pl.po_header_id
and msib.inventory_item_id (+) = pl.item_id
-----------------joining subquery-------------------
and pl.item_id (+) = quan.item_id
-----------------joining subquery-------------------
and plla.po_header_id = ph.po_header_id
and pl.po_line_id = plla.po_line_id
and plla.org_id = pl.org_id
and msib.organization_id in
(select haou.organization_id
from hr_organization_information hoi,
hr_all_organization_units haou
where haou.organization_id = hoi.organization_id
and hoi.org_information1 = 'INV'
and hoi.org_information2 = 'Y'
and haou.name like '%HEIS%')
and MSIB.Inventory_Item_Id=NVL(&ITEM,MSIB.Inventory_Item_Id)
and MSIB.organization_id = nvl(&P_ORGI, MSIB.organization_id)
AND to_date(plla.creation_date) BETWEEN
to_date(add_months(&Roll_back_date, -12),'DD-MON-YYYY') AND
to_date(&Roll_back_date,'DD-MON-YYYY')
GROUP BY msib.segment1,
MSIB.inventory_item_id,
msib.description,
MSIB.organization_id,
msib.primary_unit_of_measure,
ph.attribute1,
ph.currency_code

How can I join these two select queries into one query?

this is my first time posting, so I am sure I will get a number of things wrong. Do not hesitate to correct me and I will do everything I can to clarify.
In Oracle SQL Developer, I am trying to take two separate SELECT statements and combine them to get one row of results. Unfortunately, because this is sensitive data, I am unable to give any results from the statements individually, but instead, just the SQL statements themselves. I suspect I should be able to join these two on the field "emplid" but just cannot get there. Any help is greatly appreciated! Here is the code below, please mind the syntax :)
1st Select statement is giving me a list of people that were paid in 2017:
SELECT DISTINCT C.COMPANY,
C.EMPLID,
C.SSN
FROM PS_PAY_CHECK C
WHERE TO_CHAR(C.CHECK_DT,'YYYY') = '2017'
AND C.COMPANY IN ('001','054','076')
ORDER BY C.COMPANY, C.EMPLID
And 2nd Select statement would be a list of the deductions taken for the employees that were identified in the first statement:
SELECT G.EMPLID, G.DEDCD,
CASE
WHEN DC.DED_CLASS IN ('A','B','T')
THEN G.DED_ADDL_AMT
ELSE 0
END AS "EEAmt",
CASE
WHEN DC.DED_CLASS NOT IN ('A','B','T')
THEN G.DED_ADDL_AMT
ELSE 0
END AS "ERAmt",
DC.DED_CLASS,
G.DED_ADDL_AMT,
G.GOAL_AMT
FROM PS_GENL_DEDUCTION G,
PS_DED_CLASS_VW DC
WHERE G.EFFDT =
(SELECT MAX(G_ED.EFFDT)
FROM PS_GENL_DEDUCTION G_ED
WHERE G.EMPLID = G_ED.EMPLID
AND G.COMPANY = G_ED.COMPANY
AND G.DEDCD = G_ED.DEDCD
AND G_ED.EFFDT <= SYSDATE
)
AND ( G.DEDUCTION_END_DT IS NULL
OR G.DEDUCTION_END_DT > SYSDATE)
AND ( G.GOAL_AMT = 0.00
OR G.GOAL_AMT <> G.GOAL_BAL)
AND G.DED_ADDL_AMT > 0
AND DC.PLAN_TYPE = '00'
AND DC.DEDCD = G.DEDCD
AND DC.EFFDT =
(SELECT MAX(V1.EFFDT)
FROM PS_DED_CLASS_VW V1
WHERE V1.PLAN_TYPE = DC.PLAN_TYPE
AND V1.DEDCD = DC.DEDCD
)
AND G.EMPLID = 'XXXXXX'
Ideally, what I'd like to do is put in a value in place of 'XXXXXX' and get one row of data with the two combined statements.
Thanks everyone!
To do this, we stick each SELECT statement into a subquery and give that subquery an alias to refer to in the main queries select statement:
SELECT t1.*, t2.*
FROM
(
SELECT DISTINCT C.COMPANY,
C.EMPLID,
C.SSN
FROM PS_PAY_CHECK C
WHERE TO_CHAR(C.CHECK_DT,'YYYY') = '2017'
AND C.COMPANY IN ('001','054','076')
ORDER BY C.COMPANY, C.EMPLID
) t1
INNER JOIN
(
SELECT G.EMPLID, G.DEDCD,
CASE
WHEN DC.DED_CLASS IN ('A','B','T')
THEN G.DED_ADDL_AMT
ELSE 0
END AS "EEAmt",
CASE
WHEN DC.DED_CLASS NOT IN ('A','B','T')
THEN G.DED_ADDL_AMT
ELSE 0
END AS "ERAmt",
DC.DED_CLASS,
G.DED_ADDL_AMT,
G.GOAL_AMT
FROM PS_GENL_DEDUCTION G,
PS_DED_CLASS_VW DC
WHERE G.EFFDT =
(SELECT MAX(G_ED.EFFDT)
FROM PS_GENL_DEDUCTION G_ED
WHERE G.EMPLID = G_ED.EMPLID
AND G.COMPANY = G_ED.COMPANY
AND G.DEDCD = G_ED.DEDCD
AND G_ED.EFFDT <= SYSDATE
)
AND ( G.DEDUCTION_END_DT IS NULL
OR G.DEDUCTION_END_DT > SYSDATE)
AND ( G.GOAL_AMT = 0.00
OR G.GOAL_AMT <> G.GOAL_BAL)
AND G.DED_ADDL_AMT > 0
AND DC.PLAN_TYPE = '00'
AND DC.DEDCD = G.DEDCD
AND DC.EFFDT =
(SELECT MAX(V1.EFFDT)
FROM PS_DED_CLASS_VW V1
WHERE V1.PLAN_TYPE = DC.PLAN_TYPE
AND V1.DEDCD = DC.DEDCD
)
AND G.EMPLID = 'XXXXXX'
) t2 ON
t1.empid = t2.empid
We just treat each derived table/subquery as it's own table and join them on empid. You can tweak the SELECT statement at the top as needed.
This is similar to creating a view for both sql statements and then referencing the views in a third sql statement.
An alternative way of doing this is to use CTE (Common Table Expressions) to house the separate sql. There's no performance advantage here, but you might find it easier to read.
WITH t1 as
(
SELECT DISTINCT C.COMPANY,
C.EMPLID,
C.SSN
FROM PS_PAY_CHECK C
WHERE TO_CHAR(C.CHECK_DT,'YYYY') = '2017'
AND C.COMPANY IN ('001','054','076')
ORDER BY C.COMPANY, C.EMPLID
),
t2 AS
(
SELECT G.EMPLID, G.DEDCD,
CASE
WHEN DC.DED_CLASS IN ('A','B','T')
THEN G.DED_ADDL_AMT
ELSE 0
END AS "EEAmt",
CASE
WHEN DC.DED_CLASS NOT IN ('A','B','T')
THEN G.DED_ADDL_AMT
ELSE 0
END AS "ERAmt",
DC.DED_CLASS,
G.DED_ADDL_AMT,
G.GOAL_AMT
FROM PS_GENL_DEDUCTION G,
PS_DED_CLASS_VW DC
WHERE G.EFFDT =
(SELECT MAX(G_ED.EFFDT)
FROM PS_GENL_DEDUCTION G_ED
WHERE G.EMPLID = G_ED.EMPLID
AND G.COMPANY = G_ED.COMPANY
AND G.DEDCD = G_ED.DEDCD
AND G_ED.EFFDT <= SYSDATE
)
AND ( G.DEDUCTION_END_DT IS NULL
OR G.DEDUCTION_END_DT > SYSDATE)
AND ( G.GOAL_AMT = 0.00
OR G.GOAL_AMT <> G.GOAL_BAL)
AND G.DED_ADDL_AMT > 0
AND DC.PLAN_TYPE = '00'
AND DC.DEDCD = G.DEDCD
AND DC.EFFDT =
(SELECT MAX(V1.EFFDT)
FROM PS_DED_CLASS_VW V1
WHERE V1.PLAN_TYPE = DC.PLAN_TYPE
AND V1.DEDCD = DC.DEDCD
)
AND G.EMPLID = 'XXXXXX'
)
SELECT t1.*, t2.*
FROM t1 INNER JOIN t2 ON
t1.empid = t2.empid
Basically you do something like
select blah, blah.. (your second query )
AND G.EMPLID IN ( SELECT DISTINCT C.COMPANY,
C.EMPLID,
C.SSN
FROM PS_PAY_CHECK C
WHERE TO_CHAR(C.CHECK_DT,'YYYY') = '2017'
AND C.COMPANY IN ('001','054','076')
)
So basically I have used your first query in you second query. I removed the DISTINCT, as its not needed.
I hope that makes sense.

Resources