How to group by for different conditions - sql

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

Related

How do I return a column from an aggregate subquery to the outer select statement that is using GROUP BY?

I want to get better at subqueries which is why I am not using CTEs...
I want to generate a table of unique agent IDs that shows count of transactions, sum of sales volume and sum of commission. I also want to show those same three aggregate columns in that table where the city equals x, y and z.
SELECT
agent_id
, COUNT(transaction_id) AS count_all
, (SELECT
COUNT(transaction_id)
FROM table1
WHERE city = 'x'
GROUP BY agent_id) AS count_x
FROM table1
GROUP BY agent_id
This is an example of what I wrote but I get an error returning "Single-row subquery returns more than one row".
SELECT
t1.agent_id
, COUNT(DISTINCT t1.transaction_id) AS count_all
, (SELECT
COUNT(DISTINCT transaction_id)
FROM table1
WHERE city = 'x'
AND agent_id = t1.agent_id) AS count_x
, (SELECT
COUNT(DISTINCT transaction_id)
FROM table1
WHERE city = 'y'
AND agent_id = t1.agent_id) AS count_y
, (SELECT
COUNT(DISTINCT transaction_id)
FROM table1
WHERE city = 'z'
AND agent_id = t1.agent_id) AS count_z
, SUM(sales_volume) AS total_sales
FROM table1 t1
GROUP BY t1.agent_id;

Results from multiple queries as one result in Oracle

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;

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

Count based on group with filter

I have a query that displays 2 columns: "Device_ID" and "Status". Device_ID is the name of all computers and status contains either "reboot" or "success" as values. I would like a third column that would count how many "success" there are for that specific Device_ID.
How could I go about doing this?
SELECT tgt.Device_ID, tgt.Status, src.cnt
FROM [TableName] tgt
INNER JOIN
(
Select Device_ID, count(CASE WHEN Status = 'SUCCESS' THEN 1 ELSE 0END) cnt
from [TableName]
GROUP BY Device_ID
) src
ON tgt.Device_ID= src.Device_ID;
SELECT A.Device_ID,A.Status,B.Count_of_Success_per_Device_ID
FROM Yourtable A
INNER JOIN
(
SELECT Device_ID,
SUM( CASE WHEN Status = 'Success' THEN 1 ELSE 0 END ) AS Count_of_Success_per_Device_ID
FROM Yourtable
GROUP BY Device_ID
) B
ON A.Device_ID = B.Device_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