I need a SQL query that returns results matched by EITHER of the following SQL queries:
Query 1:
SELECT "annotations".* FROM "annotations" INNER JOIN "votes" ON "votes".voteable_id = "annotations".id AND "votes".voteable_type = 'Annotation'
WHERE (votes.vote = 't' AND votes.voter_id = 78)
Query 2:
SELECT "annotations".* FROM "annotations" INNER JOIN "songs" ON "songs".id = "annotations".song_id INNER JOIN "songs" songs_annotations ON "songs_annotations".id = "annotations".song_id INNER JOIN "users" ON "users".id = "songs_annotations".state_last_updated_by_id
WHERE (annotations.referent IS NOT NULL AND annotations.updated_at < '2010-04-05 01:51:24' AND (body = '?' OR body LIKE '%[?]%') AND ((users.id = songs.state_last_updated_by_id and users.needs_edit = 'f' and songs.state != 'work_in_progress') OR (songs.state = 'published'))
Here's what I tried, but it doesn't work:
SELECT "annotations".* FROM "annotations" INNER JOIN "songs" ON "songs".id = "annotations".song_id INNER JOIN "songs" songs_annotations ON "songs_annotations".id = "annotations".song_id INNER JOIN "users" ON "users".id = "songs_annotations".state_last_updated_by_id INNER JOIN "votes" ON "votes".voteable_id = "annotations".id AND "votes".voteable_type = 'Annotation' WHERE ((votes.vote = 't' and votes.voter_id = 78) OR (annotations.referent IS NOT NULL and annotations.updated_at < '2010-04-05 01:43:52' and (annotations.body = '?' OR annotations.body LIKE '%[?]%') and ((users.id = songs.state_last_updated_by_id and users.needs_edit = 'f') OR songs.state = 'published')))
UNION is the simplest. If your engine's optimizer doesn't do an efficient job with that, I might look deeper (but anything else would probably involve a lot of LEFT JOINs, since it looks you are joining two distinct uses of annotations):
SELECT "annotations".*
FROM "annotations"
INNER JOIN "votes"
ON "votes".voteable_id = "annotations".id
AND "votes".voteable_type = 'Annotation'
WHERE (votes.vote = 't' AND votes.voter_id = 78)
UNION
SELECT "annotations".*
FROM "annotations"
INNER JOIN "songs"
ON "songs".id = "annotations".song_id
INNER JOIN "songs" songs_annotations
ON "songs_annotations".id = "annotations".song_id
INNER JOIN "users"
ON "users".id = "songs_annotations".state_last_updated_by_id
WHERE (annotations.referent IS NOT NULL
AND annotations.updated_at < '2010-04-05 01:51:24'
AND (body = '?' OR body LIKE '%[?]%')
AND ((users.id = songs.state_last_updated_by_id and users.needs_edit = 'f' and songs.state != 'work_in_progress') OR (songs.state = 'published'))
For the same reason your INNER JOIN attempt failed (all join criteria must be satisfied), you would have to change almost everything to LEFT JOIN (or LEFT JOIN to a nested INNER JOIN) - I think the UNION is simplest.
SELECT "annotations".*
FROM "annotations"
LEFT JOIN "votes"
ON "votes".voteable_id = "annotations".id
AND "votes".voteable_type = 'Annotation'
LEFT JOIN "songs"
ON "songs".id = "annotations".song_id
LEFT JOIN "songs" songs_annotations
ON "songs_annotations".id = "annotations".song_id
LEFT JOIN "users"
ON "users".id = "songs_annotations".state_last_updated_by_id
WHERE (votes.vote = 't' AND votes.voter_id = 78)
OR
(annotations.referent IS NOT NULL
AND annotations.updated_at < '2010-04-05 01:51:24'
AND (body = '?' OR body LIKE '%[?]%')
AND ((users.id = songs.state_last_updated_by_id and users.needs_edit = 'f' and songs.state != 'work_in_progress') OR (songs.state = 'published'))
Related
I am attempting to remove the union in this sub-select due to performance issues. All attempts are unsuccessful because of how the AllocatedCRGReservedQty is calculated. How can I remove the union and combine the 2 select statements?
I was able to remove the union on the other portion of the query because it was based on a single where clause difference.
SELECT SrhCompanyID
,WhsPlantID
,SrhReason
,SrhRequestID
,SrdSeq
,SrdLotID
,SrdItemID
,InvQty
,SrdQty
,SrdSampleApprovedQty
,SrdUOM
,SohSalesOrder
,SohType
,AllocatedCRGReservedQty
FROM
( SELECT DISTINCT
SrhCompanyID
,WhsPlantID
,SrhReason
,SrhRequestID
,SrdSeq
,SrdLotID
,SrdItemID
,AsOfInventory InvQty
,SUM(SrdQty)
OVER (PARTITION BY SrdKey) SrdQty
,SUM(SrdSampleApprovedQty)
OVER (PARTITION BY SrdKey) SrdSampleApprovedQty
,SrdUOM
,SohSalesOrder
,SohType
,(SELECT SUM(SltLotAllocTranQty)
FROM SalesOrderLot
INNER JOIN
SalesOrderLotTran ON SltSolKey = SolKey
WHERE SolCompanyID = SodCompanyID AND
SolBranchID = SodBranchID AND
SolSalesOrder = SodSalesOrder AND
SolSalesOrderDtlKey = SodSalesOrderDtlKey AND
SolWipFgLotID = SrdLotID) AllocatedCRGReservedQty
FROM SampleRequestHdr
INNER JOIN SampleRequestDtl ON SrdSrhKey = SrhKey
INNER JOIN Warehouse ON WhsWhseID = SrhWhseID
INNER JOIN Lot ON LotCompanyID = SrhCompanyID AND
LotItemID = SrdItemID AND
LotID = SrdLotID
INNER JOIN #AsOfInventory ON IntLotKey = LotKey
-- Only include inventory transactions for completed production orders
-- Changed to temptable due to deadlocks on prodorder/prodorderreservation
INNER JOIN SalesOrderDtl ON SodCompanyID = SrhCompanyID AND
SodBranchID = SrdBranchID AND
SodSalesOrder = SrdSalesOrder AND
SodSalesOrderDtlKey = SrdSalesOrderDtlKey
INNER JOIN SalesOrderHdr ON SohCompanyID = SodCompanyID AND
SohBranchID = SodBranchID AND
SohSalesOrder = SodSalesOrder
WHERE SrhCompanyID = #CompanyID AND
((#PlantID <> '*' AND WhsPlantID = #PlantID) OR (#PlantID = '*')) AND
SrhApprovalExpireDate >= #As_Of_Date AND
ISNULL(SrdSampleRejected,'N') = 'N' AND
SohType IN ('Contract','Normal') AND
SrdQty <> 0
) WORK
WHERE ISNULL(AllocatedCRGReservedQty,0) < SrdQty -- if the lot is already allocated for at least the offered qty then don't include it so we don't over-state the gallons offered
UNION ALL -- then get the sample request qty offered for non-contract sales orders or no sales order assigned
SELECT SrhCompanyID
,WhsPlantID
,SrhReason
,SrhRequestID
,SrdSeq
,SrdLotID
,SrdItemID
,InvQty
,SrdQty
,SrdSampleApprovedQty
,SrdUOM
,SohSalesOrder
,SohType
,AllocatedCRGReservedQty
FROM
( SELECT DISTINCT
SrhCompanyID
,WhsPlantID
,SrhReason
,SrhRequestID
,SrdSeq
,SrdLotID
,SrdItemID
,AsOfInventory InvQty
,SUM(SrdQty)
OVER (PARTITION BY SrdKey) SrdQty
,SUM(SrdSampleApprovedQty)
OVER (PARTITION BY SrdKey) SrdSampleApprovedQty
,SrdUOM
,SohSalesOrder
,SohType
,(SELECT SUM(WptTranQty)
FROM WineProgram
CROSS APPLY
FN_CDV_WineProgramLotTranPlusPending(SrdLotID, WpgKey)
INNER JOIN
IntendedUse ON InuKey = WpgInuKey AND
InuIntendedUse = 'CRG'
LEFT JOIN
--ProdOrdDtl (NOLOCK) ON PodKey = wptPodKey
va_CDV_ProdOrder ON PodKey = wptPodKey
WHERE --WptLotID = SrdLotID AND
ISNULL(WptTranType,'') <> 'Pending' AND
((wptPodKey IS NOT NULL AND
(#CompletePOsOnly = 'N' OR -- include all POs
(#CompletePOsOnly = 'Y' AND PohCompleteDate IS NOT NULL))) -- Only include transactions if PO is Complete or if it's a non-PO transaction
OR
wptPodKey IS NULL
)
) AllocatedCRGReservedQty
FROM SampleRequestHdr
INNER JOIN SampleRequestDtl ON SrdSrhKey = SrhKey
INNER JOIN Warehouse ON WhsWhseID = SrhWhseID
INNER JOIN Lot ON LotCompanyID = SrhCompanyID AND
LotItemID = SrdItemID AND
LotID = SrdLotID
INNER JOIN
#AsOfInventory ON IntLotKey = LotKey
LEFT JOIN SalesOrderDtl ON SodCompanyID = SrhCompanyID AND
SodBranchID = SrdBranchID AND
SodSalesOrder = SrdSalesOrder AND
SodSalesOrderDtlKey = SrdSalesOrderDtlKey
LEFT JOIN SalesOrderHdr ON SohCompanyID = SodCompanyID AND
SohBranchID = SodBranchID AND
SohSalesOrder = SodSalesOrder
WHERE SrhCompanyID = #CompanyID AND
((#PlantID <> '*' AND WhsPlantID = #PlantID) OR (#PlantID = '*')) AND
SrhApprovalExpireDate >= #As_Of_Date AND
ISNULL(SrdSampleRejected,'N') = 'N' AND
ISNULL(SrdUseCRGReservation,'N') = 'N' AND -- if the SrdUseCRGReservation flag = "N" then return the sample qty
ISNULL(SohType,'') NOT IN ('Contract','Normal') AND -- offered to reduce the ATP (pseudo reservation until the sample expires or is approved/rejected)
SrdQty <> 0
) WORK
Good Morning. I have a query that has Group By and Union tables. The problem is when I try to order the final result. I'm always getting an error:
SELECT {Account}.[Id], {Account}.[AccountName], {Account}.[Project], {Account}.[Initiative], {Account}.[AccountName], {Application}.[Id], {Application}.[ApplicationName]
FROM {Account}
INNER JOIN {ApprovalWorkUnitDetail} ON {ApprovalWorkUnitDetail}.[AccountID] = {Account}.[Id]
INNER JOIN {Issue} ON {Issue}.[Id] = {ApprovalWorkUnitDetail}.[IssueID]
INNER JOIN {ServiceRequest} ON {ServiceRequest}.[Id] = {Issue}.[ServiceRequestId]
INNER JOIN {ServiceRequestProduct} ON {ServiceRequestProduct}.[ServiceRequestId] = {ServiceRequest}.[Id]
INNER JOIN {Product} ON {Product}.[Id] = {ServiceRequestProduct}.[ProductId]
LEFT JOIN {Application} ON {Application}.[Id] = {Account}.[ApplicationID]
WHERE ({ApprovalWorkUnitDetail}.[ContractID] = #ContractID)
AND ({ServiceRequest}.[Id] = #ServiceRequestID OR #ServiceRequestID = #NullIdentifier)
AND ({Product}.[Id] = #ProductID OR #ProductID = #NullIdentifier)
AND ({ApprovalWorkUnitDetail}.[MonthIdentifier] = #MonthIdentifier)
AND ({ApprovalWorkUnitDetail}.[YearIdentifier] = #YearIdentifier)
AND ({Issue}.[PaymentStatusID] = #PaymentStatusApprovedID)
UNION
SELECT {Account}.[Id], {Account}.[AccountName], {Account}.[Project], {Account}.[Initiative], {Account}.[AccountName], {Application}.[Id], {Application}.[ApplicationName]
FROM {Account}
INNER JOIN {ApprovalRefinementDetail} ON {ApprovalRefinementDetail}.[AccountID] = {Account}.[Id]
INNER JOIN {Worklog} ON {Worklog}.[Id] = {ApprovalRefinementDetail}.[WorklogID]
INNER JOIN {Issue} ON {Worklog}.[IssueId] = {Issue}.[Id]
INNER JOIN {ServiceRequest} ON {ServiceRequest}.[Id] = {Issue}.[ServiceRequestId]
INNER JOIN {ServiceRequestProduct} ON {ServiceRequestProduct}.[ServiceRequestId] = {ServiceRequest}.[Id]
INNER JOIN {Product} ON {Product}.[Id] = {ServiceRequestProduct}.[ProductId]
LEFT JOIN {Application} ON {Application}.[Id] = {Account}.[ApplicationID]
WHERE ({ApprovalRefinementDetail}.[ContractID] = #ContractID)
AND ({Product}.[Id] = #ProductID OR #ProductID = #NullIdentifier)
AND ({ServiceRequest}.[Id] = #ServiceRequestID OR #ServiceRequestID = #NullIdentifier)
AND ({ApprovalRefinementDetail}.[MonthIdentifier] = #MonthIdentifier)
AND ({ApprovalRefinementDetail}.[YearIdentifier] = #YearIdentifier)
AND ({Worklog}.[PaymentStatusID] = #PaymentStatusApprovedID)
GROUP BY {Account}.[Id], {Account}.[AccountName], {Account}.[Project], {Account}.[Initiative], {Account}.[AccountName], {Application}.[Id], {Application}.[ApplicationName]
ORDER BY {Account}.[AccountName]
I don't know why I am always getting error ORA-00904 and it indicates that AccountName is an invalid identifier.
I am using ORACLE database.
Many thanks
I would recommend moving your union query to a subquery, and sorting in the outer scope:
select *
from (
-- your big query
) t
order by accountname
Hoping you can help.
We have an app that displays a grid. We can add custom fields to the grid using subqueries with which I am struggling. The main grid query looks like this.
SELECT TOP 300000
'' AS alloc_status
,'' AS stock_status
,wo_description
,wo_quantity
,wo_number
,wo_pwos_id
,vad_variant_code
,oh_order_number
,oh_datetime
,ohd_dm_id
,ohd_customer_name
,vad_description AS vad_description_Condition
,vad_variant_code AS vad_variant_code_Condition
,wo_id AS key_id
FROM works_order
LEFT OUTER JOIN works_order_analysis
ON works_order_analysis.woa_wo_id = works_order.wo_id
LEFT OUTER JOIN works_order_process
ON works_order_process.wop_id = works_order.wo_current_wop_id
LEFT OUTER JOIN works_order_process_analysis
ON works_order_process_analysis.wopa_wop_id = works_order_process.wop_id
INNER JOIN variant_detail
ON variant_detail.vad_id = works_order.wo_vad_id
LEFT OUTER JOIN works_order_total
ON works_order.wo_id = works_order_total.wot_wo_id
LEFT JOIN order_line_item
ON oli_id = wo_oli_id
LEFT JOIN order_header
ON oh_id = oli_oh_id
LEFT JOIN job_number
ON jn_id = wo_jn_id
LEFT JOIN works_order_process_subcontract_analysis
ON wopsa_wop_id = wop_id
LEFT JOIN order_header_detail
ON ohd_oh_id = oh_id
LEFT JOIN customer_detail
ON cd_id = oh_cd_id
WHERE ((cd_ow_account = 'NOTHS')
AND (wo_pwos_id = 1)
OR (cd_ow_account = 'EBAY')
AND (wo_pwos_id = 1)
OR (cd_ow_account = '4008')
AND (wo_pwos_id = 1)
OR (cd_ow_account = 'TRUSCA')
AND (wo_pwos_id = 1))
AND ((wo_required_datetime <= '2016-12-24 23:59:59'
OR wo_required_datetime IS NULL)
AND (wo_wos_id <> 4)
AND (wo_kit = 0))
The subquery is to basically provide a column that looks at the sales order and returns a count of row within that order. When I do this I get the following:
There was a problem retrieving the data:
subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, < etc
or when the subquery is used as an expression
Below is the query complete with subquery. If anyone can help that would be amazing
SELECT TOP 300000
'' AS alloc_status
,'' AS stock_status
,wo_description
,wo_quantity
,wo_number
,wo_pwos_id
,vad_variant_code
,oh_order_number
,oh_datetime
,ohd_dm_id
,ohd_customer_name
,(SELECT
COUNT(order_line_item.oli_id) AS 'Count'
FROM dbo.order_line_item
INNER JOIN dbo.order_header
ON order_line_item.oli_oh_id = order_header.oh_id
INNER JOIN dbo.variant_detail
ON order_line_item.oli_vad_id = variant_detail.vad_id
INNER JOIN dbo.variant_setting
ON variant_setting.vas_vad_id = variant_detail.vad_id
WHERE variant_setting.vas_manufactured_variant = 1
GROUP BY order_header.oh_order_number)
AS CustomLineCount
,vad_description AS vad_description_Condition
,vad_variant_code AS vad_variant_code_Condition
,wo_id AS key_id
FROM works_order
LEFT OUTER JOIN works_order_analysis
ON works_order_analysis.woa_wo_id = works_order.wo_id
LEFT OUTER JOIN works_order_process
ON works_order_process.wop_id = works_order.wo_current_wop_id
LEFT OUTER JOIN works_order_process_analysis
ON works_order_process_analysis.wopa_wop_id = works_order_process.wop_id
INNER JOIN variant_detail
ON variant_detail.vad_id = works_order.wo_vad_id
LEFT OUTER JOIN works_order_total
ON works_order.wo_id = works_order_total.wot_wo_id
LEFT JOIN order_line_item
ON oli_id = wo_oli_id
LEFT JOIN order_header
ON oh_id = oli_oh_id
LEFT JOIN job_number
ON jn_id = wo_jn_id
LEFT JOIN works_order_process_subcontract_analysis
ON wopsa_wop_id = wop_id
LEFT JOIN order_header_detail
ON ohd_oh_id = oh_id
LEFT JOIN customer_detail
ON cd_id = oh_cd_id
WHERE ((cd_ow_account = 'NOTHS')
AND (wo_pwos_id = 1)
OR (cd_ow_account = 'EBAY')
AND (wo_pwos_id = 1)
OR (cd_ow_account = '4008')
AND (wo_pwos_id = 1)
OR (cd_ow_account = 'TRUSCA')
AND (wo_pwos_id = 1))
AND ((wo_required_datetime <= '2016-12-24 23:59:59'
OR wo_required_datetime IS NULL)
AND (wo_wos_id <> 4)
AND (wo_kit = 0))
Try the below query, it may helps you
SELECT TOP 300000
'' AS alloc_status
,'' AS stock_status
,wo_description
,wo_quantity
,wo_number
,wo_pwos_id
,vad_variant_code
,oh_order_number
,oh_datetime
,ohd_dm_id
,ohd_customer_name
,(SELECT
COUNT(order_line_item.oli_id) AS 'Count'
FROM dbo.order_line_item
INNER JOIN dbo.order_header
ON order_line_item.oli_oh_id = order_header.oh_id
INNER JOIN dbo.variant_detail
ON order_line_item.oli_vad_id = variant_detail.vad_id
INNER JOIN dbo.variant_setting
ON variant_setting.vas_vad_id = variant_detail.vad_id
WHERE variant_setting.vas_manufactured_variant = 1
and order_header.oh_order_number=ordhead.oh_order_number)
AS CustomLineCount
,vad_description AS vad_description_Condition
,vad_variant_code AS vad_variant_code_Condition
,wo_id AS key_id
FROM works_order
LEFT OUTER JOIN works_order_analysis
ON works_order_analysis.woa_wo_id = works_order.wo_id
LEFT OUTER JOIN works_order_process
ON works_order_process.wop_id = works_order.wo_current_wop_id
LEFT OUTER JOIN works_order_process_analysis
ON works_order_process_analysis.wopa_wop_id = works_order_process.wop_id
INNER JOIN variant_detail
ON variant_detail.vad_id = works_order.wo_vad_id
LEFT OUTER JOIN works_order_total
ON works_order.wo_id = works_order_total.wot_wo_id
LEFT JOIN order_line_item
ON oli_id = wo_oli_id
LEFT JOIN order_header ordhead
ON oh_id = oli_oh_id
LEFT JOIN job_number
ON jn_id = wo_jn_id
LEFT JOIN works_order_process_subcontract_analysis
ON wopsa_wop_id = wop_id
LEFT JOIN order_header_detail
ON ohd_oh_id = oh_id
LEFT JOIN customer_detail
ON cd_id = oh_cd_id
WHERE ((cd_ow_account = 'NOTHS')
AND (wo_pwos_id = 1)
OR (cd_ow_account = 'EBAY')
AND (wo_pwos_id = 1)
OR (cd_ow_account = '4008')
AND (wo_pwos_id = 1)
OR (cd_ow_account = 'TRUSCA')
AND (wo_pwos_id = 1))
AND ((wo_required_datetime <= '2016-12-24 23:59:59'
OR wo_required_datetime IS NULL)
AND (wo_wos_id <> 4)
AND (wo_kit = 0))
Change your sub query to this:
(SELECT
COUNT(order_line_item.oli_id) AS 'Count'
FROM dbo.order_line_item
INNER JOIN dbo.order_header
ON order_line_item.oli_oh_id = order_header.oh_id
INNER JOIN dbo.variant_detail
ON order_line_item.oli_vad_id = variant_detail.vad_id
INNER JOIN dbo.variant_setting
ON variant_setting.vas_vad_id = variant_detail.vad_id
WHERE variant_setting.vas_manufactured_variant = 1
AND order_header.oh_order_number = OUTER_QUERY_TABLE.SAME_FIELD )
I've added this line:
AND order_header.oh_order_number = OUTER_QUERY_TABLE.SAME_FIELD
You have to make this query correlated , so add the condition to match the record to the outer query . I didn't know which field it is.
You could also optimize your WHERE and make it more readable with IN() :
WHERE wo_pwos_id = 1
AND cd_ow_account IN('NOTHS','EBAY','4008','TRUSCA')
AND (wo_required_datetime <= '2016-12-24 23:59:59' OR wo_required_datetime IS NULL)
AND wo_wos_id <> 4
AND wo_kit = 0
I am trying to create this procedure:
CREATE OR REPLACE PROCEDURE USP_DAILY_REP_REG_REFRESH
IS
BEGIN
SAVEPOINT start_tran;
EXECUTE immediate 'truncate table rep_fir_acts';
-- Insert Query
INSERT
INTO rep_fir_acts
(
PS_CD,
LANG_CD,
FIR_REG_NUM,
FIR_DT,
ACT_CD,
PEND_INV_FIR_NUM,
FINAL_REP_SRNO,
CHARGESHEET_DT,
INVTG_STATUS_CD,
FR_TYPE_CD,
FINAL_REPORT_TYPE_CD,
PEND_TRIAL_FIR,
DISPOSAL_NATURE_CD,
ACTION_TAKEN_CD,
FIR_STATUS
)
SELECT DISTINCT F.PS_CD,
F.LANG_CD,
F.FIR_REG_NUM,
F.REG_DT FIR_DT,
C.ACT_CD ,
fnl.FIR_REG_NUM PEND_INV_FIR_NUM,
fnl.FINAL_REP_SRNO ,
fnl.CHARGESHEET_DT,
fnl.INVTG_STATUS_CD,
fnl.FR_TYPE_CD,
fnl.FINAL_REPORT_TYPE_CD ,
dis.FIR_REG_NUM PEND_TRIAL_FIR,
dis.DISPOSAL_NATURE_CD ,
f.ACTION_TAKEN_CD,
f.FIR_STATUS
FROM t_fir_registration f
INNER JOIN t_crime_act_section c
ON f.fir_reg_num = c.fir_reg_num
AND f.lang_cd = c.lang_cd
INNER JOIN t_crime_detail cd
ON c.fir_reg_num = cd.fir_reg_num
AND c.lang_cd = cd.lang_cd
AND c.CRM_DETAIL_SRNO = cd.CRM_DETAIL_SRNO
AND cd.RECORD_STATUS = 'C'
LEFT JOIN t_final_report fnl
ON f.fir_reg_num = fnl.fir_reg_num
AND f.lang_cd = fnl.lang_cd
AND IS_CHARGESHEET_ORIGINAL = 'Y'
AND fnl.final_rep_srno IN
(SELECT MAX(final_rep_srno)
FROM t_final_report fn
WHERE f.fir_reg_num = fn.fir_reg_num
AND f.lang_cd = fn.lang_cd
AND fn.IS_CHARGESHEET_ORIGINAL = 'Y'
)
LEFT JOIN t_court_disposal dis
ON f.fir_reg_num = dis.fir_reg_num
AND dis.DISPOSAL_SRNO IN
(SELECT MAX(DISPOSAL_SRNO)
FROM t_court_disposal cdis
WHERE dis.fir_reg_num = cdis.fir_reg_num
)
WHERE c.act_cd <> 0
AND f.fir_status NOT IN(2,20,104,103);
EXCEPTION
WHEN OTHERS THEN
ROLLBACK TO start_tran;
RAISE;
END;
When I try to run this I get:
PL/SQL: SQL Statement ignored
ORA-01799: a column may not be outer-joined to a subquery with insert
statement
How can I resolve this problem?
You need to convert what you're left joining into subqueries, rather than having the subquery in the join conditions. E.g., something like:
SELECT DISTINCT f.ps_cd,
f.lang_cd,
f.fir_reg_num,
f.reg_dt fir_dt,
c.act_cd,
fnl.fir_reg_num pend_inv_fir_num,
fnl.final_rep_srno,
fnl.chargesheet_dt,
fnl.invtg_status_cd,
fnl.fr_type_cd,
fnl.final_report_type_cd,
dis.fir_reg_num pend_trial_fir,
dis.disposal_nature_cd,
f.action_taken_cd,
f.fir_status
FROM t_fir_registration f
INNER JOIN t_crime_act_section c
ON f.fir_reg_num = c.fir_reg_num
AND f.lang_cd = c.lang_cd
INNER JOIN t_crime_detail cd
ON c.fir_reg_num = cd.fir_reg_num
AND c.lang_cd = cd.lang_cd
AND c.crm_detail_srno = cd.crm_detail_srno
AND cd.record_status = 'C'
LEFT JOIN (SELECT fir_reg_num,
lang_cd,
final_rep_srno,
chargesheet_dt,
invtg_status_cd,
fr_type_cd,
final_report_type_cd
FROM (SELECT fnl1.*,
MAX(fnl1.final_rep_srno) OVER (PARTITION BY fnl1.fir_reg_num, fnl1.lang_cd) max_final_rep_srno
FROM t_final_report fnl1
WHERE fnl1.is_chargesheet_original = 'Y')
WHERE final_rep_srno = max_final_rep_srno) fnl
ON f.fir_reg_num = fnl.fir_reg_num
AND f.lang_cd = fnl.lang_cd
LEFT JOIN (SELECT fir_reg_num,
disposal_nature_cd
FROM (SELECT dis1.*,
MAX(disposal_srno) OVER (PARTITION BY dis1.fir_reg_num) max_disposal_srno
FROM t_court_disposal dis1)
WHERE disposal_srno = max_disposal_srno) dis
ON f.fir_reg_num = dis.fir_reg_num
WHERE c.act_cd <> 0
AND f.fir_status NOT IN (2, 20, 104, 103);
I'm fairly new to writing queries and I'm struggling with getting the next to queries combined:
SELECT od.ODCode
, c.Description as 'Line Position'
, ps.Description as 'Pallet Stacking'
, pt.Description as 'Pallet Type'
, odex.PalletRows
, odex.PalletTypeID
,CONCAT(( SELECT sp.SpecPropertyValue FROM SpecificationProperty sp
INNER JOIN specificationclass sc ON sc.specclassid = sp.specclassid
INNER JOIN specificationcode sco ON sco.specclassid = sc.specclassid and sco.speccodeid = pt.pallettypeid
INNER JOIN SpecificationClassProperty scp ON scp.specClassid = sp.specclassid and scp.specpropertyid = sp.specpropertyid and sco.speccodeid = sp.speccodeid
WHERE scp.specpropertyname = 'DocDir'and sc.specclassname = 'Pallettypedoc'
),
(SELECT sp.SpecPropertyValue FROM SpecificationProperty sp
INNER JOIN specificationclass sc ON sc.specclassid = sp.specclassid
INNER JOIN specificationcode sco ON sco.specclassid = sc.specclassid and sco.speccodeid = pt.pallettypeid
INNER JOIN SpecificationClassProperty scp ON scp.specClassid = sp.specclassid and scp.specpropertyid = sp.specpropertyid and sco.speccodeid = sp.speccodeid
WHERE scp.specpropertyname = 'Documentname'and sc.specclassname = 'Pallettypedoc' )) AS Spec
FROM ODExitPallet odex
INNER JOIN OperationDescr od
ON od.OperationDescrID=odex.OperationDescrID
INNER JOIN PalletStacking ps
ON ps.PalletStackingID=odex.PalletStackingID
INNER JOIN PalletType pt
ON pt.PalletTypeID=odex.PalletTypeID
INNER JOIN Code c
on c.CodeNumber=odex.LinePosition and c.CodeTypeID=72
INNER JOIN WorkOrder wo
on wo.OperationDescrID=odex.OperationDescrID
WHERE wo.WorkOrderID = #WorkOrderID
and
SELECT SUM(PlannedBatch) / SUM(NominalBlanks * Stacks) as NrOfPallets
FROM (SELECT WO.PlannedBatch, WO.NominalBlanks, ODEP.OperationDescrID, LinePosition,
Case when COUNT(*) = 0 then 1
else COUNT(*)
end as Stacks
FROM WorkOrder Wo
INNER JOIN ODExitPallet ODEX
ON WO.operationDescrID = ODEX.OperationDescrID
INNER JOIN Stacking ST
ON ST.PalletStackingID = ODEX.PalletStackingID
WHERE WO.WorkOrderID = #WorkOrderID
GROUP BY PlannedBatch, NominalBlanks, ODEX.OperationDescrID, LinePosition
) TOTAL
So I want to add the second query as a field in my first one. I've tried it with numerous thing like parentheses and stuff but I keep getting errors. Can someone enlighten me?
insert ( ) in whole second Query but make sure you second query return only 1 row
SELECT od.ODCode
, c.Description as 'Line Position'
, ps.Description as 'Pallet Stacking'
, pt.Description as 'Pallet Type'
, odex.PalletRows
, odex.PalletTypeID
,CONCAT(
(
SELECT sp.SpecPropertyValue
FROM SpecificationProperty sp
INNER JOIN specificationclass sc ON sc.specclassid = sp.specclassid
INNER JOIN specificationcode sco ON sco.specclassid = sc.specclassid and sco.speccodeid = pt.pallettypeid
INNER JOIN SpecificationClassProperty scp ON scp.specClassid = sp.specclassid and scp.specpropertyid = sp.specpropertyid and sco.speccodeid = sp.speccodeid
WHERE scp.specpropertyname = 'DocDir'and sc.specclassname = 'Pallettypedoc'
)
,
(
SELECT sp.SpecPropertyValue FROM SpecificationProperty sp
INNER JOIN specificationclass sc ON sc.specclassid = sp.specclassid
INNER JOIN specificationcode sco ON sco.specclassid = sc.specclassid and sco.speccodeid = pt.pallettypeid
INNER JOIN SpecificationClassProperty scp ON scp.specClassid = sp.specclassid and scp.specpropertyid = sp.specpropertyid and sco.speccodeid = sp.speccodeid
WHERE scp.specpropertyname = 'Documentname'and sc.specclassname = 'Pallettypedoc'
)
) AS Spec
,(
SELECT SUM(PlannedBatch) / SUM(NominalBlanks * Stacks)
FROM (
SELECT WO.PlannedBatch, WO.NominalBlanks, ODEP.OperationDescrID, LinePosition,
Case when COUNT(*) = 0 then 1
else COUNT(*)
end as Stacks
FROM WorkOrder Wo
INNER JOIN ODExitPallet ODEX
ON WO.operationDescrID = ODEX.OperationDescrID
INNER JOIN Stacking ST
ON ST.PalletStackingID = ODEX.PalletStackingID
WHERE WO.WorkOrderID = #WorkOrderID
GROUP BY PlannedBatch, NominalBlanks, ODEX.OperationDescrID, LinePosition
) TOTAL
) AS [NrOfPallets]
FROM ODExitPallet odex
INNER JOIN OperationDescr od ON od.OperationDescrID=odex.OperationDescrID
INNER JOIN PalletStacking ps ON ps.PalletStackingID=odex.PalletStackingID
INNER JOIN PalletType pt ON pt.PalletTypeID=odex.PalletTypeID
INNER JOIN Code c on c.CodeNumber=odex.LinePosition and c.CodeTypeID=72
INNER JOIN WorkOrder wo on wo.OperationDescrID=odex.OperationDescrID
WHERE wo.WorkOrderID = #WorkOrderID