SQL Server Count - Group By - sql

I have a table contain records as per below image. I want to do a count for each status and am able to do that by selecting each type of status. Which I will needs to exec 4 query to get the result. I would like to know how can I achieve that by using single query statement? Any advices or suggestion is welcome and highly appreciated. Thanks in advance.

WITH CTE(NO,STATUS)AS
(
SELECT 1,'OPEN' UNION ALL
SELECT 2,'OPEN'UNION ALL
SELECT 3,'BILLED'UNION ALL
SELECT 4,'CANCELLED'UNION ALL
SELECT 5,'BILLING'UNION ALL
SELECT 6,'BILLED'UNION ALL
SELECT 7,'CANCELLED'UNION ALL
SELECT 8,'BILLING'UNION ALL
SELECT 9,'CONFIRM'UNION ALL
SELECT 10,'IN PROGRESS'UNION ALL
SELECT 11,'OPEN'UNION ALL
SELECT 12,'CONFIRM'
)
SELECT
SUM(CASE WHEN C.STATUS='BILLED'THEN 1 ELSE 0 END)AS BILLED,
SUM(CASE WHEN C.STATUS='BILLING'THEN 1 ELSE 0 END)AS BILLING,
SUM(CASE WHEN C.STATUS='CANCELLED'THEN 1 ELSE 0 END)AS CANCELLED,
SUM(CASE WHEN C.STATUS NOT IN ('CANCELLED','BILLING','BILLED')THEN 1 ELSE 0 END)AS UNBILL
FROM CTE AS C
CTE is an example you have provided. Please replace reference to it with reference to your table

Related

Is there a way to collect the data and inspect in one pass using groupby function

Sample Data of table_1
Have this Query that returns
select
customer,
SUM(CASE WHEN activity IN ( 'a','b')
THEN 1
ELSE 0 END) AS num_activity_a_or_b
from table_1
group by customer
Results:
Want to extend this to return one more column if for a given code say X1 if the Activity is "a" and "c" then return num_of_a_and_c_activity.
A bit stuck how to collect and inpect the code and activities in one pass.
can we combine windowing function to achieve this.
Please advise and help
UPDATE:
based on the updated results, maybe the below query is what you need
So what i assume is that you need both a and c as well x1 .
So I count distinct activities which are a and c and then do integer division by 2. if only a is present then count distinct =1 but 1/2 =0 in integer division.
It is only 1 when both a and c are present.
select
customer,
SUM(CASE WHEN activity IN ( 'a','b')
THEN 1
ELSE 0
END) AS num_activity_a_or_b,
COUNT(DISTINCT CASE WHEN code IN ('x1') AND activity IN ( 'a','c')
THEN activity
ELSE NULL
END)/2 AS num_activity_a_and_c
from table_1
group by customer
Maybe your query can be
select
customer,
SUM(CASE WHEN activity IN ( 'a','b')
THEN 1
ELSE 0
END) AS num_activity_a_or_b,
SUM(CASE WHEN code IN ('x1') AND activity IN ( 'a','c')
THEN 1
ELSE 0
END) AS num_activity_a_or_c
from table_1
group by customer

SQL query - group by string prefix

I'm struggling with a grouping query.
I have simple table named CarParts where some car elements stored in it.
Some of those elements are available (with Type prefix "05") and some are blocked (Type prefix "01").
I want to write select query that would group my table CarParts by SerialNr and Type as shown below on the right side.
Do you want conditional aggregation?
select serialnr, name,
sum(case when type like '%-05' then amount else 0 end) as [05-available],
sum(case when type like '%-01' then amount else 0 end) as [01-blocked]
from carparts
group by serialnr, name;
You can use PIVOT to get your desired result as below-
SELECT SerialNr,
ISNULL([05-Available],0) [05-Available],
ISNULL([01-Available],0) [01-Available],
Name
FROM
(
SELECT SerialNr,Amount,Name,RIGHT( Type,2) +'-Available' AS P_Column
FROM CarParts
) AS P
PIVOT
(
SUM(Amount)
FOR P_Column IN ([01-Available],[05-Available])
) AS PVT
you can use case when
select SerialNr,Name,
sum(case when right([Type],2)='01' then amount else 0 end) as blocked_01
sum(case when right([Type],2)='05' then amount else 0 end) as availabe_05
from tbale_name group by SerialNr,Name
select SerialNr,
sum(case when Type like '%-05' then Amount else 0 end) as '05-available',
sum(case when Type like '%-01' then Amount else 0 end) as '01-blocked',
Name
from carparts
group by SerialNr, Name

Problems with my WHERE clause (SQL)

I'm trying to write a query that returns the following columns:
owner_id,
number_of_concluded_bookings,
number_of_declined_bookings,
number_of_inquiries
However, the problem is that my WHERE clause messes up the query because I am querying the same table. Here is the code:
SELECT owner_id,
Count(*) AS number_of_cancelled_bookings
FROM bookings
WHERE state IN ('cancelled')
GROUP BY owner_id
ORDER BY 1;
It's easy to retrieve the columns individually, but I want all of them. Say I wanted number_of_concluded_bookings as well, that would mean I'd have to alter the WHERE clause ...
Help is greatly appreciated!
Consider conditional aggregations:
SELECT owner_id,
SUM(CASE WHEN state='concluded' THEN 1 ELSE 0 END) AS number_of_concluded_bookings,
SUM(CASE WHEN state='cancelled' THEN 1 ELSE 0 END) AS number_of_cancelled_bookings,
SUM(CASE WHEN state='declined' THEN 1 ELSE 0 END) AS number_of_declined_bookings,
SUM(CASE WHEN state='inquiries' THEN 1 ELSE 0 END) AS number_of_inquiries
FROM bookings
GROUP BY owner_id

SQL Query to add result set values as column values

I want to add the below queries as the column values without creating a table.
Select 'NetworkKey' as AuthKey
Select count(NetworkSK) as Totalcount from EDW.Fact.AuthorizationRequest
Select count(*) as NUllcount from EDW.Fact.AuthorizationRequest where NetworkSK is NULL
Select count(*) as NotNullcount from EDW.Fact.AuthorizationRequest where NetworkSK is Not NULL
My result should look like this without creating a table physically...
AuthKey Totalcount Nullcount NotNullCount
NetworkKey 100 5 95
YOU WANT TO DO IT LIKE THIS BECAUSE THIS WILL WORK TO SOLVE YOUR PROBLEM.
SELECT 'NetworkKey' AS AuthKey,
COUNT(*) AS TotalCount,
SUM(CASE WHEN NetworkSK IS NULL THEN 1 ELSE 0 END) AS NUllcount,
SUM(CASE WHEN NetworkSK IS NOT NULL THEN 1 ELSE 0 END) AS NotNullcount
FROM EDW.Fact.AuthorizationRequest
Happy holidays.

Specific where for multiple selects

I have the following problem:
I have a table that looks something like this:
ArticleID|Group|Price
1|a|10
2|b|2
3|a|3
4|b|5
5|c|5
6|f|7
7|c|8
8|x|3
Now im trying to get a result like this:
PriceA|PriceRest
13|30
Meaning I want to sum all prices from group a in one column and the sum of everything else in another column.
Something like this doesnt work.
select
sum(Price) as PriceGroupA
sum(Price) as PriceRest
from
Table
where
Group='a'
Group<>'a'
Is there a way to achieve this functionality?
SELECT
sum(case when [Group] = 'a' then Price else 0 end) as PriceA,
sum(case when [Group] <> 'a' then Price else 0 end) as PriceRest
from
Table
Please try:
select
sum(case when [Group]='A' then Price end) PriceA,
sum(case when [Group]<>'A' then Price end) PriceRest
from
Table
SQL Fiddle Demo
You just need two sub-queries:
SELECT (SELECT SUM(PRICE)
FROM Table1
WHERE [Group] ='a') AS PriceGroupA,
(SELECT SUM(PRICE)
FROM Table1
WHERE [Group]<>'a') AS PriceRest
Demo-Fiddle