count returns <<air>> when there's nothing to group by - oracle

Here's my SQL:
select
count(*)
from
sysdba.dw_cmap_arf_tmp
left join SYSDBA.TABLE1 rrc
on rrc.part_work_order = pwo
left join sysdba.TABLE2 R
on rrc.run_number = R.RUN_NUMBER
where
upper(run_type) like '%FEE%'
group by
pwo;
When there's nothing to group by, count returns --air--. It's null nor is it blank. I have modified the above SQL as the following to prove.
select
'>>' || count(*) || '<<' as blah
from
sysdba.dw_cmap_arf_tmp
left join SYSDBA.TABLE1 rrc
on rrc.part_work_order = pwo
left join sysdba.TABLE2 R
on rrc.run_number = R.RUN_NUMBER
where
upper(run_type) like '%ANNEAL%'
group by
pwo;
When I, however, write a statement above to perform an update, I got null. So tried coalesce, but got the same thing.
Does anyone know what I can do to replace --air-- with null or 0? Thanks!
P.S. I have did something research, but couldn't find anything...apologize in advance, if there's already a similar question out there.
Thanks!

This is what you currently have (based on Scott's EMP table): as there's no department 50, you get no rows selected (which is the air you're talking about, I presume).
SQL> with your_current_query as
2 (select count(*) cnt
3 from emp
4 where deptno = &deptno
5 group by job
6 )
7 select cnt
8 from your_current_query;
Enter value for deptno: 50
no rows selected
Just to show that it actually returns something if there are some data there:
SQL> /
Enter value for deptno: 30
CNT
----------
4
1
1
SQL>
OK; now, to do something with the situation where there are no rows selected, use union with a "dummy" row selected from the DUAL table:
SQL> with your_current_query as
2 (select count(*) cnt
3 from emp
4 where deptno = &deptno
5 group by job
6 )
7 select cnt
8 from your_current_query
9 -- add this: if YOUR_CURRENT_QUERY doesn't return anything, union it with
10 -- a select from dual
11 union all
12 select 0
13 from dual
14 where 0 = (select count(*) from your_current_query);
Enter value for deptno: 50
CNT
----------
0
SQL>
So: even though there are no employees in department 50, you got 0 as the result.
Again, to show what happens when there are some rows:
SQL> /
Enter value for deptno: 30
CNT
----------
4
1
1
SQL>
Finally, your query - rewritten - would look like this:
with your_current_query as
(select
count(*) cnt
from
sysdba.dw_cmap_arf_tmp
left join SYSDBA.TABLE1 rrc
on rrc.part_work_order = pwo
left join sysdba.TABLE2 R
on rrc.run_number = R.RUN_NUMBER
where
upper(run_type) like '%FEE%'
group by
pwo
)
select cnt from your_current_query
union all
select 0
from dual
where 0 = (select count(*) from your_Current_query);
See if it helps.

Related

Sum 2 columns from multiple rows with same ID

Currently, I have a table MY_TABLE like below:
ID ACCT_TYPE CREDIT_AMT DEBIT_AMT
-- --------- ---------- ---------
1 CDT_01 4 (null)
1 DBT_01 (null) 6
One ID can have multiple ACCT_TYPE like above, and each type has its own amount.
I want to just select the row which has ACCT_TYPE like 'CDT_%' but also the total_amount column which is the total of credit_amt and debit_amt column for the same ID.
My expected output like below:
ID ACCT_TYPE TOTAL_AMT
-- --------- ---------
1 CDT_01 10
I tried with this select statement below but it's no use, I think it's because of different ACCT_TYPE:
Select ID, ACCT_TYPE, SUM(NVL(CREDIT_AMT, 0) + NVL(DEBIT_AMT, 0)) TOTAL_AMT
FROM MY_TABLE WHERE ACCT_TYPE LIKE 'CDT_%' GROUP BY ID, ACCT_TYPE;
Here is the output of the select statement above:
ID ACCT_TYPE TOTAL_AMT
-- --------- ---------
1 CDT_01 4
I just begin to learn some query so I don't know is it really possible to get my expected output.
One way to do it is like below:
with inputs (ID, ACCT_TYPE, CREDIT_AMT, DEBIT_AMT) as
(
select 1, 'CDT_01', 4, null from dual union all
select 1, 'DBT_01', null, 6 from dual
),
prep as
(
select t.*, sum(nvl(credit_amt,0)) over (partition by id) + sum(nvl(debit_amt,0)) over (partition by id) as sum_per_id
from inputs t
)
select id, acct_type, sum_per_id
from prep
where acct_type like 'CDT_%';
Output:
A correlated subquery might be one option; sample data (thank you, #Ranagal) in lines #1 - 5; query that does the job begins at line #6.
SQL> with inputs (ID, ACCT_TYPE, CREDIT_AMT, DEBIT_AMT) as
2 (
3 select 1, 'CDT_01', 4, null from dual union all
4 select 1, 'DBT_01', null, 6 from dual
5 )
6 select a.id,
7 a.acct_type,
8 (select sum(nvl(b.credit_amt, 0)) +
9 sum(nvl(b.debit_amt , 0))
10 from inputs b
11 where b.id = a.id
12 ) total_amt
13 from inputs a
14 where acct_type like 'CDT%';
ID ACCT_T TOTAL_AMT
---------- ------ ----------
1 CDT_01 10
SQL>

How to return 0 incase of empty result in oracle select query?

I have a Query where there is number datatype column. In case of empty resultset O/P is blank. I want 0 in that case.
My query is
select nvl(infodata,0)
from table1
where group_id = 111
and appid in (select max(id)
from table1, table2
where status = 'A');
Now my inner query is returning null in that case. NVL seems to be not working on infodata column. Please help
Outcome of such a query is no rows selected.
For example (based on Scott's EMP and DEPT tables as I don't have yours, and you didn't post test case), this query returns nothing:
SQL> select e.deptno
2 from emp e
3 where e.ename = 'Littlefoot';
no rows selected
If it is used as a subquery, the result is equal to what you currently have:
SQL> select nvl(d.dname, 'x') result
2 from dept d
3 where d.deptno in (select e.deptno
4 from emp e
5 where e.ename = 'Littlefoot'
6 );
no rows selected
But, if you apply aggregation (see line #1), then you'd get what you wanted:
SQL> select nvl(max(d.dname), 'x') result
2 from dept d
3 where d.deptno in (select e.deptno
4 from emp e
5 where e.ename = 'Littlefoot'
6 );
RESULT
--------------
x
As I said: it works on my test case; might, or might not on yours. If you post sample data, we'd have something to work with.

Return Count Value back as 0 if emtpy or none

I am running the following query in our database and I am trying to get the count value back as 0 if none are present, but having problems doing so. Could someone help assist and explain how to accomplish this
select state, count(phone_number) from VOIP_PHONE_NUMBER_POOL
where status = 3
group by state
order by state asc;
For example, currently my query is returning something back like this
State Count
AZ 25
When I want it to return the empty values and look like this
State Count
AZ 25
CA 0
This mimics what you have now:
SQL> with voip_phone_number_pool (state, phone_number, status) as
2 (select 'AZ', 1234, 3 from dual union all
3 select 'AZ', 2232, 3 from dual union all
4 select 'AZ', 4444, 3 from dual union all
5 select 'AZ', 7756, 1 from dual union all
6 --
7 select 'CA', 9999, 1 from dual
8 )
9 select state,
10 count(phone_number)
11 from voip_phone_number_pool
12 where status = 3
13 group by state
14 order by state;
ST COUNT(PHONE_NUMBER)
-- -------------------
AZ 3
SQL>
There's no row for the CA state which has status = 3, so it isn't returned by that query.
What you could do, is to use outer self-join in such a manner:
SQL> with voip_phone_number_pool (state, phone_number, status) as
2 (select 'AZ', 1234, 3 from dual union all
3 select 'AZ', 2232, 3 from dual union all
4 select 'AZ', 4444, 3 from dual union all
5 select 'AZ', 7756, 1 from dual union all
6 --
7 select 'CA', 9999, 1 from dual
8 ),
9 states as
10 (select distinct state from voip_phone_number_pool)
11 select s.state,
12 count(v.phone_number)
13 from states s left join voip_phone_number_pool v on v.state = s.state
14 and v.status = 3
15 group by s.state
16 order by s.state;
ST COUNT(V.PHONE_NUMBER)
-- ---------------------
AZ 3
CA 0
SQL>
the states CTE selects all states
it is then used in outer join with voip_phone_number_pool
note that condition (status = 3) has to be moved into join (line #14); if you leave it in the where clause, you won't get desired result
If there are no rows in the fact table (VOIP_PHONE_NUMBER_POOL) with a particular state, you will not get such a result from only querying that table.
You will need to join your fact table to a dimension table (a table with all possible states) in order to get the desired counts.
For example, assuming you have such a dimension table and it is named "state_info":
SELECT s.state,
COUNT(v.phone_number) AS phone_number_count
FROM state_info s
LEFT OUTER JOIN voip_phone_number_pool v
ON s.state = v.state
GROUP BY s.state;
SELECT s.state,
COUNT(v.phone_number) AS phone_number_count
FROM CORP_ACCT s
LEFT OUTER JOIN voip_phone_number_pool v
ON s.state = v.state
GROUP BY s.state;
worked for me

Check if the result of Count equals zero Oracle

I have a need to perform MINUS operation between two similar tables, count the resulting rows and check if the count is equal to zero all in a SQL statement.
I have done the following but i am not sure about how to compare it to zero.
select count(*) from(select * from table1 MINUS SELECT * FROM table2)
Something like this?
SQL> with t_minus as
2 (select deptno from emp
3 minus
4 select deptno from dept
5 )
6 select case when count(*) = 0 then 'it is zero'
7 else 'it is not zero'
8 end result
9 from t_minus;
RESULT
--------------
it is zero
SQL> with t_minus as
2 (select empno from emp
3 minus
4 select deptno from dept
5 )
6 select case when count(*) = 0 then 'it is zero'
7 else 'it is not zero'
8 end result
9 from t_minus;
RESULT
--------------
it is not zero
SQL>

scalar subquery has an aggregate operation

My oracle version is 10.2.
It's very strange when a scalar subquery has an aggregate operation.
my table named t_test looked like this;
t_id t_name
1 1
2 1
3 2
4 2
5 3
6 3
query string looked like this;
select t1.t_id,
(select count(t_name)
from (select t2.t_name
from t_test t2
where t2.t_id=t1.t_id
group by t2.t_name)) a
from t_test t1
this query's result is,
t_id a
1 3
2 3
3 3
4 3
5 3
6 3
which is very weird,
take t1.t_id=1 for example,
select count(t_name)
from (select t2.t_name
from t_test t2
where t2.t_id=1
group by t2.t_name)
the result is 1,
somehow,the 'where' operator doesn't work,the result is exactly the same as I put my query like this:
select t1.t_id,
(select count(t_name)
from (select t2.t_name
from t_test t2
group by t2.t_name)) a
from t_test t1
why?
Can you post a cut-and-paste from SQL*Plus showing exactly what query you're running? The query you posted does not appear to be valid-- the alias t1 is not going to be valid in the subquery where you're referencing it. That makes me suspect that you're simplifying the problem to post here but you've accidentally left something important out.
SQL> ed
Wrote file afiedt.buf
1 with x as (
2 select 1 id, 1 name from dual union all
3 select 2,1 from dual union all
4 select 3,2 from dual union all
5 select 4,2 from dual union all
6 select 5,3 from dual union all
7 select 6,3 from dual
8 )
9 select t1.id
10 ,(select count(b.name)
11 from (select t2.name
12 from x t2
13 where t2.id = t1.id
14 group by t2.name) b) a
15* from x t1
SQL> /
where t2.id = t1.id
*
ERROR at line 13:
ORA-00904: "T1"."ID": invalid identifier
Presumably, it would be much more natural to write the query like this (assuming you really want to use a scalar subquery) where t1 is going to be a valid alias in the scalar subquery.
SQL> ed
Wrote file afiedt.buf
1 with x as (
2 select 1 id, 1 name from dual union all
3 select 2,1 from dual union all
4 select 3,2 from dual union all
5 select 4,2 from dual union all
6 select 5,3 from dual union all
7 select 6,3 from dual
8 )
9 select t1.id
10 ,(select count(t2.name)
11 from x t2
12 where t2.id = t1.id) cnt
13* from x t1
SQL> /
ID CNT
---------- ----------
1 1
2 1
3 1
4 1
5 1
6 1
6 rows selected.

Resources