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!!
Related
I have converted oracle query into below standard bq but in last statement(IN subquery). I am getting error:
"IN subquery is not supported inside join predicate."
Please advise how to use IN subquery in bq in the below code
#Last part of the code
INNER JOIN (
SELECT
DISTINCT `domain-rr.oracle_DB_DB.he_project_assoc`.PARENT_ISBN
PARENT_ISBN,
SUM (`domain-rr.DB_RPT.PROJECT_GR_QTY`.GR_QTY) GR_QTY
FROM
`domain-rr.oracle_DB_DB.he_project_assoc`
INNER JOIN
`domain-rr.DB_RPT.PROJECT_GR_QTY`
ON
`domain-rr.oracle_DB_DB.he_project_assoc`.child_ISBN = `domain-
rr.DB_RPT.PROJECT_GR_QTY`.BIC_GCISBN
AND `domain-rr.oracle_DB_DB.he_project_assoc`.BREAK_LABEL <>
'Associated ISBNs'
GROUP BY
`domain-rr.oracle_DB_DB.he_project_assoc`.PARENT_ISBN) xx
ON
yy.PARENT_ISBN = xx.PARENT_ISBN
AND yy.CIRCULATION_INT < xx.GR_QTY
AND yy.PARENT_ISBN IN
( SELECT
DISTINCT _BIC_GCISBN
FROM
`domain-rr.DB_RPT.BIC_GM_AGCPOAODS00_BO_VW`
INNER JOIN
`domain-rr.oracle_DB_boadmin.fiscal_bo`
ON
_BIC_ZC2GRIRIN = 'G'
AND _BIC_ZCLOEKZ = ' '
AND SUBSTR (BOUND_DATE, 1, 6) = `domain-
rr.oracle_DB_boadmin.fiscal_bo`.PRIOR_FISC_YEAR_MONTH )
Can you try like this:
Select * from (
#Last part of the code
INNER JOIN (
SELECT
DISTINCT `pearson-rr.oracle_grdw_grdw.he_project_assoc`.PARENT_ISBN
PARENT_ISBN,
SUM (`pearson-rr.GRDW_RPT.PROJECT_GR_QTY`.GR_QTY) GR_QTY
FROM
`pearson-rr.oracle_grdw_grdw.he_project_assoc`
INNER JOIN
`pearson-rr.GRDW_RPT.PROJECT_GR_QTY`
ON
`pearson-rr.oracle_grdw_grdw.he_project_assoc`.child_ISBN = `pearson-
rr.GRDW_RPT.PROJECT_GR_QTY`.BIC_GCISBN
AND `pearson-rr.oracle_grdw_grdw.he_project_assoc`.BREAK_LABEL <>
'Associated ISBNs'
GROUP BY
`pearson-rr.oracle_grdw_grdw.he_project_assoc`.PARENT_ISBN) xx
ON
yy.PARENT_ISBN = xx.PARENT_ISBN
AND yy.CIRCULATION_INT < xx.GR_QTY
) AA
where AA.PARENT_ISBN IN
( SELECT
DISTINCT _BIC_GCISBN
FROM
`pearson-rr.GRDW_RPT.BIC_GM_AGCPOAODS00_BO_VW`
INNER JOIN
`pearson-rr.oracle_grdw_boadmin.fiscal_bo`
ON
_BIC_ZC2GRIRIN = 'G'
AND _BIC_ZCLOEKZ = ' '
AND SUBSTR (BOUND_DATE, 1, 6) = `pearson-
rr.oracle_grdw_boadmin.fiscal_bo`.PRIOR_FISC_YEAR_MONTH )
The following query below listed generates blank values in a row.
-- Simplify the pivot selection query by separating the query using a with clause
WITH pivot_data AS(
SELECT va.identity,
vc.units,
s.field_name "Sample ID",
s.id_text "Lab ID",
TO_CHAR(str.result_value, S_FORMATMASK_PACKAGE.s_FormatMask(vc.analysis, s.id_numeric))result_value
FROM sample s
LEFT OUTER JOIN client c ON c.id = s.client_id
LEFT OUTER JOIN samp_test_result str ON (s.id_numeric = str.id_numeric
AND s.id_text = str.id_text and str.result_value is not null)
LEFT OUTER JOIN versioned_analysis va ON (va.identity = str.analysis)
LEFT OUTER JOIN versioned_component vc ON (vc.analysis = va.identity
AND vc.analysis_version = va.analysis_version
AND vc.name = str.component_name)
WHERE s.fas_sample_type = sample_pkg.get_leaf_sample
AND s.status = sample_pkg.get_authorised_sample
AND s.flg_released = constant_pkg.get_true
AND vc.flg_report = constant_pkg.get_true
AND c.id = UPPER ('I000009')
AND s.ID_NUMERIC between TO_NUMBER(2126) and TO_NUMBER(12917) and str.result_value <> 0
)
SELECT pvt1.*
FROM(SELECT * FROM pivot_data PIVOT (MAX(result_value) result_value, MAX(units) units FOR identity IN('NIR_N' "Nitrogen",
'XRF_P' "Phosphorus",
'XRF_K' "Potassium",
'XRF_CA' "Calcium",
'XRF_MG' "Magnesium",
'XRF_S' "Sulphur",
'XRF_SI' "Silicon",
'XRF_ZN' "Zinc",
'XRF_MN' "Manganese",
'XRF_CU' "Copper",
'XRF_FE' "Iron"))) pvt1
How do I remove a row that contains null fields
How do I create Stored Procedure on these queries and he output should show which check the anomaly was captured from, along with all the relevant data.
SELECT cm.Cust_id, cm.cust_ref_id4, cm.cust_ref_id3, cm.plan_group, cm.Company_name, cm.Cust_firstname, cm.Cust_lastname
COALESCE(c.pkCustomerID, c2.fkCustomerID, c3.pkCustomerID, c4.pkCustomerID) AS pkCustomerID, c3.CompanyName FROM PRODUCTIONSQL.[SigmaPaTri].[dbo].[CUSTOMER_MASTER] cm
LEFT JOIN PHOENIX.CORE.dbo.Customers AS c ON cust_ref_id4 = c.pkCustomerID AND cm.cust_ref_id3 = c.pkCustomerID AND cm.cust_ref_id3 >= 1000000 AND cm.Cust_firstname + ' ' + cm.Cust_lastname = c.CompanyName
LEFT JOIN PHOENIX.CORE.dbo.Contracts AS c2 ON cm.cust_ref_id3 = c2.ConfirmationNumber
WHERE cm.cust_status IN ('A','P','R','G') AND COALESCE(c.pkCustomerID, c2.fkCustomerID) IS NULL ORDER BY cust_ref_id4;
and
SELECT [pkCustomerID],b.[pkContractID],[pkCustomerTypeID],[CustomerType],b.[ContractType] AS Contractype1,c.[ContractType]
AS Contractype2 FROM [CORE].[dbo].[Customers] a
JOIN [CORE].[dbo].[CustomerTypes] ON [pkCustomerTypeID] = [fkCustomerTypeID]
LEFT JOIN (SELECT [pkContractID],[ContractType],[fkCustomerID] FROM [CORE].[dbo].[Contracts]
JOIN [CORE].[dbo].[ContractTypes] ON [fkContractTypeID] = [pkContractTypeID] WHERE [ContractType] NOT LIKE 'Holdover%')b ON a.pkCustomerID=b.fkCustomerID
LEFT JOIN (SELECT [pkContractID],[fkCustomerID],[ContractType] FROM [CORE].[dbo].[Contracts]
JOIN [CORE].[dbo].[ContractTypes] ON [fkContractTypeID] = [pkContractTypeID] WHERE ContractType LIKE 'Holdover%')c ON b.fkCustomerID=c.fkCustomerID WHERE [CustomerType] IN ('Customer','Former Customer') AND (b.ContractType IS NULL OR c.ContractType IS NULL)
You question is lacking a very important piece of information, the explanation of what you are trying to do. I took a shot in the dark here as a guess to what you might be looking for. BTW, I ran this through a formatter so it was legible.
SELECT 'Found in query1'
,cm.Cust_id
,cm.cust_ref_id4
,cm.cust_ref_id3
,cm.plan_group
,cm.Company_name
,cm.Cust_firstname
,cm.Cust_lastname
,COALESCE(c.pkCustomerID, c2.fkCustomerID, c3.pkCustomerID, c4.pkCustomerID) AS pkCustomerID
,c3.CompanyName
FROM PRODUCTIONSQL.[SigmaPaTri].[dbo].[CUSTOMER_MASTER] cm
LEFT JOIN PHOENIX.CORE.dbo.Customers AS c ON cust_ref_id4 = c.pkCustomerID
AND cm.cust_ref_id3 = c.pkCustomerID
AND cm.cust_ref_id3 >= 1000000
AND cm.Cust_firstname + ' ' + cm.Cust_lastname = c.CompanyName
LEFT JOIN PHOENIX.CORE.dbo.Contracts AS c2 ON cm.cust_ref_id3 = c2.ConfirmationNumber
WHERE cm.cust_status IN (
'A'
,'P'
,'R'
,'G'
)
AND COALESCE(c.pkCustomerID, c2.fkCustomerID) IS NULL
SELECT 'Found in query 2'
,[pkCustomerID]
,b.[pkContractID]
,[pkCustomerTypeID]
,[CustomerType]
,b.[ContractType] AS Contractype1
,c.[ContractType] AS Contractype2
FROM [CORE].[dbo].[Customers] a
INNER JOIN [CORE].[dbo].[CustomerTypes] ON [pkCustomerTypeID] = [fkCustomerTypeID]
LEFT JOIN (
SELECT [pkContractID]
,[ContractType]
,[fkCustomerID]
FROM [CORE].[dbo].[Contracts]
INNER JOIN [CORE].[dbo].[ContractTypes] ON [fkContractTypeID] = [pkContractTypeID]
WHERE [ContractType] NOT LIKE 'Holdover%'
) b ON a.pkCustomerID = b.fkCustomerID
LEFT JOIN (
SELECT [pkContractID]
,[fkCustomerID]
,[ContractType]
FROM [CORE].[dbo].[Contracts]
INNER JOIN [CORE].[dbo].[ContractTypes] ON [fkContractTypeID] = [pkContractTypeID]
WHERE ContractType LIKE 'Holdover%'
) c ON b.fkCustomerID = c.fkCustomerID
WHERE [CustomerType] IN (
'Customer'
,'Former Customer'
)
AND (
b.ContractType IS NULL
OR c.ContractType IS NULL
)
i have 2 tables TblMemberDetails,TblMemberDetailsSub am using following sql queries to fetch records
QUERY 1
SELECT (TblMemberDetails.MemFname +' '+TblMemberDetails.MemMname +' '+
TblMemberDetails.MemLname)As Name,
TblStateMaster.StateName,
TblDistMaster.DistName,
TblTaluqaMaster.TaluqaName
FROM TblMemberDetails INNER JOIN TblStateMaster
ON TblMemberDetails.StateId = TblStateMaster.StateId
INNER JOIN TblDistMaster ON TblMemberDetails.DistId = TblDistMaster.DistId
INNER JOIN TblTaluqaMaster ON TblMemberDetails.TaluqaId = TblTaluqaMaster.TaluqaId
INNER JOIN TblMemberDetailsSub ON TblMemberDetails.MemId = TblMemberDetailsSub.MemId
WHERE (TblMemberDetailsSub.MemMode = 'Provider')
AND (TblMemberDetailsSub.CycleStatus = 'Uncompleted')
GROUP BY TblMemberDetailsSub.MemId, TblMemberDetails.MemId,
TblMemberDetails.MemFname, TblMemberDetails.MemMname,
TblMemberDetails.MemLname, TblStateMaster.StateName,
TblDistMaster.DistName, TblTaluqaMaster.TaluqaName
order by TblMemberDetailsSub.MemId Asc
QUERY 2
SELECT TblMemberDetailsSub.MemId, ISNULL(sum(TblMemberDetailsSub.Amount),0) AS TotalAmount,
TblMemberDetailsSub.PayRound, TblMemberDetailsSub.PlanName
FROM TblMemberDetailsSub
WHERE (TblMemberDetailsSub.MemMode = 'Provider')
AND (TblMemberDetailsSub.CycleStatus = 'Uncompleted')
GROUP BY TblMemberDetailsSub.MemId, TblMemberDetailsSub.PayRound,
TblMemberDetailsSub.PlanName, TblMemberDetailsSub.Amount
ORDER BY TblMemberDetailsSub.MemId Asc
I just want both table result in single set (i user union but it is not working)
how can i merge both queries for single result
Group by in first query is not significant if you have only one row for each memberId in
TblStateMaster ,TblDistMaster ,TblTaluqaMaster also you are not selecting any value from TblMemberDetailsSub in first query . One way to merge the two queries is as follows:
SELECT (TblMemberDetails.MemFname +
' '+
TblMemberDetails.MemMname +
' '+
TblMemberDetails.MemLname)As Name,
TblStateMaster.StateName,
TblDistMaster.DistName,
TblTaluqaMaster.TaluqaName,
T.MemId,
T.TotalAmount,
T.PayRound,
T.PlanName
FROM
TblMemberDetails
INNER JOIN TblStateMaster ON TblMemberDetails.StateId = TblStateMaster.StateId
INNER JOIN TblDistMaster ON TblMemberDetails.DistId = TblDistMaster.DistId
INNER JOIN TblTaluqaMaster ON TblMemberDetails.TaluqaId = TblTaluqaMaster.TaluqaId
Inner join (
select TblMemberDetailsSub.MemId,
ISNULL(sum(TblMemberDetailsSub.Amount),0)As TotalAmount,
TblMemberDetailsSub.PayRound,
TblMemberDetailsSub.PlanName
from TblMemberDetailsSub
WHERE (TblMemberDetailsSub.MemMode = 'Provider')
AND (TblMemberDetailsSub.CycleStatus = 'Uncompleted')
group by TblMemberDetailsSub.MemId,TblMemberDetailsSub.PayRound,TblMemberDetailsSub.PlanName,TblMemberDetailsSub.Amount
) T ON TblMemberDetails.MemId = T.MemId
order by T.MemId Asc
I have a SQL Query :
SELECT * FROM Customer c
LEFT JOIN Invoice I ON I.InvcNum = C.Cust_InvcNum
some thing changed and the join does not work because there is no consistency in the data in the 'Cust_InvcNum'. So now when it does not find the record for the join or if it is null it needs to check for another condition.
LEFT JOIN Invoice I ON I.InvcNum = (SELECT p.InvcPrefix FROM Prefix WHERE p.DealerID = I.DealrID ) + '-' + I.InvcNum
second join I do works but it is taking too long for it get me data. Is there any other way to do this.
Earlier it used to be
Join on I.InvcNum = C.Cust_InvcNum
both the columns has the same data like DlrCd-InvcNum i.e both the columns 1234-A789
but not it could match on the above data or now the column 'InvcNum' in invoice table
can be populated like Dlrd-InvcNum or InvcPrefix-InvcNum
So InvcNum = 1234-A789 but CustNum = I94-A789
so we need to check if the for InvoicePrefix-InvcNum
SELECT *
FROM Customer c
LEFT JOIN (
SELECT i.InvcNum, p.InvcPrefix + '-' + I.InvcNum AS pInvcNum
FROM Invoice i LEFT JOIN Prefix p ON i.DealrID = p.DealrID
) ip ON c.Cust_InvcNum = CASE WHEN c.Cust_InvcNum = ip.InvcNum
THEN ip.InvcNum
ELSE ip.pInvcNum END