If statement in Oracle - oracle

I need to create a query, that if a certain field is blank or null. I need to do a select statement to another table and retrieve the blank field . Could you please advise on a way to accomplish this. Below is the query. The field in question is BEAT.
SELECT COALESCE(ADDRESSES.BEAT,Incident_addresses.beat)
, COALESCE (ADDRESSES.SUB_BEAT,Incident_addresses.sub_beat)
, ADDRESSES.STREET_NAME
, ADDRESSES.STREET_NUMBER
, ADDRESSES.SUB_NUMBER
, WARRANT_PEOPLE_VW.LNAME
, WARRANT_PEOPLE_VW.FNAME
, WARRANT_PEOPLE_VW.DOB
, WARRANT_PEOPLE_VW.RACE_RACE_CODE
, WARRANT_PEOPLE_VW.SEX_SEX_CODE
, WARRANT_PEOPLE_VW.CASE_NUMBER
, E_WARRANTS.DATE_ISSUED
, E_WARRANTS.TELETYPE_NUMBER
, E_WARRANTS.ORDINANCE_VIOLATION
FROM EJSDBA.ADDRESSES
, POL_LEEAL.E_WARRANTS
, POL_LEEAL.WARRANT_PEOPLE_VW,incident_people,Incident_addresses
WHERE ADDRESSES.ADDRESS_ID =E_WARRANTS.ADDR_ADDRESS_ID
AND E_WARRANTS.WARRANT_ID = WARRANT_PEOPLE_VW.WARRANT_ID
AND WARRANT_PEOPLE_VW.NME_TYP_NAME_TYPE_CODE = 'P'
AND WARRANT_PEOPLE_VW.AGNCY_CD_AGENCY_CODE = 'MCPD'
AND WARRANT_PEOPLE_VW.WSC_CODE='A'
AND EJSDBA.ADDRESSES.ADDRESS_ID= Incident_addresses.ADDRESS_ID
and incident_people.inc_incident_id=Incident_addresses.incident_id
ORDER BY ADDRESSES.BEAT
, ADDRESSES.SUB_BEAT
, ADDRESSES.STREET_NAME
, ADDRESSES.STREET_NUMBER
;

You can embed a correlated subquery into a case expression, but you MUST reference the table inside the subquery to some values of the outer query so that the correct value can be located.
SELECT
COALESCE(ADDRESSES.BEAT, Incident_addresses.beat)
, CASE
WHEN Addresses.BEAT IS NULL THEN (
SELECT
Beat
FROM incidents inner_ref
WHERE ???outer??.incident_id = inner_ref.id
AND rownum = 1)
END AS x
, COALESCE(ADDRESSES.SUB_BEAT, Incident_addresses.sub_beat)
...
and that subquery MUST only return a single value (hence I have used "and rownum = 1".
(In Oracle 12 you could use FETCH FIRST 1 ROW ONLY)

Related

Data exists within database but doesnt show up on webi report

Hi I am building a v simple report as below; when I include the customer reference number and filter for another column (resolved time) being NULL then the number of incidents are reduced. (I know they exist within the database with the same filters)
(only INC1988464 is showing when more incidents should be showing)
Is there a way to test issues such as this in webi? Or a way to resolve this? Thanks in advance
Here is the sql used to make the report:
SELECT
'INC'||TRIM(to_char(ead_incident.incident,'0000000')),
ead_incident_credit.circuit_ref,
ead_incident_credit.customer_ref,
ead_incident_credit.data_rate,
ead_incident_credit.connection_type,
ead_incident_credit.completed_date,
ead_incident_credit.wholesaler_name,
ead_incident_credit.opened_datetime,
ead_incident_credit.resolved_datetime,
ead_incident_credit.resolution_details,
ead_incident_credit.resolution_duration_seconds,
ead_incident_credit.access_circuit_core_network,
ead_incident_credit.charge_amount,
ead_incident_credit.or_cost,
ead_incident_credit.sky_cost,
ead_incident_credit.service_credit_applicable,
ead_incident_credit.service_credit_due,
CASE WHEN COALESCE(COALESCE(ead_incident.actual_end_datetime,ead_incident.impact_end_datetime),ead_incident.resolved_datetime) IS NOT NULL THEN CASE WHEN ead_incident.impact_type = 'Full Outage' AND COALESCE(ead_incident.cause_classification,'') <> 'Resolved - No fault found' AND COALESCE(odwh_data.ead_within_sla('TTR',ead_incident.opened_datetime, COALESCE(COALESCE(ead_incident.actual_end_datetime,ead_incident.impact_end_datetime),ead_incident.resolved_datetime)),'f') = 'f' THEN 'Y' ELSE 'N' END ELSE NULL END,
CASE WHEN ead_incident.correlation = 't' THEN 'Y' ELSE 'N' END,
odwh_system.kpi_nextweek_startdate(),
odwh_system.kpi_currentweek_startdate()
FROM
odwh_data.ead_incident ead_incident INNER JOIN odwh_data.ead_incident_credit ead_incident_credit ON (ead_incident_credit.incident=ead_incident.incident)
WHERE
(
CASE WHEN COALESCE(COALESCE(ead_incident.actual_end_datetime,ead_incident.impact_end_datetime),ead_incident.resolved_datetime) IS NOT NULL THEN CASE WHEN ead_incident.impact_type = 'Full Outage' AND COALESCE(ead_incident.cause_classification,'') <> 'Resolved - No fault found' AND COALESCE(odwh_data.ead_within_sla('TTR',ead_incident.opened_datetime, COALESCE(COALESCE(ead_incident.actual_end_datetime,ead_incident.impact_end_datetime),ead_incident.resolved_datetime)),'f') = 'f' THEN 'Y' ELSE 'N' END ELSE NULL END = 'Y'
AND
(
CASE WHEN ead_incident.deleted = 't' THEN 'Y' ELSE 'N' END = 'N'
AND
(
CASE WHEN ead_incident.wholesaler = 't' THEN 'Y' WHEN ead_incident.wholesaler = 'f' THEN 'N' END = 'Y'
OR
CASE WHEN ead_incident.wholesaler = 't' THEN 'Y' WHEN ead_incident.wholesaler = 'f' THEN 'N' END Is Null
)
)
)
If you can create a free-hand SQL query which exhibits your issue like this...
SELECT
'INC1988464' AS [Incident Number]
, 'SKY-COLT-003' AS [Customer Reference]
, CONVERT (DATETIME, '2022-05-08 11:17:49') AS [Resovled Time]
UNION
SELECT
'INC1988464' AS [Incident Number]
, 'SKY-COLT-005' AS [Custome rReference]
, CONVERT (DATETIME, '2022-05-09 9:03:21') AS [Resovled Time]
UNION
SELECT
'INC1988464' AS [Incident Number]
, 'SKY-COLT-007' AS [Customer Reference]
, NULL AS [Resovled Time]
UNION
SELECT
'INC1988464' AS [Incident Number]
, 'SKY-COLT-009' AS [Customer Reference]
, CONVERT (DATETIME, '2022-05-09 10:17:02') AS [Resovled Time];
Then we can take that query, put it into a WebI report, replicate the issue, and possibly resolve it.
First and easiest thing to check. Do you have any filters already on your report page or are you starting on a brand new page? The filters may be against the page or the individual report block.
If you have, then remove them and see if the issue is resolved. Note that there may also be a ranking in there, returning the top 1 incidents.
If not, then there will be something in the report logic preventing the records from being returned. Comment out the where clauses and make sure to add the columns from the where clauses into your result set. From here you have two things to check:
1/ All rows are being returned
2/ You can see which rows are going to be filtered by your where clause based on what you see on your report based on your logic. You have nested case expressions which may not be doing what you expected for example.

Error: ORA-00905: missing keyword when joining table to a select query

I am trying to link a table to a select query and I get the Error: ORA-00905: missing keyword. This is the oracle sql I have written.
When I run things separately data pulls. Its when I try to join them I get the error. I tried adding in the Group BY and Order by per some help information I found on the Internet but still get the same error.
SELECT
AS_MASTER_NF.CONTRACT_NO
, AS_HISTORY_NF.AH_CONTRACT_NBR
, AS_MASTER_NF.ID
, AS_MASTER_NF.A_INVENT_DATE
, AS_MASTER_NF.A_DISP_DATE
FROM INFL_IDS.AS_MASTER_NF
LEFT JOIN (SELECT
NVL(SUBSTR(ALTERNATE_ID, 0, INSTR(ALTERNATE_ID, '*')-1), ALTERNATE_ID) AS ASSET
, AS_HISTORY_NF.AH_CONTRACT_NBR
FROM INFL_IDS.AS_HISTORY_NF
WHERE LENGTH (AH_CONTRACT_NBR)> 3) AS ASHISTORY
ON INFL_IDS.AS_MASTER_NF.ID = INFL_IDS.ASHISTORY.ASSET
WHERE AS_MASTER_NF.A_INVENT_DATE IS NOT NULL
GROUP BY
AS_MASTER_NF.CONTRACT_NO
, AS_HISTORY_NF.AH_CONTRACT_NBR
, AS_MASTER_NF.ID
, AS_MASTER_NF.A_INVENT_DATE
, AS_MASTER_NF.A_DISP_DATE
ORDER BY
AS_MASTER_NF.CONTRACT_NO
, AS_HISTORY_NF.AH_CONTRACT_NBR
, AS_MASTER_NF.ID
, AS_MASTER_NF.A_INVENT_DATE
, AS_MASTER_NF.A_DISP_DATE
FETCH FIRST 20 ROWS ONLY
Change:
ON INFL_IDS.AS_MASTER_NF.ID = INFL_IDS.ASHISTORY.ASSET
to:
ON INFL_IDS.AS_MASTER_NF.ID = ASHISTORY.ASSET
Because, ASHISTORY is not a table or view under INFL_IDS schema but just a sub-query which's defined in this sql.

Mutiple Where subqueries in Hive doesn't work

I have a Query like below:
SELECT T.MTH_END_DT, T.SRC_SYS_CD, T.BTCH_ID
FROM PROD_RCRR.BAL_CNTRL_LOG T
WHERE T.SRC_SYS_CD='SL'
AND T.MTH_END_DT in (SELECT(MAX(MTH_END_DT)) FROM PROD_RCRR.BAL_CNTRL_LOG)
AND T.BTCH_ID in (SELECT(MAX(BTCH_ID )) FROM PROD_RCRR.BAL_CNTRL_LOG)
A error message shows Hive only can support one "in" clause. Anyone can give me a solution?
You can replace the whole thing with Join ON clause
SELECT
T.MTH_END_DT
, T.SRC_SYS_CD
, T.BTCH_ID
FROM PROD_RCRR.BAL_CNTRL_LOG T
JOIN ( SELECT
MAX(MTH_END_DT) ENDT
, MAX(BTCH_ID ) BTCH
FROM PROD_RCRR.BAL_CNTRL_LOG ) X
ON T.SRC_SYS_CD='SL'
AND T.MTH_END_DT = X.ENDT
AND T.BTCH_ID = X.BTCH

Using group by subquery to deduplicate record set

I would like to know how I can return the activity Id to a parent query using a query like the following to de-duplicate the parent record set. The issue here is that using this group by or distinct I cannot find a way.
I will use all of the group by fields to determine a unique record. But, I need to use only the record with the select min(status.effective_date)
The query returns the correct date values, but I cannot link it back to the parents activity records with just that date value.
select min(status.effective_date)
from accounts
, address
, activity
, status
where accounts.par_row_id = activity.account_id
and address.row_id = activity.address_id
and status.par_row_id = activity.status_id
and account.name = 'xyz'
group by account.name, address.addr, address.ADDR_LINE_2, address.ADDR_LINE_3
, address.ADDR_LINE_4, address.CITY, address.COUNTRY, address.X_STATE
, address.ZIPCODE
You should try this query:
select min(activity.row_id) keep(dense_rank first order by status.effective_date) as activity_id
from accounts
, address
, activity
, status
where accounts.par_row_id = activity.account_id
and address.row_id = activity.address_id
and status.par_row_id = activity.status_id
and accounts.name = 'xyz'
group by accounts.name, address.addr, address.ADDR_LINE_2, address.ADDR_LINE_3
, address.addr_line_4, address.city, address.country, address.x_state
, address.ZIPCODE;

ORA-01791 Pl-Sql error

hi guy i have a query that give me the followin error:
ORA-01791: not a SELECTed expression
this is the select expresison , please can you tell me why ?
declare
freqLettura varchar2(64);
billingcy varchar2(64);
begin
freqLettura := null;
billingcy := null;
for rec in ( select distinct(fn_get_facilityid(z.uidfacility) ) as a, 1 as b
from facilityhistory z,
locality l ,
plant p ,
ztmp_sam_tb_sdv zsdv ,
ztmp_sam_tb_plantcode zplant ,
sam_tb_ca_pdr sam,
meterhistory mh,
meter m ,
meterclass mc
where
Z.UIDLOCALITY = L.UIDLOCALITY and
p.UIDPLANT = L.UIDPLANT and
z.uidaccount = zsdv.uidaccount and
p.plantcode = zplant.plantcode and
sam.uidfacility = z.uidfacility and
z.stoptime is null and
sam.status = 'U' and
mh.uidfacility = z.uidfacility and
mh.uidmeter = m.uidmeter and
m.uidmeterclass = mc.uidmeterclass and
(billingcy is null or p.UIDBILLINGCYCLE = billingcy )
AND
(
(
(freqLettura = 'G') AND ( mh.corrmeterid is not null and mh.stoptime is null and mc.maxflowmeter >= SAM_FN_GET_PARAMETER_FLOAT('MAXFLOWMET_DETT_GIORN'))
)
OR
(
nvl(freqLettura,'nullo') <> 'G' AND (freqLettura is null or sam.readfrequency = freqLettura)
)
) and ROWNUM = 1 order by sam.stoptime, sam.uidsamtbpdr desc ) loop
begin
insert into ztmp_sam_tb_elab_pdr (facilityid, uidbatchrequest) VALUES (rec.a, rec.b);
exception
when dup_val_on_index then
null;
end;
end loop;
end;
Whenever you get an Oracle error message you don't understand, the first thing to do is look up the meaning. One way is simply to Google it. In this case the full description found in
Oracle9i Database Error Messages is:
ORA-01791 not a SELECTed expression
Cause: There is an incorrect ORDER
BY item. The query is a SELECT DISTINCT query with an ORDER BY clause.
In this context, all ORDER BY items must be constants, SELECT list
expressions, or expressions whose operands are constants or SELECT
list expressions.
Action: Remove the inappropriate ORDER BY item from the SELECT list
and retry the statement.
(Oddly this error message isn't documented in the 10G or 11G manuals, despite still being raised!)
This matches the statement you have written, which is a SELECT DISTINCT query where you are trying to order the results by a column that you did not select.
If you think about it, what you are asking for doesn't make sense: by selecting DISTINCT values that do not include sam.stoptime (for example) you may be consolidating many rows with different values for sam.stoptime, so which one would govern the ordering?
Also, as Noel's answer points out, there is no reason to have an ORDER BY clause in this code anyway, so the solution is simply to remove it.
If you are using DISTINCT in your SELECT query, then your ORDER BY clause should contain only those columns that your selecting. In this case sam.stoptime, sam.uidsamtbpdr are not there in SELECT statement. You can remove the ORDER BY clause, as it is not doing anything useful in your example.

Resources