Results from multiple queries as one result in Oracle - sql

I have results from same query with multiple conditions as shown below:
SELECT COUNT(DISTINCT CASEID) FROM MYTABLE WHERE YR=2019 AND STATUS IN('W')
SELECT COUNT(DISTINCT CASEID) FROM MYTABLE WHERE YR=2019 AND STATUS IN('K') AND APSTAT='J'
SELECT COUNT(DISTINCT CASEID) FROM MYTABLE WHERE YR=2019 AND STATUS IN('L') AND RCODE='901'
SELECT COUNT(DISTINCT CASEID) FROM MYTABLE WHERE YR=2019 AND STATUS IN('L') AND
RCODE='910'

You could use conditional aggregation:
SELECT
COUNT(DISTINCT CASE WHEN STATUS IN('W') THEN CASEID END)
,COUNT(DISTINCT CASE WHEN STATUS IN('K') AND APSTAT='J' THEN CASEID END)
,COUNT(DISTINCT CASE WHEN STATUS IN('L') AND RCODE='901' THEN CASEID END)
,COUNT(DISTINCT CASE WHEN STATUS IN('L') AND RCODE='910' THEN CASEID END)
FROM MYTABLE
WHERE YR=2019;

Related

SQL: Select within Select as subquery

I've got the following statement:
select
product_name as ShortestLength = (select top 1 product_name, len(fact_name) Value_Length
from table
order by Value_Length, fact_name ASC)
Which returns this output:
shortestlength
PS
I'd like to add this outcome to another select statement:
select
'Product' as Column_Name,
avg(case when product is null then 1.000 else 0 end) * 100 as PctMissing,
count(product) as TotalCount,
count(distinct product) as UniqueCount
from
table
so the result will be:
column_name
pctmissing
totalcount
uniquecount
shortestlength
Product
5.100
181186
15
PS
What should I add to my initial select statement?
You can use conditional aggregation:
select 'Product' as Column_Name,
avg(case when t.product is null then 1.000 else 0 end)*100 as PctMissing,
count(t.product) as TotalCount,
count(distinct t.product) as UniqueCount,
max(case when seqnum = 1 then product_name end) as shortest_length
from (select t.*,
row_number() over (order by len(fact_name), fact_name) as seqnum
from table t
) t
This assumes that the two table references are really the same table.
You can just use first query as subquery in place of a column in your select statement:
select
'Product' as Column_Name,
avg(case when product is null then 1.000 else 0 end)*100 as PctMissing,
count(product) as TotalCount,
count(distinct product) as UniqueCount,
(select top 1 product_name from table order by Value_Length, fact_name ASC) as ShortestLength
from table

Nested Select with Count Distinct and Group By

So I'm trying to get 2 sets or results from the same table grouped by a 3rd column, it's best i let my example explain;
SELECT
(SELECT COUNT(DISTINCT id)
FROM Database
WHERE Status NOT LIKE 'closed') AS ColumnA,
(SELECT COUNT(DISTINCT id)
FROM Database
WHERE Status NOT LIKE 'closed' AND Datevalue <= getdate()) AS ColumnB
Group By ColumnC
Now I know this wont/doesn't work but it explains what I want. If I leave the group by out then i get the figures as a whole but i want them grouping by another Column.
Mind is melting, ready to be enlightened.
Is this what you want?
select columnC,
count(distinct case when Status <> 'closed' then id end) as columnA,
count(distinct case when Status <> 'closed' and datevalue <= getdate() then id end) as columnb
from database -- a very curious name for a table
group by ColumnC;
Try this
SELECT ColumnC,
COUNT(DISTINCT CASE WHEN Status NOT LIKE 'closed' THEN id END) as ColumnA,
COUNT(DISTINCT CASE WHEN Status NOT LIKE 'closed' AND Datevalue <= getdate() THEN id END) as ColumnB
FROM mydatabase
GROUP BY ColumnC

How to group by for different conditions

I have table 1 as follow, I want to group the total money for prospect in one group. However, am facing difficulties to group all money in won status as in Won stage.
I want result like below:
SELECT
name,sum(value),stagename
FROM table1
group by name,stagename
I think you want to aggregate on a combination of the status and stagename columns:
select name,
(case when status = 'won' then 'won' else stagename end) as stagename,
sum(value)
from table1
group by name,
(case when status = 'won' then 'won' else stagename end);
You can query like this:
select name, stagename, sum(value) from #yourstages
where status = 'open'
group by name, stagename
union all
select name, status as StageName, sum(value) from #yourstages
where status = 'won'
group by name, status
For multiple stagename values:
SELECT T1.name, CASE WHEN T1.STAGENAME=T2.STATUS THEN SUM(T2.VALUE) ELSE SUM(T1.value) END AS VALUE, T1.STAGENAME
FROM table1 T1 LEFT JOIN TABLE1 T2 ON T2.STATUS=T1.STAGENAME
group by T1.name,T2.STATUS, T1.stagename

aggregation functions with different where condition sentence

I have SQL query give table of 6 columns from
mytable:
TotalNumberOfRecords
TotalDurationOfCalls
AvgdurationPer
TotalCallednumbers
TotalCallednumbers
Ratiocalledtoallcalls
these columns resulted of aggregation functions. but each one have different where condition sentences in order to select from table.
MY Query like this:
select ID,
count(*) as TotalNumberOfRecords,
sum (isnull(cast(duration as int),0)) {where condition1} as TotalDurationOfCalls ,
AVG(isnull(cast(duration as int),0)){where condition2} as AvgdurationPer,
count(distinct IDS) {where condition3} as TotalCallednumbers ,
count(distinct CGI) {where condition4} as TotalOfLocations,
cast(count(distinct IDS) as float)/cast(count(*) as float) {where condition5} as Ratiocalledtoallcalls
from Mytable
group by ID
Now, my problem is, how can I execute this query in one query to get one table?
You want conditional aggregation. You do this by making a case expression the argument to an aggregation function:
select ID, count(*) as TotalNumberOfRecords,
sum(case when condition1 then cast(duration as int) else 0 end) as TotalDurationOfCalls ,
avg(case when condition2 then cast(duration as int) else 0 end) as AvgdurationPer,
count(distinct case when condition3 then IDS end) as TotalCallednumbers,
count(distinct case when condition4 then CGI end) as TotalOfLocations,
cast(distinct count(case when condition5 then IDS end) as float)/cast(count(*) as float) as Ratiocalledtoallcalls
from Mytable
group by ID

SQL Select for multiple where clause

I am trying to create SQL Select that returns counts of a certain field based on a field.
So, here is what I am trying to do.
Select count(distinct id) as TotalCount, -- this will be the total of id
count(distinct id where type='A') as TotalA, -- this will be total when type='A'
count(distinct id where type='B') as TotalB -- This will be total when type = 'B'
from MyTable
Basically, TotalCount = TotalA + TotalB.
How can I achieve this in SQL Select Statement?
Thanks.
Select count(distinct id) as TotalCount, -- this will be the total of id
count(distinct case type when 'A' then id else NULL end) as TotalA,
count(distinct case type when 'B' then id else NULL end) as TotalB
from MyTable;
Of course TotalCount may or may not be TotalA + TotalB, depending on the actual data.
You can do it like that:
SELECT
count(distinct id) as TotalCount,
sum(CASE WHEN type = 'A' THEN 1 ELSE 0) as TotalA,
sum(CASE WHEN type = 'B' THEN 1 ELSE 0) as TotalB,
FROM
MyTable
Count per type:
SELECT
type,
count(DISTINCT id)
FROM
MyTable
GROUP BY
type
Why not simply UNION the separate queries.
Select 'all' as which, count(distinct id) as Total from mytable
union
select 'a' as which, count(distinct id) where type='A' as Total from mytable
union
select 'b' as which, count(distinct id) where type='B' as Total from mytable