Count based on condition in SQL Server - sql

Does anyone know how can I do a count in SQL Server based on condition.
Example:
How can I do a column count for records with name 'system', and total CaseID records in the table?
Customer table
UserID CaseID Name
1 100 alan
1 101 alan
1 102 amy
1 103 system
1 104 ken
1 105 ken
1 106 system
The result will display like below:
UserID TotalCaseID TotalRecordsWithSystem
1 7 2

Use SUM/CASE...
SELECT
COUNT(*), --total
SUM(CASE WHEN name = 'system' THEN 1 ELSE 0 END) --conditional
FROM
myTable

I think he wanted user id in the results
SELECT
userid,
COUNT(*) as TotalcaseID, --total
SUM(CASE WHEN name = 'system' THEN 1 ELSE 0 END) as TotalRecordsWithSystem
FROM
myTable
group by userid

select
userid,
count('x') as TotalCaseID,
count(case when name = 'system' then 'x' else null end) as TotalRecordsWithSystem
from CustomerTable
group by userid

If you're on SQL Server 2012+, then you can use SUM/IIF
SELECT
COUNT(*) AS Total,
SUM(IIF(Name = 'system', 1, 0)) AS SystemTotal
FROM
CustomerTable

Related

Checking if all values for user_id IS NOT NULL

I have dataset which looks like this:
UserID AccountID CloseDate
1 1000 14/3/2022
1 2000 16/3/2022
2 1000 NULL
2 2000 4/3/2022
2 3000 NULL
And I would like to check if within one user_id all of the close dates are not null. In other words if all accounts within user_id are closed. I was trying using MAX or MIN but it is not working as I expected, because it is simply avoiding NULL values. Is there any other function which can check it? Let's say that my output would be another column which will assign 1 when all CloseDates are not null and else 0.
Sample output:
UserID AccountID CloseDate Check
1 1000 14/3/2022 1
1 2000 16/3/2022 1
2 1000 NULL 0
2 2000 4/3/2022 0
2 3000 NULL 0
Use conditional aggregation to explicitly COUNT the rows where the column has the value NULL:
SELECT GroupedColumn,
COUNT(CASE WHEN NullableColumn IS NULL THEN 1 END) AS NullCount
FROM dbo.YourTable
GROUP BY GroupedColumn;
If you want to just have a 1 or 0 just wrap the count in a CASE expression:
CASE COUNT(CASE WHEN NullableColumn IS NULL THEN 1 END) WHEN 0 THEN 1 ELSE 0 END
You can try to use FIRST_VALUE condition window function
SELECT *,
FIRST_VALUE(IIF(CloseDate IS NULL,0,1)) OVER(PARTITION BY UserID ORDER BY CloseDate )
FROM T
sqlfiddle
with dataset as (select 1 as UserId, 1000 as AccountID, '14/3/2022' as CloseDate
union all select 1, 2000, '16/3/2022'
union all select 2, 1000, NULL
union all select 2, 2000, '4/3/2022'
union all select 2, 3000, NULL)
select userid from dataset
group by userid
having sum(case when closedate is null then 1 else 0 end) = 0;
select d.*, iif(chk>0, 0, 1) chk
from d
outer apply (
select UserId, COUNT(*) CHK
from d dd
WHERE d.UserId = dd.UserId
and dd.CloseDate IS NULL
group by UserId
) C
You can also use "exists". e.g. :
select y.UserID, y.AccountID, y.CloseDate,
-- [Check]: returns 0 if there is a row in the table for the
-- UserID where CloseDate is null, else 1
(case when exists(select * from YourTable y2 where y2.UserID = y.UserID
AND y2.CloseDate is null) then 0 else 1 end) as [Check]
from YourTable y

Error in SQL command using Group by?

I'm beginner in Oracle SQL. I am using SQL Developer. This query is not executed. I need for each id like 1001,1002 how many no of yes status and how many no of no status. Thanks in advance....
I used this SQL:
SELECT ID, COUNT(STATUS)
FROM TABLE1
WHERE
GROUP BY ID, STATUS
HAVING STATUS = YES OR STATUS = NO;
I have table like this:
id school status
--------------------------
1001 vani YES
1002 sunbeam YES
1001 shristri YES
1002 jain NO
1001 holycross YES
1001 vani NO
I need output like
id yesstatus Nostatus
-------------------------
1001 3 1
1002 1 1
Your current query syntax is really wrong, but you can do conditional aggregation :
select id,
sum(case when status = 'YES' then 1 else 0 end) as yesstatus,
sum(case when status = 'NO' then 1 else 0 end) as Nostatus
from table1 t1
where status in ('YES', 'NO')
group by id;
select status, count(status)
from your_table
group by status
CREATE TABLE dbo.TABLE1
(
id INT,
school NVARCHAR(50) ,
status BIT
)
INSERT INTO dbo.TABLE1 ( id ,
school ,
status )
VALUES (1001,'vani',1),
(1002,'sunbeam', 1),
(1001,'shristri',1),
(1002,'jain',0),
(1001,'holycross',1),
(1001,'vani', 0)
SELECT id,
SUM(CASE WHEN status=1 THEN 1 ELSE 0 END) as yesstatus,
SUM(CASE WHEN status=0 THEN 1 ELSE 0 END) as Nostatus
FROM dbo.TABLE1
GROUP BY id

SQL select row count by unique ID group by range

Given that I have the following two tables:
User
-------
UserID
UserLikes
----------
UserID
OtherUserID
When a user likes another user, the user performing the like gets stored as UserID and the user receiving the like gets stored as OtherUserID.
How can I get a distribution showing a count of how many users that have given:
0 likes
1-10 likes
11-50 likes
51-100 likes
101 + likes
For example, people that liked between 1 and 10:
SELECT T.userID,count(*) as TotalLikes
FROM UserLikes t
GROUP BY T.userID
HAVING COUNT(*) between 1 and 10
To change the range, just change the having clause
HAVING COUNT(*) = 0
HAVING COUNT(*) between 1 and 10
HAVING COUNT(*) between 11 and 50
HAVING COUNT(*) between 51 and 100
HAVING COUNT(*) > 100
If you meant you want the results to be:
0 likes | 1-10 likes | ....
3 5 ....
Then:
SELECT sum(case when TotalLikes = 0 then 1 else 0 end) as No_Likes,
SELECT sum(case when TotalLikes between 1 and 10 then 1 else 0 end) as One_To_ten_likes,
SELECT sum(case when TotalLikes between 11 and 50 then 1 else 0 end) as Eleven_To_Fifty_Likes,
.......
FROM(
SELECT T.userID,count(*) as TotalLikes
FROM UserLikes t
GROUP BY T.userID)

Count Values from Table for each type

I have the following table
UserId [nvarchar(128)], Rating [varchar(170)] :values will be mostly 1,2,3 but can have exceptions
Rating contains 3 values [1,2, or 3]
I want to get a result something like
UserId Count(1's),Count(2's) Count(3's)
1. 1001 10 8 2
2. 1002 5 10 3
Is it possible in a single query
Do a GROUP BY UserId to count for each user-id. Use CASE to count 1's, 2's and 3's separately:
select UserId,
count(case when Rating = 1 then 1 end) as [Count(1's)],
count(case when Rating = 2 then 1 end) as [Count(2's)],
count(case when Rating = 3 then 1 end) as [Count(3's)]
from tablename
group by UserId
Use a CASE statement in each COUNT and then GROUP BY UserId.
SELECT UserId, COUNT(CASE WHEN value = '1' THEN 1 END) AS [Count(1's)], COUNT(CASE WHEN value = '2' THEN 1 END) AS [Count(2's)], COUNT(CASE WHEN value = '3' THEN 1 END) AS [Count(3's)]
FROM yourtable
GROUP BY UserId
Use PIVOT:
SELECT
UserId,
COALESCE([1],0) [Count(1's)],
COALESCE([2],0) [Count(2's)],
COALESCE([3],0) [Count(3's)]
FROM
ýour_table
PIVOT
(COUNT([Rating])
FOR Rating
in([1],[2],[3])
)AS p
ORDER BY
UserId

select from 2 tables with multiple counts

I have 2 tables I'm trying to join in a select query.
Table 1: Store, primary_key(id,store_num)
store_id store_num due_date manager_id
1 100 06-30-2024 user1
2 108 06-30-2018 user2
3 109 13-31-2014 user3
Table 2: Department, where status(A-applied,p-Pending)
store_id store_num dept_num status
1 100 201 A
1 100 202 A
1 100 203 P
1 100 204 A
1 100 205 P
1 100 206 A
Expecting to select store_id, store_num, due_date, manager_id, Applied count, pending count. The result is something looks like this.
store_id store_num due_date manager_id applied_count pending_count
1 100 06-30-2024 user1 4 2
I tried it and got where I am able to join and get it in multiple rows, But counts not working out for me. can some one help me how I can get the counts
select
store.store_id,
store.store_num,
store.due_date,
store.manager_id,
dept.status
from store as store
inner join department as dept on store.store_id = dept.store_id
and store.store_num = dept.store_num
Your query is half way done. You need to do an aggregation to get the values in different columns. This is a conditional aggregation, as shown here:
select s.store_id, s.store_num, s.due_date, s.manager_id,
sum(case when d.status = 'A' then 1 else 0 end) as Active_Count,
sum(case when d.status = 'P' then 1 else 0 end) as Pending_Count
from store s inner join
department as dept
on s.store_id = d.store_id and s.store_num = d.store_num
group by store.store_id, store.store_num, store.due_date, store.manager_id;
The expression:
sum(case when d.status = 'A' then 1 else 0 end) as Active_Count,
Is counting the rows where status = 'A'. It does so by assigning such rows a value of 1 and then summing up that value.