ORA-00904: "S"."AIR_TIME": invalid identifier - oracle

Why does this code show invalid identifier when sum is used in distance and air_time column?
When sum is not used this statement process successfully but using sum I get error? I need to use sum for this statement.
MERGE INTO FACT_COMPANY_GROWTH F
USING (SELECT DISTINCT TIME_ID, FLIGHT_KEY, AEROPLANE_KEY, SUM(DISTANCE) AS TOTAL_DISTANCE, SUM(AIR_TIME) AS TOTAL_AIRTIME
FROM TRANSFORM_FLIGHT T
INNER JOIN TRANSFORM_AEROPLANE A
ON T.FK_AEROPLANE_KEY = A.AEROPLANE_KEY
INNER JOIN DIM_TIME D
ON D.YEAR = T.YEAR
AND D.MONTH = T.MONTH
GROUP BY TIME_ID, FLIGHT_KEY, AEROPLANE_KEY) S
ON (F.FK1_TIME_ID = S.TIME_ID
AND F.FK2_FLIGHT_KEY = S.FLIGHT_KEY
AND F.FK3_AEROPLANE_KEY = S.AEROPLANE_KEY
)
WHEN MATCHED THEN
UPDATE SET
F.TOTAL_AIRTIME = S.AIR_TIME,
F.TOTAL_DISTANCE = S.DISTANCE,
F.TOTAL_NO_OF_FLIGHTS = S.FLIGHT_KEY,
F.TOTAL_NO_OF_AEROPLANE = S.AEROPLANE_KEY
WHEN NOT MATCHED THEN
INSERT(FACT_ID, FK1_TIME_ID, FK2_FLIGHT_KEY, FK3_AEROPLANE_KEY, TOTAL_DISTANCE, TOTAL_AIRTIME, TOTAL_NO_OF_FLIGHTS, TOTAL_NO_OF_AEROPLANE)
VALUES
(NULL, S.TIME_ID, S.FLIGHT_KEY, S.AEROPLANE_KEY, S.DISTANCE, S.AIR_TIME, S.FLIGHT_KEY, S.AEROPLANE_KEY);

USING(
SELECT DISTINCT
TIME_ID,
FLIGHT_KEY,
AEROPLANE_KEY,
SUM(DISTANCE) AS TOTAL_DISTANCE,
SUM(AIR_TIME) AS TOTAL_AIRTIME
...) S
The problem is at UPDATE SET F.TOTAL_AIRTIME = S.AIR_TIME. There are 5 fields defined in S and none is named AIR_TIME.

UPDATE SET
F.TOTAL_AIRTIME = S.TOTAL_AIRTIME,
F.TOTAL_DISTANCE = S.TOTAL_DISTANCE,

Related

Oracle Query not giving result though outer join is present

In the below query when i comment one line AND EVT.EVT_EVNT_ID(+) = DPT.DPT_EVNT_ID or add outer join (+) in one line AND EVT.EVT_ENTITY_ID ='G' the query will fetch results, else it will not give any results.
`
select * from
(select B11.ext_db_id,B11.FRST_LINE_OF_PRFRD_NAME, E11.*, D1.D1_evnt_id,
(SELECT E1.E1_DT
FROM S1core.E1_EVNT_DT_DTLS E1
WHERE E1.E1_ENTITY_ID = D1.D1_ENTITY_ID
AND E1.E1_EVNT_ID = D1.D1_EVNT_ID
AND E1.E1_D1_ID = D1.D1_D1_ID
AND E1.E1_DT_TYP = 'MEET'
AND E1.E1_OPTN_SEQ_N = '999'
AND E1.STATUS = 'AUTHD' ) "MEET_DATE"
FROM S1core.E1_EVNT_DT_DTLS E11, S1core.SECURITY_ACCOUNT SFA,
S1core.EXTRNL_SYS_DETAILS EXT,
S1core.D1_DPOT_EVNT_DTLS D1, S1core.IP_ACNT_RELATION i1,
S1core.ELG_ELGBLTY E1,S1core.P1_D1_PRXY_DTLS P1, S1core.EVT_DTLS EVT,
S1core.BUSINESS_PARTNER B1, S1core.BUSINESS_PARTNER B11
WHERE
P1.P1_EVNT_ID(+) = D1.D1_EVNT_ID
AND B1.BP_ID = i1.IP_ID
AND SFA.BP_ID = B11.BP_ID
AND B11.OWNER_ENTITY = SFA.OWNER_ENTITY
AND P1.P1_D1_ID(+) = D1.D1_D1_ID
AND P1.P1_ENTITY_ID(+) = D1.D1_ENTITY_ID
AND P1.STATUS(+) = 'AUTHD'
AND EXT.LVL_REF = SFA.SCA_REF
AND EXT.OWNER_ENTITY = SFA.owner_entity
AND EXT.EXTRNL_SYS_ID = '39'
AND EXT.BP_ID = SFA.BP_ID
AND EXT.LVL = 5
AND E1.ELG_SEC_ID = D1.D1_SEC_ID
AND D1.D1_ENTITY_ID = E1.ELG_ENTITY_ID
AND D1.D1_EVNT_ID = E1.ELG_EVNT_ID
AND D1.D1_D1_ID = E1.ELG_D1_ID
AND E1.ELG_ENTITY_ID = SFA.OWNER_ENTITY
AND E1.ELG_ACNT_ID = SFA.SCA_REF
AND i1.owner_entity = SFA.OWNER_ENTITY
AND i1.owner_entity = B1.OWNER_ENTITY
AND i1.SCA_REF = SFA.SCA_REF
AND i1.stat <> 2
AND EVT.EVT_ENTITY_ID ='G'
AND EVT.EVT_EVNT_ID(+) = D1.D1_EVNT_ID
AND EVT.STATUS(+) = 'AUTHD'
AND E11.E1_ENTITY_ID = D1.D1_ENTITY_ID
AND E11.E1_EVNT_ID = D1.D1_EVNT_ID
AND E11.E1_D1_ID = D1.D1_D1_ID
AND D1.D1_EVNT_GRP = 'MEETING'
AND E1.ELG_FNL_ELGBL_QNTTY > 0
AND D1.D1_ENTITY_ID = 'GSSIN' -- P_D1_ENTITY_ID
AND B11.ext_db_id LIKE 'LICMF' -- P_GLOBAL_CUSTODIAN
AND B1.ext_db_id LIKE '%' -- P_FUND_MANAGER
AND SUBSTR(EXT.LEVEL_EXTNL_SYS,1,5) LIKE '%' -- P_SUB_ACCOUNT_ID
AND SUBSTR(EXT.LEVEL_EXTNL_SYS,6,9) LIKE '%' -- P_SCHEMA_ID
AND SUBSTR( D1.D1_EVNT_TYP, 1, 4 ) LIKE '%' -- P_EVENT_TYPE
AND E11.E1_DT_TYP = 'MEET'
and D1.D1_evnt_id='12457886544'
AND D1.OU_ID IN('17','80') -- ('S1 INDIA PC')
) ;
Am I missing any outer join or the query is having any other issue as i can see that dpt_evnt_id='12457886544'is available in all tables.
`
I tried to run this query by commenting AND EVT.EVT_EVNT_ID(+) = DPT.DPT_EVNT_ID
and by adding outer join in EVT.EVT_ENTITY_ID (+) ='G'
You have:
AND EVT.EVT_ENTITY_ID ='G'
AND EVT.EVT_EVNT_ID(+) = D1.D1_EVNT_ID
AND EVT.STATUS(+) = 'AUTHD'
Even though the second two have (+) the first one does not so the join to EVT will be treated as an INNER JOIN and not an OUTER JOIN.
You should convert your code to use ANSI joins as it will be much easier to spot issues like this.
Note: There may be more issues like this. However, your code has so many joins, the syntax is archaic, and we have no way of validating the correctness that I stopped looking after finding the first issue. You are going to need to debug it all to check for any other issues.

GORM hangs with parameter

The below queries hangs exactly after 5 calls every time,
tx := db.Raw("select count(*) as hash from transaction_logs left join blocks on transaction_logs.block_number = blocks.number"+
" where (transaction_logs.address = ? and transaction_logs.topic0 = ?) and blocks.total_confirmations >= 7 "+
"group by transaction_hash", strings.ToLower("0xa11265a58d9f5a2991fe8476a9afea07031ac5bf"),
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef").Scan(&totalIds)
If we replace it without the arguments it works
db.Raw("select count(*) as hash from transaction_logs left join blocks on transaction_logs.block_number = blocks.number"+
" where (transaction_logs.address = #tokenAddress and transaction_logs.topic0 = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef') and blocks.total_confirmations >= 7 "+
"group by transaction_hash", sql.Named("tokenAddress", strings.ToLower("0xa11265a58d9f5a2991fe8476a9afea07031ac5bf"))
Tried even with named parameter, same result
Can anyone help here

Error with group by expression?

SELECT DISTINCT dha.order_number,
dha.ordered_date,
dha.org_id,
houf.name Business_Unit,
dha.TRANSACTIONAL_CURRENCY_CODE,
dla.ORDERED_QTY,
dla.UNIT_SELLING_PRICE,
esib.item_number,
esit.description,
hca.account_name,
hzps.PARTY_SITE_NAME,
cicev.UNIT_COST_AVERAGE,
MAX (cicev.cost_date) AS MaxDate
FROM DOO_HEADERS_ALL dha,
DOO_PRICE_ADJUSTMENTS dpa,
hr_organization_units_f_tl houf,
DOO_LINES_ALL dla,
DOO_FULFILL_LINES_ALL dfla,
egp_system_items_b esib,
egp_system_items_tl esit,
hz_cust_accounts hca,
HZ_party_sites hzps,
CST_ITEM_COST_ELEMENTS_V cicev
WHERE dha.org_id = houf.organization_id
AND dha.header_id = dla.header_id
AND esib.inventory_item_id = dfla.inventory_item_id
AND dfla.inventory_item_id = esit.INVENTORY_ITEM_ID
AND dfla.header_id = dha.header_id
AND dha.SOLD_TO_PARTY_ID = hca.party_id
AND dha.SOLD_TO_PARTY_ID = hzps.party_id
AND esit.inventory_item_id = cicev.inventory_item_id
GROUP BY (dha.order_number)
All columns that aren't aggregated must be included into the GROUP BY clause, which means that it should look like this; note that column aliases must be removed!
GROUP BY dha.order_number,
dha.ordered_date,
dha.org_id,
houf.name,
dha.TRANSACTIONAL_CURRENCY_CODE,
dla.ORDERED_QTY,
dla.UNIT_SELLING_PRICE,
esib.item_number,
esit.description,
hca.account_name,
hzps.PARTY_SITE_NAME,
cicev.UNIT_COST_AVERAGE
Apart from that, there's no need for DISTINCT (in SELECT) because GROUP BY will select distinct values anyway.
[EDIT: how to include a new condition into the WHERE clause?]
select ...
from ...
where cicev.cost_date = (select max(cicev1.cost_date)
from CST_ITEM_COST_ELEMENTS_V cicev1
where cicev1.inventory_item_id = cicev.inventory_item_id
)
and ...

1 result set from 3 queries?

I'm at my wits end attempting to make this happen. Currently I have 3 separate queries. For automation purposes I need to get the results of these three queries in one output, I cannot seem to join them properly to get the expected results.
QUERY 1:
SELECT OH.EXTN_HOST_ORDER_REF,
OL.EXTN_HOST_ORDER_LINE_REF,
OL.ORIGINAL_ORDERED_QTY,
OL.EXTN_TENDER_QUANTITY,
OL.EXTN_CUM_PICK_QTY,
OL.SHIPPED_QUANTITY,
OL.EXTN_REFUND_QTY
FROM YFS_ORDER_HEADER OH,
YFS_ORDER_LINE OL
WHERE OH.ORDER_HEADER_KEY = OL.ORDER_HEADER_KEY
AND OH.DOCUMENT_TYPE = '0001'
AND OH.EXTN_HOST_ORDER_REF = 'xxxxxxxxxxx'
ORDER BY PL.EXTN_HOST_ORDER_LINE_REF ASC;
QUERY 2:
SELECT RS.STATUS_QUANTITY AS RETURNED_QTY
FROM YFS_ORDER_HEADER OH,
YFS_ORDER_LINE OL,
YFS_ORDER_RELEASE_STATUS RS
WHERE OH.ORDER_HEADER_KEY = OL.ORDER_HEADER_KEY
AND OL.ORDER_LINE_KEY = RS.ORDER_LINE_KEY
AND RS.STATUS = '3700.02'
AND OH.EXTN_HOST_ORDER_REF = 'xxxxxxxxxxx';
QUERY 3
SELECT RS.STATUS_QUANTITY AS CANCELLED_QTY
FROM YFS_ORDER_HEADER OH,
YFS_ORDER_LINE OL,
YFS_ORDER_RELEASE_STATUS RS
WHERE OH.ORDER_HEADER_KEY = OL.ORDER_HEADER_KEY
AND OL.ORDER_LINE_KEY = RS.ORDER_LINE_KEY
AND RS.STATUS = '9000'
AND OH.EXTN_HOST_ORDER_REF = 'xxxxxxxxxxx';
The query should show NULL values where no data exists from query 2 & 3.
Thanks in advance for your help and advice!
If you are allowed to show both returned and cancelled quantities, the following simple edit should work. Hope this helps.
SELECT oh.extn_host_order_ref,
ol.extn_host_order_line_ref,
ol.original_ordered_qty,
ol.extn_tender_quantity,
ol.extn_cum_pick_qty,
ol.shipped_quantity,
ol.extn_refund_qty,
DECODE (rs.status, '3700.02', rs.status_quantity) AS returned_qty,
DECODE (rs.status, '9000', rs.status_quantity) AS cancelled_qty
FROM yfs_order_header oh
INNER JOIN yfs_order_line ol
ON oh.order_header_key = ol.order_header_key
LEFT OUTER JOIN yfs_order_release_status rs
ON ol.order_line_key = rs.order_line_key
WHERE oh.document_type = '0001' AND oh.extn_host_order_ref = 'xxxxxxxxxxx'
ORDER BY pl.extn_host_order_line_ref ASC;

Update/Merge multiple rows with select statement data

Im trying to update a single table with output of a select statement which returns two values. One is the accountid that I need to update in the table, and the other is the information I need to update.
Here is my select statement
select CTP.CARETEAMPATIENTID, O.ORGANIZATIONID
from acts.careteampatient ctp,
ods.member m,
ods.supplierorganization so,
ods.MASTERSUPPLIERSUPPLIERRELATION mssr,
ods.INSURANCEORGSUPPLIERRELATION IOSR,
ods.INSURANCEORGANIZATION IO,
acts.organization o
where ctp.accountorgid is null
and m.primarymemberplanid = ctp.primarymemberplanid
and SO.AHMSUPPLIERID = M.AHMSUPPLIERID
and mssr.SUPPLIERID = so.SUPPLIERORGID
AND iosr.SUPPLIERID = so.SUPPLIERORGID
AND io.INSURANCEORGID = iosr.INSURANCEORGID
and io.processingmodecd = 'P'
and so.usagemnemonic = 'P'
and O.ODSACCOUNTID = IO.INSURANCEORGID
and O.ACCOUNTFLG = 'Y'
I want to do something along the lines of
update careteampatient
from (select CTP.CARETEAMPATIENTID patientid, O.ORGANIZATIONID orgid
from acts.careteampatient ctp,
ods.member m,
ods.supplierorganization so,
ods.MASTERSUPPLIERSUPPLIERRELATION mssr,
ods.INSURANCEORGSUPPLIERRELATION IOSR,
ods.INSURANCEORGANIZATION IO,
acts.organization o
where ctp.accountorgid is null
and m.primarymemberplanid = ctp.primarymemberplanid
and SO.AHMSUPPLIERID = M.AHMSUPPLIERID
and mssr.SUPPLIERID = so.SUPPLIERORGID
AND iosr.SUPPLIERID = so.SUPPLIERORGID
AND io.INSURANCEORGID = iosr.INSURANCEORGID
and io.processingmodecd = 'P'
and so.usagemnemonic = 'P'
and o.odsaccountid = io.insuranceorgid
and o.accountflg = 'Y') b
set a.accountorgid = b.orgid
where a.careteampatientid = b.patientid
Here is the Merge I also tried
merge into careteampatient a
using (select CTP.CARETEAMPATIENTID patientid, O.ORGANIZATIONID orgid
from acts.careteampatient ctp,
ods.member m,
ods.supplierorganization so,
ods.MASTERSUPPLIERSUPPLIERRELATION mssr,
ods.INSURANCEORGSUPPLIERRELATION IOSR,
ods.INSURANCEORGANIZATION IO,
acts.organization o
where ctp.accountorgid is null
and m.primarymemberplanid = ctp.primarymemberplanid
and SO.AHMSUPPLIERID = M.AHMSUPPLIERID
and mssr.SUPPLIERID = so.SUPPLIERORGID
AND iosr.SUPPLIERID = so.SUPPLIERORGID
AND io.INSURANCEORGID = iosr.INSURANCEORGID
and io.processingmodecd = 'P'
and so.usagemnemonic = 'P'
and o.odsaccountid = io.insuranceorgid
and o.accountflg = 'Y') b
on a.careteampatientid = b.patientid
when matched then
update
set a.accountorgid = b.orgid
This isnt working and Im fresh out of ideas, any help would be awesome. Thanks
Figured it out, I was missing brackets on my ON. It should be on (a.careteampatientid = b.patientid) also my select should read select distinct

Resources