I need help with making a conclusion in SQL Server based on some column's values, like status aggregation sort of. As an example, below is a table containing server tasks and their status.
If I want to return the aggregated status of each server here are the rules:
Example Table: Tasks
Server | Task_Status | Task |
---|---|---|
Server 1 | RUNNING | 1-1 |
Server 1 | COMPLETED | 1-2 |
Server 1 | SUBMITTED | 1-3 |
Server 2 | COMPLETED | 2-1 |
Server 2 | COMPLETED | 2-2 |
Server 3 | SUBMITTED | 3-1 |
Server 3 | SUBMITTED | 3-2 |
Example Query Result:
Server | Completion |
---|---|
Server 1 | IN PROGRESS |
Server 2 | DONE |
Server 3 | AWAITING |
So far I tried to use the following query with no success:
SELECT Server,
CASE
WHEN Task_Status NOT IN ('RUNNING', 'COMPLETED') THEN 'AWAITING' -- Status SUBMITED
WHEN Task_Status NOT IN ('RUNNING', 'SUBMITED') THEN 'DONE' -- Status COMPLETED
ELSE 'IN PROGRESS'
END Completion
FROM Tasks
You could use this:
WITH table_name AS
(
SELECT 'Server 1' AS server, 'RUNNING' AS task_status, '1-1' AS task UNION ALL
SELECT 'Server 1' AS server, 'COMPLETED' AS task_status, '1-2' AS task UNION ALL
SELECT 'Server 1' AS server, 'SUBMITTED' AS task_status, '1-3' AS task UNION ALL
SELECT 'Server 2' AS server, 'COMPLETED' AS task_status, '2-1' AS task UNION ALL
SELECT 'Server 2' AS server, 'COMPLETED' AS task_status, '2-2' AS task UNION ALL
SELECT 'Server 3' AS server, 'SUBMITTED' AS task_status, '3-1' AS task UNION ALL
SELECT 'Server 3' AS server, 'SUBMITTED' AS task_status, '3-2' AS task
)
SELECT server,
CASE
WHEN COUNT(DISTINCT task_status) = 1 AND MAX(task_status) = 'SUBMITTED' THEN 'AWAITING'
WHEN COUNT(DISTINCT task_status) = 1 AND MAX(task_status) = 'COMPLETED' THEN 'DONE'
ELSE 'IN PROGRESS'
END AS completion
FROM table_name
GROUP BY server
ORDER BY server;
Tested in sqlfiddle
select server,
case when count(distinct task_status) > 1 then 'in progress'
else case min(task_status)
when 'completed' then 'done'
when 'submitted' then 'awaiting'
else 'some other result, perhaps an error'
end
end result
from your_table
group by server
select Server,
case max(Task_Status) when 'COMPLETED' then 'DONE' else
(case min(Task_Status) when 'SUBMITTED' then 'AWAITING' else 'IN PROGRESS' end )
end as Completion
from Table_1
group by Server
result
Server (Completion)
Server1 IN PROGRESS
Server2 DONE
Server3 AWAITING
SELECT
server,
CASE
WHEN status = 3 THEN 'IN PROGRESS'
WHEN status = 2 THEN 'WAITING'
WHEN status = 1 THEN 'DONE'
END as completion
FROM (
SELECT
server,
MAX( CASE
WHEN task_status = 'RUNNING' THEN 3
WHEN task_status = 'SUBMITTED' THEN 2
WHEN task_status = 'COMPLETED' THEN 1
END
) status
FROM Tasks
GROUP BY server
) result
select
u.server,
case
when u.cnt=u.cntCompleted then 'DONE'
when u.cnt=u.cntSubmitted then 'AWAITING'
else 'IN PROGRESS'
end Completion
from
(
select
server,
count(*) cnt,
(select count(tstatus) from task t2 where t2.server=t1.server and t2.tstatus='SUBMITTED') cntSubmitted,
(select count(tstatus) from task t2 where t2.server=t1.server and t2.tstatus='COMPLETED') cntCompleted
from task t1
group by server
) u
Thanks to Pham X Bach for table creation script.
You can go for step by step approach, where you calculate the different statuses count and later apply the logic as given below:
WITH table_name AS
(
SELECT 'Server 1' AS server, 'RUNNING' AS task_status, '1-1' AS task UNION ALL
SELECT 'Server 1' AS server, 'COMPLETED' AS task_status, '1-2' AS task UNION ALL
SELECT 'Server 1' AS server, 'SUBMITTED' AS task_status, '1-3' AS task UNION ALL
SELECT 'Server 2' AS server, 'COMPLETED' AS task_status, '2-1' AS task UNION ALL
SELECT 'Server 2' AS server, 'COMPLETED' AS task_status, '2-2' AS task UNION ALL
SELECT 'Server 3' AS server, 'SUBMITTED' AS task_status, '3-1' AS task UNION ALL
SELECT 'Server 3' AS server, 'SUBMITTED' AS task_status, '3-2' AS task
)
select server, case when Completed_Count = total_Count then 'Done'
when Submitted_Count = total_Count then 'Awaiting'
else 'In Progress' end as Completion
from
(
SELECT server, count(case when task_status = 'Completed' then 1 end) as Completed_Count,
count(case when task_status = 'Submitted' then 1 end) as Submitted_Count,
count(*) as total_count
FROM table_name
GROUP BY SERVER
) as t
server | Completion |
---|---|
Server 1 | In Progress |
Server 2 | Done |
Server 3 | Awaiting |
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.