Data exists within database but doesnt show up on webi report - business-intelligence

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.

Related

Get default value (and context column) when group by returns no records in Oracle

I have a query that I need for it to return a record even when there are no records. In the case where there are records, I simply want those records returned. On the other hand, when there are no records, I need it to still return a record but with the value for the "context" column (the GROUP BY column) equal to the value of the GROUP BY column that did not meet the criteria and a default value for aggregate function/column (e.g., 0). I tried a subquery:
SELECT
(
SELECT
CONTEXT,
SUM(VAL)
FROM
A_TABLE
WHERE
COL = 'absent'
GROUP BY
CONTEXT
)
FROM
DUAL;
but anything greater than one column in the subquery SELECT clause fails w/ a "too many values" message.
I also tried a UNION (with a little more context to more faithfully represent my situation):
SELECT
*
FROM
(
SELECT
CONTEXT,
SUM(VAL)
FROM
A_TABLE
WHERE
COL = 'absent'
GROUP BY
CONTEXT
UNION
SELECT
CONTEXT,
0
FROM
B_TABLE
)
AB_TABLE
INNER JOIN C_TABLE C -- just a table that I need to join to
ON
C.ID = AB_TABLE.C_ID
WHERE
C.ID = 10
AND ROWNUM = 1 -- excludes 2nd UNION subquery result when 1st returns record;
This one does work but I don't know why since the 2nd UNION subquery does not seem to be expressly connected w/ the first (I need the 2nd CONTEXT value to be the same as the 1st for the case where the 1st returns no records). The problem is that the real query does not return any records when I try to implement a similar strategy. I would like to see if there's a better way to approach this problem and perhaps get it to work for the real query (not included as it is too large and somewhat sensitive).
I am not sure I understand the question, but let's try.
I believe what you are saying is this. You have a table called A_TABLE, with columns CONTEXT, VAL, COL (and perhaps others as well).
You want to group by CONTEXT, and get the sum of VAL but only for those rows where COL = 'absent'. Otherwise you want to return a default value (let's say 0).
This can be done with conditional aggregation. The condition is in a CASE expression within the SUM, not in a WHERE clause (as you saw already, if you filter by COL='absent', in a WHERE clause, the query - past the WHERE clause - has no knowledge of the CONTEXT values that don't appear in any rows with COL = 'absent').
If the "default value" was NULL, you could do it like this:
select context, sum(case when col = 'absent' then value end) as val
from a_table
group by context
;
If the default value is anything other than NULL, the temptation may be to use NVL() around the sum. However, if VAL may be NULL, then it is possible that SUM(VAL) is NULL even when there are rows with COL = 'absent'. To address that possibility, you must leave the sum as NULL in those cases, and instead set the value to 0 (or whatever other "default value") only when there are NO rows with COL = 'absent'. Here is one way to do that. Still a standard "conditional" aggregate query:
select context,
case when count(case when col = 'absent' then 1 end) > 0
then sum(case when col = 'absent' then value end)
else 0 -- or whatever "default value" you must assign here
end as val
from a_table
group by context
;
Here's another way you could handle it that avoids the two additional tables (B_TABLE and C_TABLE).
SELECT context
, MAX(val)
FROM (
SELECT context
, SUM(val) as val
FROM a_table
WHERE col = 'absent'
GROUP BY context
UNION
SELECT context
, 0 as val
FROM a_table
) t
GROUP BY context
This assumes the default value you want to return is 0 and that any value in A_TABLE.VAL will be a positive integer.
http://sqlfiddle.com/#!4/c6ca0/20
SELECT b.context
, sum(a.val)
FROM b_table b
LEFT OUTER JOIN a_table a
ON a.context = b.context
AND a.col = 'absent'
GROUP BY b.context

ORDER BY CASE WHEN: ORDER BY items must appear in the select list

I have this example code of something I'm trying to run. Only he table names and column names were changed. What I want to do is have a result set of states and have 'NULL' be the first value and the rest of the results appear below 'NULL' in ascending order and I can't for the life of me make it work. I get the error at the bottom. This may be a very "noobish" question, but can anyone help? Much appreciated everyone!
SELECT DISTINCT
State
FROM TABLE1 (NOLOCK)
WHERE COLUMN1 NOT LIKE '%THAT%'
AND COLUMN1 NOT LIKE '%THIS%'
UNION
SELECT 'NULL'
ORDER BY ( CASE WHEN State = 'NULL' THEN 0
ELSE 1
END );
Error Message:
ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.
You need to select that column... even if you don't use it later, the order by requires it to be present in the select.
SELECT DISTINCT
State,
CASE WHEN State = 'NULL' THEN 0
ELSE 1 END orderId
FROM TABLE1 (NOLOCK)
WHERE COLUMN1 NOT LIKE '%THAT%'
AND COLUMN1 NOT LIKE '%THIS%'
ORDER BY ( CASE WHEN State = 'NULL' THEN 0
ELSE 1
END );

How can we do update,delete,insert in single merge

I have a source and target table. I was doing a simple merge where I was able to do insert and update operation simultaneously. I just added columns like insert date and update date to know when the record is inserted and updated. Then, I also added a flag columns which tells us that if the record is ever updated if yes then 'Y' else 'N'. I was successful till this but when I tried to use delete it doesn't allow me.I have seen many solutions in sql server but mine is oracle. I was doing a soft delete in which the record is not deleted from the target but just the flag column is updated to 'Y' if some record is deleted from the source table. But I was not able to do that. I used case also but of no use.To be clear I just want to update flag when there is delete in source. Can anybody tell me how can I do that with single merge statement? I have simple table student having attribute like name,address mobile no , state. I have made my query like this-
merge into target t using (SELECT CASE WHEN S.STUDENT_ID IS NULL THEN T.STUDENT_ID ELSE S.STUDENT_ID END
AS STUDENT_ID,S.NAME,S.ADRESS,S.STATE,S.MOBILE FROM STUDENTS S full JOIN TARGET T ON S.STUDENT_ID=T.STUDENT_ID) s
on (s.student_id = t.student_id)
when not matched then insert (student_id,name,address,state,mobile,insert_date,update_date)
values(s.student_id,s.name,s.adress,s.state,s.mobile,sysdate,sysdate)
when matched then update set t.name=s.name,t.address=s.adress, t.state=s.state,t.mobile=s.mobile,update_date=sysdate
where s.name!=t.name or s.adress!=t.address or s.state!=t.state or s.mobile!=t.mobile;
this time I used case with every attribute and I am able to solve it but every time I run it it shows the no of extra rows present in target merged like - 4 rows are merged I dont want that can anyone help my query is-
MERGE INTO TARGET T USING (SELECT CASE WHEN S.STUDENT_ID IS NULL THEN T.STUDENT_ID ELSE S.STUDENT_ID END
AS STUDENT_ID,S.NAME,S.ADRESS,S.STATE,S.MOBILE FROM STUDENTS S FULL JOIN TARGET T ON S.STUDENT_ID=T.STUDENT_ID) S
ON (S.STUDENT_ID=T.STUDENT_ID)
WHEN NOT MATCHED THEN INSERT
(STUDENT_ID,NAME,ADDRESS,STATE,MOBILE,INSERT_DATE,UPDATE_DATE,FLAG )
VALUES(S.STUDENT_ID,S.NAME,S.ADRESS,S.STATE,S.MOBILE,SYSDATE,SYSDATE, 'N')
WHEN MATCHED THEN UPDATE SET T.FLAG='Y' , t.name = case when s.name is null then T.NAME else S.name end ,
T.ADDRESS=case when s.ADRESS is null then T.ADDRESS else S.ADRESS end ,
t.STATE=case when s.STATE is null then T.STATE else S.STATE end ,
T.MOBILE=CASE WHEN S.MOBILE IS NULL THEN T.MOBILE ELSE S.MOBILE END
WHERE S.NAME!=T.NAME OR S.ADRESS!=T.ADDRESS OR S.STATE!=T.STATE OR S.MOBILE!=T.MOBILE or t.STUDENT_ID in
(SELECT TARGET.STUDENT_ID FROM
STUDENTS FULL JOIN TARGET ON STUDENTS.STUDENT_ID=TARGET.STUDENT_ID WHERE STUDENTS.STUDENT_ID IS NULL);
Answer to my question-
MERGE INTO TARGET T USING
(SELECT CASE
WHEN S.STUDENT_ID IS NULL THEN T.STUDENT_ID
ELSE S.STUDENT_ID
END AS STUDENT_ID,
S.NAME,
S.ADRESS,
S.STATE,
S.MOBILE
FROM STUDENTS S
FULL JOIN TARGET T ON S.STUDENT_ID=T.STUDENT_ID) S ON (S.STUDENT_ID=T.STUDENT_ID) WHEN NOT MATCHED THEN
INSERT (STUDENT_ID,
NAME,
ADDRESS,
STATE,
MOBILE,
INSERT_DATE,
UPDATE_DATE,
FLAG)
VALUES(S.STUDENT_ID,
S.NAME,
S.ADRESS,
S.STATE,
S.MOBILE,
SYSDATE,
SYSDATE,
'N') WHEN MATCHED THEN
UPDATE
SET T.FLAG= CASE
WHEN ( T.STUDENT_ID IN (SELECT TARGET.STUDENT_ID
FROM STUDENTS
FULL JOIN TARGET ON STUDENTS.STUDENT_ID=TARGET.STUDENT_ID
WHERE STUDENTS.STUDENT_ID IS NULL)) THEN 'Y'
ELSE 'N'
END,
T.NAME = CASE
WHEN S.NAME IS NULL THEN T.NAME
ELSE S.NAME
END,
T.ADDRESS=CASE
WHEN S.ADRESS IS NULL THEN T.ADDRESS
ELSE S.ADRESS
END,
T.STATE=CASE
WHEN S.STATE IS NULL THEN T.STATE
ELSE S.STATE
END,
T.MOBILE=CASE
WHEN S.MOBILE IS NULL THEN T.MOBILE
ELSE S.MOBILE
END,
T.UPDATE_DATE=SYSDATE ,
T.INSERT_DATE=SYSDATE
WHERE S.NAME!=T.NAME
OR S.ADRESS!=T.ADDRESS
OR S.STATE!=T.STATE
OR S.MOBILE!=T.MOBILE
OR T.STUDENT_ID IN
(SELECT TARGET.STUDENT_ID
FROM STUDENTS
FULL JOIN TARGET ON STUDENTS.STUDENT_ID=TARGET.STUDENT_ID
WHERE STUDENTS.STUDENT_ID IS NULL)
AND FLAG!='Y';

If statement in 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)

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