Table not referenced in recursive PostgreSQL query - sql

for some reasons I don't have, a table, although referenced in the query, is not referenced according to the error.
The query :
WITH RECURSIVE temptable AS (
SELECT part_id, tracability_component1, tracability_component2, tracability_component3, tracking_id
FROM kpitr.subop_result SR
LEFT JOIN kpitr.part_produce PP ON PP.supplier_id || PP.product_id LIKE SR.tracability_component1 OR PP.supplier_id || PP.product_id LIKE SR.tracability_component2 OR PP.supplier_id || PP.product_id LIKE SR.tracability_component3
WHERE part_id LIKE '%0005001723A031T'
UNION ALL
SELECT SR2.part_id, SR2.tracability_component1, SR2.tracability_component2, SR2.tracability_component3, PP2.tracking_id
FROM kpitr.subop_result SR2, temptable
LEFT JOIN kpitr.part_produce PP2 ON PP2.supplier_id || PP2.product_id LIKE SR2.tracability_component1 OR PP2.supplier_id || PP2.product_id LIKE SR2.tracability_component2 OR PP2.supplier_id || PP2.product_id LIKE SR2.tracability_component3 OR SR2.part_id LIKE temptable.tracking_id
)
SELECT * FROM temptable
The error :
ERROR: invalid reference to FROM-clause entry for table "sr2"
LINE 9: ...uce PP2 ON PP2.supplier_id || PP2.product_id LIKE SR2.tracab...
^
HINT: There is an entry for table "sr2", but it cannot be referenced from this part of the query.
SQL state: 42P01
Character: 678
I believe I place in the wrong order something, or I missed sometinh, but after some tests, it didn't change anything.
Could you help me ?

Got the solution, after some tries :
WITH RECURSIVE temptable AS (
SELECT SR1.part_id, SR1.tracability_component1, SR1.tracability_component2, SR1.tracability_component3, PP1.tracking_id
FROM kpitr.subop_result SR1
LEFT JOIN kpitr.part_produce PP1 ON UPPER(PP1.reference) || UPPER(PP1.product_id) = UPPER(SR1.tracability_component1) OR UPPER(PP1.reference) || UPPER(PP1.product_id) = UPPER(SR1.tracability_component2) OR UPPER(PP1.reference) || UPPER(PP1.product_id) = UPPER(SR1.tracability_component3)
WHERE part_id LIKE '%0005001723A031T'
UNION ALL
SELECT sr.part_id, sr.tracability_component1, sr.tracability_component2, sr.tracability_component3, pp.tracking_id
FROM kpitr.subop_result sr
JOIN temptable ON UPPER(sr.part_id) = UPPER(temptable.tracability_component1) OR UPPER(sr.part_id) = UPPER(temptable.tracability_component2) OR UPPER(sr.part_id) = UPPER(temptable.tracability_component3) OR UPPER(sr.part_id) = UPPER(temptable.tracking_id)
LEFT JOIN kpitr.part_produce pp ON UPPER(pp.reference) || UPPER(pp.product_id) = UPPER(sr.tracability_component1) OR UPPER(pp.reference) || UPPER(pp.product_id) = UPPER(sr.tracability_component2) OR UPPER(pp.reference) || UPPER(pp.product_id) = UPPER(sr.tracability_component3)
)
SELECT * FROM temptable

Related

Sqlite. Loop through rows and match, break when value is equal

Could anyone suggest please is it possible to break recursion function if circular dependency happend ? As soon as I encounter that s.Part_Ptr = s.Substituent_Ptr I want to break Loop and return result.
with SubstitutionRecursion as (select Part_Ptr, Substituent_Ptr from model_2271_substitute where Part_Ptr = 24588687861243
union all
select s.Part_Ptr, s.Substituent_Ptr from SubstitutionRecursion sr
left join model_2271_substitute s on s.Part_Ptr = sr.Substituent_Ptr
where s.Substituent_Ptr is not null)
select p.partCode, p.partDescription, p.notes from SubstitutionRecursion sr
left join model_2271_all_parts p on p.PartPtr = sr.Substituent_Ptr;
You would typically keep track of the visited nodes in another column of the CTE, and stop the recursion when an already visited node is met.
Your question does not show sample data, which makes it a bit more complicated to answer, but the logic would look like:
with substitutionrecursion as (
select part_ptr, substituent_ptr, '/' || substituent_ptr || '/' path
from model_2271_substitute
where part_ptr = 24588687861243
union all
select s.part_ptr, s.substituent_ptr, sr.path || s.substituent_ptr || '/'
from substitutionrecursion sr
inner join model_2271_substitute s on s.part_ptr = sr.substituent_ptr
where s.substituent_ptr not like '%/' || sr.path || '/%'
)
select ...
path is a string column that concatenates the visited substituent_ptrs.

Unable to retrieve Data Oracle Fusion BI Publisher

1.Main Code:
SELECT
iodv.organization_id,
iodv.organization_name,
mil.SUBINVENTORY_CODE subinventory_name,
mil.inventory_location_id locator_id,
mil.segment11 || '.' || mil.segment12 || '.' || mil.segment13 locator_name
FROM
inv_item_locations mil,
inv_organization_definitions_v iodv
WHERE
1 = 1
and iodv.organization_id = mil.organization_id
and iodv.Organization_Code = ':Organizations'
and mil.SUBINVENTORY_CODE = ':p_Subinventories'
Parameter p_SubInventories:
select
distinct
iil.Subinventory_code
from
inv_item_locations iil,
INV_ORG_PARAMETERS iop
where
1=1
and iil.ORGANIZATION_ID=iop.ORGANIZATION_ID
and iop.ORGANIZATION_CODE= :Organizations
Parameter for Organizations:
select distinct organization_code from inv_org_parameters
Unable to retrieve data with the above query in Data Model # Oracle BI Publisher......
LEFT JOIN here
SELECT
iodv.organization_id,
iodv.organization_name,
mil.SUBINVENTORY_CODE subinventory_name,
mil.inventory_location_id locator_id,
mil.segment11 || '.' || mil.segment12 || '.' || mil.segment13 locator_name
FROM
inv_item_locations mil
LEFT JOIN
inv_organization_definitions_v iodv
ON
iodv.organization_id = mil.organization_id
WHERE 1 = 1
AND iodv.Organization_Code = ':Organizations'
AND mil.SUBINVENTORY_CODE = ':p_Subinventories'
I would have thought the single apostrophe was problem, but maybe removing the inner join also helps.
It's worth noting the LEFT JOIN could be applied using using PL SQL Outer Join Operator (+). See adjustment to Ops main code below
SELECT
iodv.organization_id,
iodv.organization_name,
mil.SUBINVENTORY_CODE subinventory_name,
mil.inventory_location_id locator_id,
mil.segment11 || '.' || mil.segment12 || '.' || mil.segment13 locator_name
FROM
inv_item_locations mil,
inv_organization_definitions_v iodv
WHERE
1 = 1
and iodv.organization_id (+) = mil.organization_id ----
and iodv.Organization_Code = ':Organizations'
and mil.SUBINVENTORY_CODE = ':p_Subinventories'

Unsure why ORA-00918 column ambiguously defined is appearing

I'm unsure as to why I'm getting the ORA-00918 error message appearing when I type in the following code.
I can't see which column is ambiguously defined.
What I want to do is create a table that pulls in the b.site_code value based on the work_header_no, work_version_no and site_numbers matching in queries A & B matching.
Code is below
SELECT
a.statement.statement_date,
a.sw_header.organise_code,
a.organisation.organise_name,
a.PermitRef,
a.actual_inspection.logged_time,
a.insp_category.insp_category_name,
a.actual_inspection.insp_number,
a.actual_inspection.site_number,
a.inspection_outcome.insp_outcome_name,
a.insp_category.insp_charge,
a.actual_inspection.insp_notes,
a.actual_inspection.work_header_no,
a.actual_inspection.insp_time,
b.site_code
FROM
(select
statement.statement_date,
sw_header.organise_code,
organisation.organise_name,
CAST(
organisation.external_ref_2 ||''||
sw_header.works_ref||'.'||
sw_notice_header.app_seq_no||'.'||
sw_notice_header.ext_version_no
as VARCHAR (40)) as PermitRef,
actual_inspection.logged_time,
insp_category.insp_category_name,
actual_inspection.insp_number,
actual_inspection.site_number,
inspection_outcome.insp_outcome_name,
insp_category.insp_charge,
actual_inspection.insp_notes,
actual_inspection.work_header_no,
actual_inspection.insp_time,
sw_notice_header.work_header_no,
sw_notice_header.work_version_no,
actual_inspection.site_number
from
actual_inspection
inner join sw_header on
actual_inspection.work_header_no = sw_header.work_header_no
inner join sw_notice_header on
sw_header.work_header_no = sw_notice_header.work_header_no
and sw_header.work_version_no = sw_notice_header.work_version_no
inner join insp_category on
actual_inspection.insp_category_code = insp_category.insp_category_code
inner join inspection_outcome on
actual_inspection.insp_outcome_code = inspection_outcome.insp_outcome_code
inner join organisation on
sw_header.organise_code = organisation.organise_code
inner join statement on
organisation.organise_code = statement.organise_code
and organisation.statement_number = statement.statement_no
where
actual_inspection.notice_type_code = '2600' and
actual_inspection.insp_outcome_code != 'O40'
order by
actual_inspection.logged_time)
a
JOIN
(
select
sns.work_header_no,
sns.work_version_no,
sns.site_number,
sns.site_code
from
sw_notice_site sns
)
b
ON a.work_header_no = b.work_header_no and
a.work_version_no = b.work_version_no and
a.site_number = b.site_number
You have duplicate actual_inspection.site_number and work_header_no remove the duplicate rows
actual_inspection.site_number,
inspection_outcome.insp_outcome_name,
insp_category.insp_charge,
actual_inspection.insp_notes,
actual_inspection.work_header_no,
actual_inspection.insp_time,
sw_notice_header.work_header_no,
sw_notice_header.work_version_no,
actual_inspection.site_number
No need to use a.statement.statement_date. You can use a.statement_date.
Likewise change all other columns for a..
Your whole query should look like this:
SELECT -- removed table names from all the columns
A.STATEMENT_DATE,
A.ORGANISE_CODE,
A.ORGANISE_NAME,
A.PERMITREF,
A.LOGGED_TIME,
A.INSP_CATEGORY_NAME,
A.INSP_NUMBER,
A.SITE_NUMBER,
A.INSP_OUTCOME_NAME,
A.INSP_CHARGE,
A.INSP_NOTES,
A.WORK_HEADER_NO,
A.INSP_TIME,
B.SITE_CODE
FROM
(
SELECT
STATEMENT.STATEMENT_DATE,
SW_HEADER.ORGANISE_CODE,
ORGANISATION.ORGANISE_NAME,
CAST(ORGANISATION.EXTERNAL_REF_2
|| ''
|| SW_HEADER.WORKS_REF
|| '.'
|| SW_NOTICE_HEADER.APP_SEQ_NO
|| '.'
|| SW_NOTICE_HEADER.EXT_VERSION_NO AS VARCHAR(40)) AS PERMITREF,
ACTUAL_INSPECTION.LOGGED_TIME,
INSP_CATEGORY.INSP_CATEGORY_NAME,
ACTUAL_INSPECTION.INSP_NUMBER,
--ACTUAL_INSPECTION.SITE_NUMBER, -- commented this as it is there in statement twice
INSPECTION_OUTCOME.INSP_OUTCOME_NAME,
INSP_CATEGORY.INSP_CHARGE,
ACTUAL_INSPECTION.INSP_NOTES,
ACTUAL_INSPECTION.WORK_HEADER_NO,
ACTUAL_INSPECTION.INSP_TIME,
--SW_NOTICE_HEADER.WORK_HEADER_NO, -- commented this as it is there in statement twice
SW_NOTICE_HEADER.WORK_VERSION_NO,
ACTUAL_INSPECTION.SITE_NUMBER
FROM
ACTUAL_INSPECTION
INNER JOIN SW_HEADER ON ACTUAL_INSPECTION.WORK_HEADER_NO = SW_HEADER.WORK_HEADER_NO
INNER JOIN SW_NOTICE_HEADER ON SW_HEADER.WORK_HEADER_NO = SW_NOTICE_HEADER.WORK_HEADER_NO
AND SW_HEADER.WORK_VERSION_NO = SW_NOTICE_HEADER.WORK_VERSION_NO
INNER JOIN INSP_CATEGORY ON ACTUAL_INSPECTION.INSP_CATEGORY_CODE = INSP_CATEGORY.INSP_CATEGORY_CODE
INNER JOIN INSPECTION_OUTCOME ON ACTUAL_INSPECTION.INSP_OUTCOME_CODE = INSPECTION_OUTCOME.INSP_OUTCOME_CODE
INNER JOIN ORGANISATION ON SW_HEADER.ORGANISE_CODE = ORGANISATION.ORGANISE_CODE
INNER JOIN STATEMENT ON ORGANISATION.ORGANISE_CODE = STATEMENT.ORGANISE_CODE
AND ORGANISATION.STATEMENT_NUMBER = STATEMENT.STATEMENT_NO
WHERE
ACTUAL_INSPECTION.NOTICE_TYPE_CODE = '2600'
AND ACTUAL_INSPECTION.INSP_OUTCOME_CODE != 'O40'
ORDER BY
ACTUAL_INSPECTION.LOGGED_TIME
) A
JOIN (
SELECT
SNS.WORK_HEADER_NO,
SNS.WORK_VERSION_NO,
SNS.SITE_NUMBER,
SNS.SITE_CODE
FROM
SW_NOTICE_SITE SNS
) B ON A.WORK_HEADER_NO = B.WORK_HEADER_NO
AND A.WORK_VERSION_NO = B.WORK_VERSION_NO
AND A.SITE_NUMBER = B.SITE_NUMBER;
Cheers!!

SQL query columns spreading

I have the following SQL query and as we see in the screenshot, there are two repeating row with the same constr_id(2015) value but with different assigned_insurance_packages.
Query:
select
ac.constr_id,
AIP.NAME as ASSIGNED_INSURANCE_PACKAGES,
ac.code,ac.constr_name,
ac.offer_name,
ac.repay_freq,
ac.min_amt,
ac.max_amt,
ac.min_downpay_percent,
ac.max_downpay_percent,
ac.downpay_amount,
ac.min_term,
(select
listagg(AF.NAME, '; ') within group(order by ACF.CONSTR_ID)
from
CREDILOGIC.ACQ_CONSTR_FEE acf, CREDILOGIC.ACQ_FEE af
where
ACF.CONSTR_ID = AC.CONSTR_ID and AF.FEE_ID = ACF.FEE_ID) as CONSTRUCTION_FEES,
INS_COMPANY.SHORT_NAME,
(select listagg(x.DURATION_MIN || '-' || TO_CHAR(x.RATE_SHIFT, '90.99') || ',' || x.DURATION_MAX || '-' || TO_CHAR(x.RATE_SHIFT, '90.99'), '; ') within group (order by X.CONSTRUCTION_ID)
from credilogic.ACQ_CONSTRUCTION_BASERATE x
where X.CONSTRUCTION_ID = AC.CONSTR_ID) as RATE
from
CREDILOGIC.ACQ_CONSTRUCTION ac
left join
CREDILOGIC.ACQ_CONSTR_INSPACKAGE aci on ACI.CONSTR_ID = AC.CONSTR_ID
and ACI.DELETED_TSTAMP is null
left join
CREDILOGIC.ACQ_INSURANCE_PACKAGE aip on AIP.INSURANCE_PACKAGE_ID = ACI.INSURANCE_PACKAGE_ID
left join
credilogic.ath_party ins_company ON INS_COMPANY.PARTY_ID = aip.PARTY_ID
left join
credilogic.ACQ_CONSTRUCTION_DUEDATE acd on ac.constr_id = acd.constr_id
left join
credilogic.ACQ_APPLICATION acp on ac.constr_id = acp.construction_id
I am just going to create two additional columns like 0.3% insurance pack and 0.5% insurance pack to put ASSIGNED_INSURANCE_PACKAGES values into different columns like in the picture below

issues combining two columns using concat

I am having issues with combining two columns into the one using mssql
table 1 format:
|| WJCPrefix || WJCNo ||
|| UK-R/SWJC/14/ || 1234 ||
|| UK-R/CUWJC/14/ || 2345 ||
|| UK-R/CUWJC/14/ || 3456 ||
|| UK-R/SWJC/14/ || 4567 ||
|| UK-R/CUWJC/14/ || 5678 ||
The desired out would be:
UK-R/CUWJC/14/3456
UK-R/CUWJC/14/5678
the sql statement i am using is:
SELECT tblWJCItem.AddedDescription, concat(tblWJC.WJCPrefix, tblWJC.WJCNo) AS OurRef
FROM tblWJC
INNER JOIN tblWJCItem ON tblWJC.WJCID = tblWJCItem.WJCID;
I've also used:
SELECT tblWJCItem.AddedDescription, tblWJC.WJCPrefix + '' + tblWJC.WJCNo AS OurRef
FROM tblWJC
INNER JOIN tblWJCItem ON tblWJC.WJCID = tblWJCItem.WJCID;
I can't seem to connect these two columns could anyone point out what I am doing wrong here?
Thanks
Your first query (Concat() function) should work if you are using SQL Server 2012 or later.
For other versions, you may need to Convert()/Cast() WJCNo to a string type
SELECT t2.AddedDescription,
t1.WJCPrefix + CONVERT(Varchar(10),t1.WJCNo) AS OurRef
FROM tblWJC t1
INNER JOIN tblWJCItem t2 ON t1.WJCID = t2.WJCID;
Your first query should be fine. But you might try:
SELECT tblWJCItem.AddedDescription, tblWJC.Prefix + cast(tblWJC.WJCNo as varchar(255)) AS OurRef
FROM tblWJC INNER JOIN
tblWJCItem
ON tblWJC.WJCID = tblWJCItem.WJCID;
You might get an error in the second version if WJCno is numeric rather than a string.
I think WJCNo is numeric or int field so Convert this field to Varchar first then concat:-
SELECT tblWJCItem.AddedDescription,
tblWJC.WJCPrefix + '' + CONVERT(Varchar(10),tblWJC.WJCNo) AS OurRef
FROM tblWJC
INNER JOIN tblWJCItem ON tblWJC.WJCID = tblWJCItem.WJCID;