Oracle sqlplus query - oracle

I am getting "Invalid Number"
Can anyone please guide me what I am doing wrong here ???
select TO_CHAR(a.START_TIME,'YY-MON-DD HH24') as START_TIME,
count(*) AS NbOperations,
SUM(CASE WHEN OPERATION_RESULT=0 THEN 1 ELSE 0 END) AS Success,
SUM(CASE WHEN OPERATION_RESULT=200 THEN 1 ELSE 0 END) AS Cancel,
SUM(CASE WHEN OPERATION_RESULT=203 THEN 1 ELSE 0 END) AS AppletInternalError,
SUM(CASE WHEN OPERATION_RESULT=406 THEN 1 ELSE 0 END) AS TimeoutWaitForCard,
SUM(CASE WHEN OPERATION_RESULT=413 THEN 1 ELSE 0 END) AS BillingError,
SUM(CASE WHEN OPERATION_RESULT=1000 THEN 1 ELSE 0 END) AS ABANDONNED,
SUM(CASE WHEN OPERATION_RESULT=1004 THEN 1 ELSE 0 END) AS ABANDON_FOR_NEW_OPERATION,
SUM(CASE WHEN OPERATION_RESULT NOT IN (0,200,203,406,413,1000,1004) THEN 1 ELSE 0 END) AS NbOthers
from MyTable1 a,
MyTable2 b
where OPERATION_TYPE in (2,3,4,5)
and a.OPERATION_TYPE=b.OPERATION_ID
and a.START_TIME >= to_timestamp('&1', 'YYYY-MM')
and a.START_TIME < to_timestamp('&2', 'YYYY-MM') + interval '1' month
group by TO_CHAR(a.START_TIME,'YY-MON-DD HH24')
order by TO_CHAR(START_TIME,'YY-MON-DD HH24');

check the format of your inputs &1 and &2 maybe it doesn't match with your date mask: 'YYYY-MM'. Another possible reason is the contents of the table: OPERATION_TYPE and OPERATION_RESULT are both number datatypes? Possible there are storing things different from a number.

I am able to solve the issue using below SQL code:
select TO_CHAR(a.START_TIME,'YY-MON-DD HH24') as Hourly, count(*) AS NbOperations,
SUM(CASE WHEN OPERATION_RESULT=0 THEN 1 ELSE 0 END) AS Success,
SUM(CASE WHEN OPERATION_RESULT=200 THEN 1 ELSE 0 END) AS Cancel,
SUM(CASE WHEN OPERATION_RESULT=203 THEN 1 ELSE 0 END) AS AppletInternalError,
SUM(CASE WHEN OPERATION_RESULT=406 THEN 1 ELSE 0 END) AS TimeoutWaitForCard,
SUM(CASE WHEN OPERATION_RESULT=413 THEN 1 ELSE 0 END) AS BillingError,
SUM(CASE WHEN OPERATION_RESULT=1000 THEN 1 ELSE 0 END) AS ABANDONNED,
SUM(CASE WHEN OPERATION_RESULT=1004 THEN 1 ELSE 0 END) AS ABANDON_FOR_NEW_OPERATION,
SUM(CASE WHEN OPERATION_RESULT NOT IN (0,200,203,406,413,1000,1004) THEN 1 ELSE 0 END) AS NbOthers
from gsg3_invocation_history a, GSG3_OPERATION_TYPE b where START_TIME >= to_timestamp('&1', 'YYYY-MM')
and START_TIME < to_timestamp('&2', 'YYYY-MM') + interval '1' month and OPERATION_TYPE in (2,3,4,5) and a.OPERATION_TYPE=b.OPERATION_ID
group by TO_CHAR(a.START_TIME,'YY-MON-DD HH24') order by TO_CHAR(START_TIME,'YY-MON-DD HH24');

Related

Count and Sum inside a select with multiple sums and counts

I'm using sub-query factoring and I have a query that returns invoice lines, and in the end I have this final sub-query:
I've already tried Partition but without success
SELECT
COUNT(CASE WHEN PC <> 0 THEN 1 END) AS A_LINECOUNT,
SUM(CASE WHEN PC > 0 THEN NR ELSE 0 END) AS B_PRODUCTCOUNT,
COUNT(CASE WHEN ALLOW_PAY = 1 THEN 1 END) AS C_INVOICECOUNT, --- ERROR
SUM(CASE WHEN ALLOW_PAY = 1 THEN MISSING_VALUE ELSE 0 END) AS D_INVOICETOTAL, --- ERROR
COUNT(CASE WHEN IS_NON_LIQUIDABLE_PRODUCT = 1 THEN 1 END) AS E_CONDITIONCOUNT,
COUNT(CASE WHEN IS_LIQUIDABLE_PRODUCT = 1 THEN 1 END) AS F_CONDITIONCOUNT
FROM MAIN_Q
The calculation of C_INVOICECOUNT and D_INVOICETOTAL is not correct because their values are repeated within each line of the invoice. Please consider that um MAIN_Q i also have a document_id where i can group by.
thanks
Maybe I understood correctly, maybe not, but this is too long for comment. If yes, C_INVOICECOUNT can be count as:
count(distinct case when allow_pay = 1 then document_id end)
But the problem is with D_INVOICETOTAL. You have repeated values for each invoice here and details which do not repeat. If so, add row numbering to your query:
select main_q.*, row_number() over (partition by document_id) rn from main_q
and then in problematic places use rn = 1:
select ...
count(case when rn = 1 and allow_pay = 1 then 1 end),
sum(case when rn = 1 and allow_pay = 1 then missing_value else 0 end)
...
from (select main_q.*, row_number() over (partition by document_id) rn from main_q)
Only first rows for each invoice will be analysed. Of course you can add row_number in earlier step.

oracle SQL procedure updating table

I need to update the the following columns in a table. males,females,infants and children using SQL procedure.from this column paxtype which has f,i,m,c.which is females,infants,males and children repectively.but am not able error ORA-00907: missing right parenthesis
update xxxx a set (a.INFANTS,a.MALES,a.CHILDREN,a.FEMALES)=
(SELECT b.PAXTYPE, COUNT (b.PAXTYPE) FROM xxxx b
(case ( count (b.PAXTYPE))
when ( count (b.PAXTYPE))='M'then 'a.males'
when ( count (b.PAXTYPE))='F' then 'a.females'
when ( count (b.PAXTYPE))='I' then 'a.infants'
when ( count (b.PAXTYPE))='C' then 'a.children'
END)
WHERE a.date_key = TO_CHAR (b.FLIGHTDATE, 'RRRRMMDD')
AND a.FLTNUM_KEY = TRIM (SUBSTR (b.flightnumber, 3))
AND a.origin = b.frm
AND a.destination = b.too
--and a.date_key=20170801
--and fightnumber = '100'
AND TRIM (a.cancelled) IS NULL
-- and rownum = 1
GROUP BY b.PAXTYPE;
)
It looks like you want a total count of each type, right? I think this is what you want to do.
update xxxx a set (a.INFANTS,a.MALES,a.CHILDREN,a.FEMALES)=
(SELECT
sum(case when b.PAXTYPE = 'I' then 1 else 0 end) as infants_count,
sum(case when b.PAXTYPE = 'M' then 1 else 0 end) as males_count,
sum(case when b.PAXTYPE = 'C' then 1 else 0 end) as children_count,
sum(case when b.PAXTYPE = 'F' then 1 else 0 end) as females_count
FROM xxxx b
WHERE a.date_key = TO_CHAR (b.FLIGHTDATE, 'RRRRMMDD')
AND a.FLTNUM_KEY = TRIM (SUBSTR (b.flightnumber, 3))
AND a.origin = b.frm
AND a.destination = b.too
--and a.date_key=20170801
--and fightnumber = '100'
AND TRIM (a.cancelled) IS NULL
-- and rownum = 1
GROUP BY b.PAXTYPE);

Plsql Program using control statements

I have a table like "Emp_Info", For the below output i have used "two" SELECT Statements, I want work that using ONE SELECT STATEMENT.
select count(*) into cnt
from emp_info t
where upper(trim(t.email)) = upper(trim(field1value))
and t.company_id = companyId;
select
(case when cnt1 > 0 then 'YES' else 'NO' end) into val_acc
from (
select count(*) cnt1 from
emp_info t
where upper(trim(t.email)) = upper(trim(field1value))
and (t.account_expiry_dt is null or t.account_expiry_dt >= sysdate)
and t.company_id = companyId
);
if cnt = 0 then
raise_application_error(-20002, 'User does not exist');
elsif cnt > 1 then
raise_application_error(-20003, 'Duplicate records found');
elsif val_acc = 'NO' then
raise_application_error(-20004, 'Account has expired');
else
/* some logic */
end if;
end;
may be this help you
select count(*),
CASE WHEN
sum( CASE when t.account_expiry_dt is null or t.account_expiry_dt >= sysdate then 1 else 0 end) > 0 then 'YES'
else 'NO'
END
into cnt, val_acc
from emp_info t
where upper(trim(t.email)) = upper(trim(field1value))
and t.company_id = companyId;
simple demo:
with emp_info(login,username) as (select 'test', 'test' from dual union
select 'TEst', 'TEst' from dual )
select count(*) emp_count,
CASE when sum( CASE WHEN login like '%st%' then 1 else 0 end) > 0 then 'Y' else 'N' end emp_flag,
sum( CASE WHEN login like '%st%' then 1 else 0 end) emp_flag_count,
CASE when sum( CASE WHEN login like '%te%' then 1 else 0 end) > 0 then 'Y' else 'N' end emp_flag1,
sum( CASE WHEN login like '%te%' then 1 else 0 end) emp_flag_count1,
CASE when sum( CASE WHEN login like '%TE%' then 1 else 0 end) > 0 then 'Y' else 'N' end emp_flag2,
sum( CASE WHEN login like '%TE%' then 1 else 0 end) emp_flag_count2
from emp_info
answer if it helped you

Oracle query optimization help (involves TRUNC)

Here is the query.
closed_ts is a timestamp column. What I am trying to do is, find out how many were "closed" today, this month, this year and this week. Is there a better way to do this?
select table_id,
case
when trunc(closed_ts) = trunc(SYSDATE, 'DD') then 1
else 0
end as day_count,
case
when trunc(closed_ts) >= trunc(SYSDATE, 'MM') then 1
else 0
end as month_count,
case
when trunc(closed_ts) >= trunc(sysdate, 'YYYY') then 1
else 0
end as year_count,
case
when trunc(closed_ts) >= TRUNC(sysdate, 'IW') then 1
else 0
end as week_count
from myTable
Seems like you would want to aggregate the values with a sum() around each case statement and group by table_id, but since you asked about query optimization...
If there is an index on closed_ts you won't be able to use it because you are trunc'ing it. Truncating the closed_ts date is unnecessary since you are checking whether it is strictly larger than day, week, month, year, so this query will definitely result in a full table scan.
I'd likely rewrite it without the trunc whether there was an index on closed_ts or not:
select table_id
,sum(case when closed_ts >= trunc(sysdate) then 1 else 0 end) as day_count
,sum(case when closed_ts >= trunc(sysdate,'MM') then 1 else 0 end) as month_count
,sum(case when closed_ts >= trunc(sysdate,'YYYY') then 1 else 0 end) as year_count
,sum(case when closed_ts >= trunc(sysdate,'IW') then 1 else 0 end) as week_count
from myTable
group by table_id
btw: table_id sounds like a primary or surrogate key - are you sure you want it in the results?
Pop an index on (closed_ts,table_id), and apply a predicate on closed_ts as below ...
select
table_id,
sum(case when closed_ts >= trunc(SYSDATE,'DD' ) then 1 end) day_count,
sum(case when closed_ts >= trunc(SYSDATE,'MM' ) then 1 end) month_count,
sum(case when closed_ts >= trunc(SYSDATE,'YYYY') then 1 end) year_count,
sum(case when closed_ts >= trunc(SYSDATE,'IW' ) then 1 end) week_count,
from
myTable
where
closed_ts >= least(trunc(SYSDATE,'YYYY'),trunc(SYSDATE,'IW'))
group by
table_id
As user1842757 mentioned, lose the trunc on the closed_ts. You can also lose the ELSE 0 in the case statements

How do I sum records by week using pl/sql?

I have a query in Oracle for a report that looks like this:
SELECT TRUNC (created_dt) created_dt
, COUNT ( * ) AllClaims
, SUM(CASE WHEN filmth_cd in ('T', 'C') THEN 1 ELSE 0 END ) CT
, SUM(CASE WHEN filmth_cd = 'W' THEN 1 ELSE 0 END ) Web
, SUM(CASE WHEN filmth_cd = 'I' THEN 1 ELSE 0 END ) Icon
FROM claims c
WHERE c.clsts_cd NOT IN ('IN', 'WD')
AND TRUNC (created_dt) between
to_date('1/1/2006', 'dd/mm/yyyy') AND
to_date('1/1/2100', 'dd/mm/yyyy')
GROUP BY TRUNC (created_dt)
ORDER BY TRUNC (created_dt) DESC;
It returns data like this:
Create_Dt AllClaims CT Web Icon
1/26/2011 675 356 285 34
1/25/2011 740 322 379 39
...
What I need is a result set that sums all of the daily values into a weekly value. I am pretty new to PL/SQL and not sure where to begin.
Something like
SELECT TRUNC (created_dt, 'IW') created_dt
, COUNT ( * ) AllClaims
, SUM(CASE WHEN filmth_cd in ('T', 'C') THEN 1 ELSE 0 END ) CT
, SUM(CASE WHEN filmth_cd = 'W' THEN 1 ELSE 0 END ) Web
, SUM(CASE WHEN filmth_cd = 'I' THEN 1 ELSE 0 END ) Icon
FROM claims c
WHERE c.clsts_cd NOT IN ('IN', 'WD')
AND TRUNC (created_dt) between
to_date('1/1/2006', 'dd/mm/yyyy') AND
to_date('1/1/2100', 'dd/mm/yyyy')
GROUP BY TRUNC (created_dt, 'IW')
ORDER BY TRUNC (created_dt, 'IW') DESC;
will aggregate the data based on the first day of the ISO week.
Here is a simple example that I quite often use:
SELECT
TO_CHAR(created_dt,'WW'),
max(created_dt),
COUNT(*)
from MY_TABLE
group by
TO_CHAR(created_dt,'WW');
The to_char(created_dt,'WW') create the groups, and the max(created_dt) displays the last day of the week. Use min(created_dt) if you want to display the first day of the week.

Resources