MariaDb does not support Subquerying, so i am trying to figure out the best method to take this query and turn it into a view. I am not 100% sure how to do this, as i know i need to create multiple views to do this, but i am uncertain on how to break this up.
I have been requested to create a view from this dataset and i was able to create this query with (subqueries), but now i need to turn it into a view, which i have hit a wall on, as i do not know where to start when trying to break this up into digestible pieces to create the multiple views to create the master view, which is this query.
SELECT
`TestDelete`.`CustomerInputName` AS `CustomerInputName`,
`TestDelete`.`customerInputCustid` AS `customerInputCustid`,
( CASE WHEN ( `TestDelete`.`YQ` = '2017Q1') and `TestDelete`.`customerInputCustid` in (select `portfolio_review_ACC1_2017Q1_REUP`.`customerInputCustid` from portfolio_review_ACC1_2017Q1_REUP)
THEN `TestDelete`.`Quarter`
WHEN ( `TestDelete`.`YQ` = '2017Q1' and `TestDelete`.`customerInputCustid` not in (select `portfolio_review_ACC1_2017Q1_REUP`.`customerInputCustid` from portfolio_review_ACC1_2017Q1_REUP)) or
( `TestDelete`.`YQ` = '2017Q2' and `TestDelete`.`customerInputCustid` not in (select `portfolio_review_ACC1_2017Q1_REUP`.`customerInputCustid` from portfolio_review_ACC1_2017Q1_REUP)) or
( `TestDelete`.`YQ` = '2017Q3' and `TestDelete`.`customerInputCustid` not in (select `portfolio_review_ACC1_2017Q1_REUP`.`customerInputCustid` from portfolio_review_ACC1_2017Q1_REUP)) or
( `TestDelete`.`YQ` = '2017Q4' and `TestDelete`.`customerInputCustid` not in (select `portfolio_review_ACC1_2017Q1_REUP`.`customerInputCustid` from portfolio_review_ACC1_2017Q1_REUP)) or
( `TestDelete`.`YQ` = '2018Q1' and `TestDelete`.`customerInputCustid` not in (select `portfolio_review_ACC1_2017Q1_REUP`.`customerInputCustid` from portfolio_review_ACC1_2017Q1_REUP)) or
( `TestDelete`.`YQ` = '2018Q2' and `TestDelete`.`customerInputCustid` not in (select `portfolio_review_ACC1_2017Q1_REUP`.`customerInputCustid` from portfolio_review_ACC1_2017Q1_REUP)) or
( `TestDelete`.`YQ` = '2018Q3' and `TestDelete`.`customerInputCustid` not in (select `portfolio_review_ACC1_2017Q1_REUP`.`customerInputCustid` from portfolio_review_ACC1_2017Q1_REUP)) or
( `TestDelete`.`YQ` = '2018Q4' and `TestDelete`.`customerInputCustid` not in (select `portfolio_review_ACC1_2017Q1_REUP`.`customerInputCustid` from portfolio_review_ACC1_2017Q1_REUP))
then 'First'
END ) AS `2017Q1`
Seems like this would be equivalent, but faster:
`TestDelete`.`YQ` IN ('2017Q2', '2017Q3', ...)
AND NOT EXISTS(
SELECT 1 FROM portfolio_review_ACC1_2017Q1_REUP
WHERE customerInputCustid = TestDelete.customerInputCustid
)
Meanwhile, your SELECT seems to be missing at least FROM customerInputCustid ??
Related
I have a query as follows:
INSERT ALL
WHEN NEWEST_ID IS NOT NULL AND
(SELECT COUNT(1) FROM (
SELECT *
FROM MY_TABLE
WHERE ID = NEWEST_ID
QUALIFY ROW_NUMBER() OVER (PARTITION BY ID ORDER BY OFFSET DESC) = 1
)
WHERE ACTIVE) = 0 THEN
INTO MY_TABLE VALUES(
NEWEST_ID,
CURRENT_DATE,
NAME,
FALSE
)
SELECT * FROM TEST_TABLE;
However I am getting an unsupported subquery type error when I try to write the select count(1) or count(*) from the subquery. Why is this so?/ How can I change this? In my subquery I am just trying to get the first row in a group of IDs after ordering by the descending offset. And then I am trying to determine whether the ACTIVE column from that result row is TRUE.
the QUALIFY can have the WHERE ACTIVE added to it:
SELECT COUNT(1)
FROM (
SELECT 1
FROM MY_TABLE as x
WHERE x.ID = NEWEST_ID
QUALIFY ROW_NUMBER() OVER (PARTITION BY x.ID ORDER BY x.OFFSET DESC) = 1 AND x.ACTIVE
)
this the inner only keeps the "last" offset per id AND if it is also active
the count = 0 can be turned into a NOT EXIST like:
INSERT ALL
WHEN newest_id IS NOT NULL
AND NOT EXISTS (
SELECT 1
FROM my_table AS x
WHERE x.id = newest_id
QUALIFY ROW_NUMBER() OVER (PARTITION BY x.id ORDER BY x.offset desc) = 1 AND x.active
) THEN
INTO my_table VALUES( newest_id, current_date, name, false)
SELECT * FROM TEST_TABLE;
"in theory"
the other option is to push that into a CTE:
WITH last_id_active AS (
SELECT x.id
FROM my_table AS x
QUALIFY
ROW_NUMBER() OVER (PARTITION BY x.id ORDER BY x.offset desc) = 1
AND x.active
)
which would need to be on the SELECT like:
INSERT ALL
WHEN tt.newest_id IS NOT NULL
AND lia.id IS NOT NULL THEN
INTO my_table VALUES( tt.newest_id, tt.current_date, tt.name, false)
WITH last_id_active AS (
SELECT x.id
FROM my_table AS x
QUALIFY
ROW_NUMBER() OVER (PARTITION BY x.id ORDER BY x.offset desc) = 1
AND x.active
)
SELECT * FROM TEST_TABLE as tt
LEFT JOIN last_id_active as lia
ON tt.newest_id = lia.id;
*based on theory
which could also be simplyfied, as lia.id will be null when tt.newest_id is also null, thus the INSERT_ALL could be simplefied more as:
INSERT ALL
WHEN lia.id IS NOT NULL THEN
INTO my_table VALUES( tt.newest_id, tt.current_date, tt.name, false)
WITH last_id_active AS (
SELECT x.id
FROM my_table AS x
QUALIFY
ROW_NUMBER() OVER (PARTITION BY x.id ORDER BY x.offset desc) = 1
AND x.active
)
SELECT * FROM TEST_TABLE as tt
LEFT JOIN last_id_active as lia
ON tt.newest_id = lia.id;
I've had this query running for over 2 years and just recently this began throwing the ORA-01427 single row sub-query returns multiple rows error. What's the best way to debug this when there are multiple sub-queries? Should I be adding a MAX statement to each subquery? I've tried switching some of the = operators to IN but that's not working and I'm not confident it would give me the correct results either.
Appreciate any insight or assistance this community can provide. I'm ripping my hair out working through this one!
SELECT
NVL(
(SELECT
'Y'
FROM
PER_ASSIGNMENT_SUPERVISORS_F
WHERE
MANAGER_ID = PAPF.PERSON_ID
AND MANAGER_TYPE ='LINE_MANAGER'
AND TRUNC(SYSDATE) BETWEEN EFFECTIVE_START_DATE AND EFFECTIVE_END_DATE
AND ROWNUM = 1),'N') MANAGER_FLAG,
PAAM.ASSIGNMENT_STATUS_TYPE,
PAAM.EMPLOYMENT_CATEGORY,
TO_CHAR(PPOS.DATE_START,'YYYY-MM-DD') AS HIRE_DATE,
PER_EXTRACT_UTILITY.GET_STANDARD_WORKING_HOURS(PAAM.ASSIGNMENT_ID,TRUNC(SYSDATE)) AS STANDARD_WORKING_HOURS,
TO_CHAR(PAAM.EFFECTIVE_START_DATE,'YYYY-MM-DD') AS EFFECTIVE_START_DATE,
(
SELECT
PJFVL.JOB_CODE
FROM
PER_ALL_ASSIGNMENTS_M PAAM1,
PER_JOBS_F_VL PJFVL
WHERE
PAAM1.PERSON_ID = PASF.MANAGER_ID
AND TRUNC(SYSDATE) BETWEEN PAAM1.EFFECTIVE_START_DATE AND PAAM1.EFFECTIVE_END_DATE
AND TRUNC(SYSDATE) BETWEEN PJFVL.EFFECTIVE_START_DATE AND PJFVL.EFFECTIVE_END_DATE
and PAAM1.ASSIGNMENT_STATUS_TYPE='ACTIVE'
AND PAAM1.ASSIGNMENT_TYPE = 'E'
AND PAAM1.effective_latest_change = 'Y'
AND PJFVL.JOB_ID = PAAM1.JOB_ID
)as Manager_job_code,
(
SELECT
HOIF.ORG_INFORMATION1
FROM
PER_ALL_ASSIGNMENTS_M PAAM2,
HR_ORGANIZATION_INFORMATION_F HOIF
WHERE
PAAM2.PERSON_ID = PASF.MANAGER_ID
AND HOIF.ORG_INFORMATION_CONTEXT = 'DEPT_DET'
and PAAM2.ASSIGNMENT_STATUS_TYPE='ACTIVE'
AND PAAM2.ASSIGNMENT_TYPE = 'E'
AND PAAM2.effective_latest_change = 'Y'
AND TRUNC(SYSDATE) BETWEEN PAAM2.EFFECTIVE_START_DATE AND PAAM2.EFFECTIVE_END_DATE
AND TRUNC(SYSDATE) BETWEEN HOIF.EFFECTIVE_START_DATE AND HOIF.EFFECTIVE_END_DATE
AND HOIF.ORGANIZATION_ID = PAAM2.ORGANIZATION_ID
) as Manager_dep_code,
(SELECT
COUNT(PPOS.PERIOD_OF_SERVICE_ID)
FROM
PER_PERIODS_OF_SERVICE PPOS
WHERE
1 = 1
AND PAAM.PERSON_ID = PPOS.PERSON_ID
) AS INACTIVE_WORKRELATIONSHIP,
PAPF.PERSON_NUMBER as SAMACCOUNTNAME,
(SELECT CSB.NAME FROM
CMP_SALARY CS,
CMP_SALARY_BASES CSB
WHERE
CS.ASSIGNMENT_ID = PAAM.ASSIGNMENT_ID
AND CS.SALARY_BASIS_ID = CSB.SALARY_BASIS_ID
and TRUNC(SYSDATE) BETWEEN CS.DATE_FROM AND CS.DATE_TO
) AS hourly_salary_Paid,
TO_CHAR(PP.DATE_OF_BIRTH,'YYYY-MM-DD') AS DOB,
PPNFV.LAST_NAME,
PPNFV.FIRST_NAME,
HLA.LOCATION_NAME as Location,
HLA.ADDRESS_LINE_1 AS LOC_ADDRESS_1,
HLA.ADDRESS_LINE_2 AS LOC_ADDRESS_2,
HLA.TOWN_OR_CITY AS City,
HLA.POSTAL_CODE AS ZIP_CODE,
HLA.REGION_2 AS STATE,
PPNFV.KNOWN_AS AS PREFERRED_NAME,
TRIM((PPNFV.KNOWN_AS||' '||PPNFV.LAST_NAME)) AS PREFERRED_NAME_LAST_NAME,
(SELECT
PPNFV.DISPLAY_NAME
FROM per_person_names_f_v PPNFV
WHERE 1 = 1
AND PPNFV.PERSON_ID = PASF.MANAGER_ID
AND PPNFV.NAME_TYPE='GLOBAL'
AND TRUNC(SYSDATE) BETWEEN PPNFV.EFFECTIVE_START_DATE AND PPNFV.EFFECTIVE_END_DATE
) AS MANAGER_NAME,
(SELECT
PAPF.PERSON_NUMBER
FROM PER_ALL_PEOPLE_F PAPF
WHERE PAPF.PERSON_ID = PASF.MANAGER_ID
AND TRUNC(SYSDATE) BETWEEN PAPF.EFFECTIVE_START_DATE AND PAPF.EFFECTIVE_END_DATE
) AS MANAGER_NUMBER,
HAOUFVL.NAME AS DEPARTMENT,
PAAM.ASSIGNMENT_NAME AS JOB_TILE,
PLE.NAME as Company,
PJLG.INFORMATION1 AS FLSA,
SUBSTR(PNI.NATIONAL_IDENTIFIER_NUMBER,-4) AS SSN_NUMBER,
PAAM.ASS_ATTRIBUTE1 as Officer_TITLE,
(
select
bu.name
from
hr_all_organization_units_f_vl bu
where 1 = 1
and paam.business_unit_id = bu.organization_id
and trunc(sysdate) between bu.effective_start_date and bu.effective_end_date
) as BUS_UNIT,
TO_CHAR (PPOS.ORIGINAL_DATE_OF_HIRE,'YYYY-MM-DD') AS ORIGINAL_DATE_OF_HIRE1,
(
CASE WHEN PPLF.SEX = 'F' THEN
'TRUE'
ELSE
'FALSE'
END
) AS GENDER,
(
CASE WHEN PJFFVL.JOB_FAMILY_NAME = 'Executive' THEN
'ELT'
ELSE
' '
END
) AS ELT_DESIGNATION,
HOIF.ORG_INFORMATION1 as DEPATMENT_CODE,
PJFV.JOB_CODE AS JOB_CODE,
PAF.ADDRESS_LINE_1 AS HOME_ADDRESS_LINE_1,
PAF.ADDRESS_LINE_2 AS HOME_ADDRESS_LINE_2,
PAF.TOWN_OR_CITY AS HOW_ADDRESS_CITY,
PAF.REGION_2 AS HOME_ADDRESS_STATE,
PAF.POSTAL_CODE AS HOME_ADRESS_ZIP_CODE,
PGFTL.NAME as Grade_level,
(SELECT
distinct (per_extract_utility.get_decoded_lookup('JOB_FUNCTION_CODE',PJF.JOB_FUNCTION_CODE))
FROM
PER_JOB_SECURED_LIST_V job WHERE TRUNC(SYSDATE) BETWEEN effective_start_date AND effective_end_date
) as JOB_FUNCTION,
pp.attribute1 as PER_NETWORKID,
(SELECT
PPNFV.attribute1
FROM PER_PERSONS PPNFV
WHERE 1 = 1
AND PPNFV.PERSON_ID = PASF.MANAGER_ID
) AS MANAGER_NETWORKID,
HOIF.ORG_INFORMATION2 AS REGION,
to_char(paam.ass_attribute_date1,'MM/DD/YYYY') as OfficerPromoDate
FROM
PER_ALL_ASSIGNMENTS_M PAAM,
PER_ASSIGNMENT_SUPERVISORS_F PASF,
PER_PERIODS_OF_SERVICE PPOS,
PER_ALL_PEOPLE_F PAPF,
PER_PERSONS PP,
per_person_names_f_v PPNFV,
HR_LOCATIONS_ALL HLA,
HR_ALL_ORGANIZATION_UNITS_F_VL HAOUFVL,
PER_JOB_LEG_F PJLG,
PER_NATIONAL_IDENTIFIERS PNI,
PER_PEOPLE_LEGISLATIVE_F PPLF,
PER_JOB_FAMILY_F_VL PJFFVL,
PER_JOBS_F_V PJFV,
HR_ORGANIZATION_INFORMATION_F HOIF,
PER_ADDRESSES_F PAF,
PER_PERSON_ADDR_USAGES_F PPAUF,
PER_GRADES_F_TL PGFTL,
PER_JOBS_F PJF,
PER_LEGAL_EMPLOYERS PLE,
HR_ALL_ORGANIZATION_UNITS_F HAOUF
WHERE
PAAM.ASSIGNMENT_STATUS_TYPE='ACTIVE'
AND PAAM.ASSIGNMENT_TYPE = 'E'
AND PAAM.effective_latest_change = 'Y'
AND PAAM.ASSIGNMENT_ID = PASF.ASSIGNMENT_ID(+)
AND PASF.MANAGER_TYPE (+) = 'LINE_MANAGER'
AND PNI.PERSON_ID(+) = PAAM.PERSON_ID
AND PAAM.PERSON_ID = PPLF.PERSON_ID(+)
and PJFV.JOB_FAMILY_ID= PJFFVL.JOB_FAMILY_ID(+)
AND PJFV.JOB_ID(+) = PAAM.JOB_ID
AND HOIF.ORGANIZATION_ID(+)= PAAM.ORGANIZATION_ID
AND HOIF.ORG_INFORMATION_CONTEXT(+) = 'DEPT_DETAILS'
AND PPAUF.PERSON_ID(+)= PAPF.PERSON_ID
AND PPAUF.ADDRESS_TYPE (+) = 'HOME'
AND PPAUF.ADDRESS_ID= PAF.ADDRESS_ID(+)
AND PGFTL.GRADE_ID(+)= PAAM.GRADE_ID
AND PJF.JOB_ID(+)= PAAM.JOB_ID
AND PJLG.INFORMATION_CATEGORY='HRX_US_JOBS'
and HAOUF.ORGANIZATION_ID(+)=PLE.ORGANIZATION_ID
and PAAM.LEGAL_ENTITY_ID=PLE.ORGANIZATION_ID
AND TRUNC(SYSDATE) BETWEEN PAAM.EFFECTIVE_START_DATE(+) AND PAAM.EFFECTIVE_END_DATE (+)
AND PAAM.PERIOD_OF_SERVICE_ID = PPOS.PERIOD_OF_SERVICE_ID
AND PAPF.PERSON_ID = PAAM.PERSON_ID
AND PP.PERSON_ID = PAAM.PERSON_ID
AND PPNFV.PERSON_ID = PAAM.PERSON_ID
AND HLA.LOCATION_ID(+) = PAAM.LOCATION_ID
AND PJLG.JOB_ID(+) = PAAM.JOB_ID
AND HAOUFVL.ORGANIZATION_ID(+) = PAAM.ORGANIZATION_ID
AND PPNFV.NAME_TYPE = 'GLOBAL'
AND TRUNC(SYSDATE) BETWEEN PPNFV.EFFECTIVE_START_DATE AND PPNFV.EFFECTIVE_END_DATE
AND TRUNC(SYSDATE) BETWEEN PAPF.EFFECTIVE_START_DATE AND PAPF.EFFECTIVE_END_DATE
AND TRUNC(SYSDATE) BETWEEN HAOUFVL.EFFECTIVE_START_DATE(+) AND HAOUFVL.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PPLF.EFFECTIVE_START_DATE(+) AND PPLF.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PJFFVL.EFFECTIVE_START_DATE(+) AND PJFFVL.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PJFV.EFFECTIVE_START_DATE(+) AND PJFV.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN HOIF.EFFECTIVE_START_DATE(+) AND HOIF.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PAF.EFFECTIVE_START_DATE(+) AND PAF.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PPAUF.EFFECTIVE_START_DATE(+) AND PPAUF.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PGFTL.EFFECTIVE_START_DATE(+) AND PGFTL.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PJF.EFFECTIVE_START_DATE(+) AND PJF.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PLE.EFFECTIVE_START_DATE AND PLE.EFFECTIVE_END_DATE
AND TRUNC(SYSDATE) BETWEEN HAOUF.EFFECTIVE_START_DATE(+) AND HAOUF.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN HLA.EFFECTIVE_START_DATE(+) AND HLA.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PJLG.EFFECTIVE_START_DATE(+) AND PJLG.EFFECTIVE_END_DATE(+)
AND TRUNC(SYSDATE) BETWEEN PASF.EFFECTIVE_START_DATE(+) AND PASF.EFFECTIVE_END_DATE(+)
ORDER BY PAPF.PERSON_NUMBER
Run the query in a SQL client that will generate a more detailed error message. For example, the below code is run in SQL*Plus, which correctly indicates that the problem is with the subquery on line 3.
SQL> select
2 (select 1 from dual) this_will_work,
3 (select 1 from dba_objects) this_will_fail,
4 (select 1 from dual) this_will_work
5 from dual;
(select 1 from dba_objects) this_will_fail,
*
ERROR at line 3:
ORA-01427: single-row subquery returns more than one row
I have a sp that is giving me wrong results. That sp returns a cursor which is missing some rows. Now
When we fire the query within the sp directly in a query window, correct results come up. The only difference in this query is that instead of a cursor, the query directly outputs the result set; i.e. no cursor is opened when we fire the query directly.
When we recompile the sp, correct results come up for some time, and then it is back to missing rows after some time.
Have not been able to make much progress on this. Any pointers on what to look for, how to make progress etc. are much appreciated. I can paste the sp in here if needed, and probably the sp can be rewritten in some other way to make the problem go away, but I am more interested in knowing root cause - what could be causing this strange problem?
SP
CREATE OR REPLACE PROCEDURE "SP_GETTAXSETTINGCHANNELINFO" (v_bid varchar2,
p_rdTaxSetting out SYS_REFCURSOR) as
vT_bid varchar2(100);
begin
vT_bid := v_bid;
Open p_rdTaxSetting for
Select taxmappingid, CHANNELNAME,PROPERTYNAME, PROPERTYID, TAXTYPE, ISPARTIALLYINCLUSIVE, PARTIALLYINCLUSIVEVALUE
FROM (
WITH ChannelData AS (
SELECT ONDEMAND_SELECTED_SOURCES AS OnDemandChannels,
SCHEDULED_SELECTED_SOURCES AS ScheduledChannels
FROM SUB_ORDER S
INNER JOIN CONTACT C ON S.Supplier_ID = C.Contact_ID
WHERE C.BID = vT_bid
AND S.STATUS='Complete' and current_ind = 1
),
Property_Data As (
SELECT
SC.PROPERTY_NUM AS PropertyID
,SC.Company_Name as PropertyName
,SC.competitor_number as PropertyOrder
FROM SUB_ORDER S
INNER JOIN CONTACT C ON S.Supplier_ID = C.Contact_ID
INNER JOIN SUB_ORDER_COMPETITOR SC ON S.Order_Id = SC.Order_ID
WHERE C.BID = vT_bid
AND S.STATUS='Complete' and current_ind = 1
),
ODChannel as (
select regexp_substr (OnDemandChannels, '[^,]+', 1, rn) as Channel
from (Select OnDemandChannels From ChannelData)
cross join
(select rownum rn
from (select max (length (regexp_replace (OnDemandChannels, '[^,]+'))) + 1 max_value
from (Select OnDemandChannels From ChannelData))
connect by level <= max_value)
where regexp_substr (OnDemandChannels, '[^,]+', 1, rn) is not null
),
SCDChannel As (
select regexp_substr (ScheduledChannels, '[^,]+', 1, rn) as Channel
from (Select ScheduledChannels From ChannelData)
cross join
(select rownum rn
from (select max (length (regexp_replace (ScheduledChannels, '[^,]+'))) + 1 max_value
from (Select ScheduledChannels From ChannelData))
connect by level <= max_value)
where regexp_substr (ScheduledChannels, '[^,]+', 1, rn) is not null
),
ChaData1 As (
Select
CASE lower(Channel)
WHEN 'galileo' then 'GDS'
else Channel
end AS Channel
from ODChannel
),
ChaData2 As (
Select CASE lower(Channel)
WHEN 'galileo' then 'GDS'
else Channel
end AS Channel FROM SCDChannel
),
PropData As (
Select Distinct PropertyID,PropertyName from Property_Data
order by PropertyID
),
AllChannel AS (
Select Channel From ChaData1
union
Select Channel From ChaData2
),
Prop_Cha_Data As (
Select Distinct Channel,PropertyID,PropertyName from AllChannel,Property_Data
order by Channel,PropertyID
),
Tax_Channel_Prop_Exists AS ( select
CASE lower(CHANNELNAME)
WHEN 'galileo' then 'GDS'
else CHANNELNAME
end AS CHANNELNAME,
PropertyName As PROPERTYNAME, TCP.PROPERTYID As PROPERTYID,TAXTYPE,
ISPARTIALLYINCLUSIVE, PARTIALLYINCLUSIVEVALUE,taxmappingid
from TAXSETTING_CHANNEL_PROPERTY TCP
INNER JOIN AllChannel PC On TRIM (CASE lower(TCP.CHANNELNAME)
WHEN 'galileo' then 'GDS'else TCP.CHANNELNAME end) = TRIM(PC.Channel)
INNER JOIN PropData CP On TCP.PROPERTYID = CP.PropertyID
where TCP.BID=vT_bid
)
Select Distinct taxmappingid, CHANNELNAME,PROPERTYNAME, PROPERTYID, TAXTYPE, ISPARTIALLYINCLUSIVE, PARTIALLYINCLUSIVEVALUE from Tax_Channel_Prop_Exists
UNION ALL
select DISTINCT 0 As taxmappingid, PCD.Channel As CHANNELNAME,
PCD.PropertyName As PROPERTYNAME ,
PCD.propertyid As PROPERTYID,
-1 As TAXTYPE,
0 As ISPARTIALLYINCLUSIVE,
'' As PARTIALLYINCLUSIVEVALUE
FROM Prop_Cha_Data PCD
WHERE NOT EXISTS (
Select taxmappingid FROM Tax_Channel_Prop_Exists E
WHERE E.channelname = PCD.Channel AND
E.propertyid = PCD.propertyid )
)
Order by PROPERTYNAME,CHANNELNAME ;
end SP_GetTaxSettingChannelInfo;
I have two queries with a union as follows:
select '00/00/0000' as payment_date , h1.customer_no
from payments h1
where not exists ( select 1 from payments h2 where h2.customer_no = h1.customer_no and h2.ctype = 'CASH' )
and h1.customer_no = 400
group by h1.customer_no
union
select to_char(h1.payment_date, 'MM/DD/YYYY') , h1.customer_no
from payments h1 inner join ( select customer_no, max(payment_date ) as max_date from payments where ctype = 'CASH' group by customer_no ) subQ
on ( h1.customer_no = subQ.customer_no
and h1.payment_date = subQ.max_date )
and h1.customer_no = 400
group by h1.payment_date, h1.customer_no
Now, I want to use this union in another query.
select * from (
select '00/00/0000' as payment_date , h1.customer_no
from payments h1
where not exists ( select 1 from payments h2 where h2.customer_no = h1.customer_no and h2.ctype = 'CASH' )
and h1.customer_no = p.customer_no
group by h1.customer_no
union
select to_char(h1.payment_date, 'MM/DD/YYYY') , h1.customer_no
from payments h1 inner join ( select customer_no, max(payment_date ) as max_date from payments where ctype = 'CASH' group by customer_no ) subQ
on ( h1.customer_no = subQ.customer_no
and h1.payment_date = subQ.max_date )
and h1.customer_no = p.customer_no
group by h1.payment_date, h1.customer_no ) sq,
payments p
where p.customer_no = 400
and sq.customer_no = p.customer_no
when I run this, I get ORA-00904: "P"."CUSTOMER_NO": invalid identifier. I need to join h1.customer_no to outer queries customer_no.
I have seen some queries with rank but I couldn't quite figure it out. How do I join the inner query with the outer query?
thanks in advance.
Does your payments table have a customer_no column? That seems to be what your error is indicating.
As for how to join the subqueries, you might want to look into factored subqueries. You can do:
WITH z AS (
SELECT ...
UNION
SELECT ...
), y AS (
SELECT ...
)
SELECT ...
FROM y
JOIN z ON y.x = z.x
JOIN some_other_table t ON z.a = t.a
I have a chain of CTEs, and at the end I want to select from the last one twice. Oracle lets me do either of the selects but not a union of both of them (even if I do "select * from (select union select)". The only thing I could narrow down the problem to was referring to "runinfo" in two selects, but that in and of itself is not the problem as Shannon shows.
WITH lastTen AS (
SELECT id
FROM (SELECT autobuild_id, id, rank() OVER (PARTITION BY autobuild_id ORDER BY id DESC) as rank
FROM runs
WHERE status='FINISHED' AND type='FULL' AND build_failed in ('n', 'N'))
WHERE rank <= 10
),
recentAvg AS (
SELECT autobuild_id, avg(elapsed) avgtime
FROM runs
JOIN lastTen ON (runs.id = lastTen.id)
GROUP BY autobuild_id
),
runinfo AS (
SELECT autobuildid, runid, changelist, status, age
FROM (
SELECT runs.autobuild_id autobuildid, runs.id runid, changelist, runs.status status, runs.create_date, a.avgtime,
CASE WHEN status = 'RUNNING' THEN TO_NUMBER(sysdate - start_date)*86400 -- in seconds to compare to elapsed
WHEN status = 'TO BE' THEN TO_NUMBER(sysdate - create_date) -- in days
END AS age
FROM runs
LEFT JOIN recentAvg a ON (runs.autobuild_id = a.autobuild_id)
)
WHERE (status = 'RUNNING' AND age > avgtime * 1.5)
OR (status = 'TO BE' AND age > 1)
ORDER BY autobuildid, runid DESC
),
running AS (
SELECT autobuilds.id, name, runid, changelist, runinfo.status, age
FROM runinfo
JOIN autobuilds ON (runinfo.autobuildId=autobuilds.id)
WHERE runinfo.status='RUNNING'
),
tobe AS (
SELECT autobuildid, name, runid, changelist, status, age
FROM (SELECT autobuildid, name, runid, changelist, runinfo.status, age, RANK() OVER (PARTITION BY autobuildid ORDER BY runid DESC) AS rank
FROM runinfo
JOIN autobuilds ON (runinfo.autobuildid=autobuilds.id)
WHERE runinfo.status='TO BE')
WHERE rank=1
)
SELECT * FROM running
UNION ALL
SELECT * FROM tobe
What is the simpliest query that doesn't work for you?
What is the complete error message?
Oracle does allow a CTE to be referenced twice, but I am not understanding what you are attempting:
SQL> with a as (select * from dual)
2 , b as (select * from a)
3 , c as (select * from b)
4 select *
5 from b, c
6 where b.dummy = c.dummy;
D D
- -
X X