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
Related
proc sql;
create table final as select drink,
count(distinct id) as total_persons,
count(distinct case when age_cat = '20-25' then id end) as tot_20_25,
count(distinct case when age_cat = '25-30' then id end) astot_25_30,
count(distinct case when age_cat = '30-35' then id end) as tot_30_35
from old_table
group by drink
order by total_persons
quit;
This table gives me what i want,but i would like one more row as the totals for all persons and for each category.I can get that single row with correct number if remove from above code the group by steatement.
Is any way to have both cases,by drinks ,and also in same table totals for each category?
You can use union all:
proc sql;
create table final as
select drink,
count(distinct id) as total_persons,
count(distinct case when age_cat = '20-25' then id end) as tot_20_25,
count(distinct case when age_cat = '25-30' then id end) as tot_25_30,
count(distinct case when age_cat = '30-35' then id end) as tot_30_35
from old_table
group by drink
union all
select 'Total',
count(distinct id) as total_persons,
count(distinct case when age_cat = '20-25' then id end) as tot_20_25,
count(distinct case when age_cat = '25-30' then id end) as tot_25_30,
count(distinct case when age_cat = '30-35' then id end) as tot_30_35
from old_table;
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;
i am having a query with conditional group by , i was just wondering is there a way to have select statement with that condition
my query is
select sum(totalamount),typetitle,
from #temp group by (case when Type ='1' then typetitle else Type end)
with this query i get error at typetitle as its not in group by statement , is there any workaround for this?
Tr like below
select sum(totalamount),
(case when Type ='1' then typetitle else Type end)
from #temp
group by (case when Type =1 then typetitle else Type end)
In group by projection column name and group by column have to be same
If you don't want to repeat the expression, then apply is one method:
select v.typegroup, sum(v.totalamount),
from #temp t cross apply
(values (case when Type ='1' then typetitle else Type end)
) v(typegroup)
group by v.typegroup;
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
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