0

I have a column called "status" in a sql table called "latest" which contains five different values new,deployed,resolved,assigned and closed as shown below

 Status
--------------
  NEW
  NEW
  DEPLOYED
  NEW
  RESOLVED
  ASSIGNED
  ASSIGNED
  RESOLVED
  ASSIGNED
  NEW
  NEW
  RESOLVED
  CLOSED
  ASSIGNED

I want to write a query in which i can count the number of times each of these words occur in the column "status". Currently I'm using count function in 5 different queries like this.

 1)select count(status) from latest where status="NEW";
 2)select count(status) from latest where status="DEPLOYED";
 3)select count(status) from latest where status="RESOLVED";
 4)select count(status) from latest where status="ASSIGNED";
 5)select count(status) from latest where status="CLOSED";

Is there a way i could combine all these queries into single query without using joins or union to get 5 different count values??

2

4 Answers 4

1

QUERY 1:

SELECT STATUS, COUNT(1) FROM LATEST
GROUP BY STATUS
ORDER BY STATUS;

Your Output would be:

STATUS         COUNT
ASSIGNED       4
CLOSED         1
DEPLOYED       1
NEW            5
RESOLVED       3

To get the output in single row with 5 columns you can use: QUERY 2:

SELECT COUNT(CASE WHEN STATUS='ASSIGNED' THEN 1 END)  ASSIGNED_COUNT,
COUNT(CASE WHEN STATUS='CLOSED' THEN 1 END)  CLOSED_COUNT,
COUNT(CASE WHEN STATUS='DEPLOYED' THEN 1 END)  DEPLOYED_COUNT,
COUNT(CASE WHEN STATUS='NEW' THEN 1 END)  NEW_COUNT,
COUNT(CASE WHEN STATUS='RESOLVED' THEN 1 END)  RESOLVED_COUNT
FROM LATEST

Your Output would be:

ASSIGNED_COUNT   | CLOSED_COUNT | DEPLOYED_COUNT | NEW_COUNT | RESOLVED_COUNT
4                |  1           | 1              | 5         | 3

EXPLANATION:

  1. Query 1 can be used where there can be any number of dynamic status. For example, even if there is a status named DEFERRED, it would automatically be included without query change. But, if there is say, no record with status as "NEW", then no data would be returned.

  2. Query 2 can be used to return all the data in a single row. If a new status is to be included, then the query must be modified. If there are no records for a status, then 0 would be returned. IDEAL FOR DASHBOARD kind of usage.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this cool one

  Select *
  from
  (
  Select Status,COUNT(*) 'Count'
  from LATEST
  group by Status with rollup
  ) t
  where t.Count is not null

Comments

0

Try this:

SELECT l.status, COUNT(1) 
FROM latest l
GROUP BY l.status;

OR

SELECT SUM(l.status = 'NEW') AS NEW_STATUS,  
       SUM(l.status = 'DEPLOYED') AS DEPLOYED_STATUS, 
       SUM(l.status = 'RESOLVED') AS RESOLVED_STATUS, 
       SUM(l.status = 'ASSIGNED') AS ASSIGNED_STATUS, 
       SUM(l.status = 'CLOSED') AS CLOSED_STATUS
FROM latest l;

UPDATE

Use SELECT...INTO statement to achieve this, check below query

SELECT SUM(l.status = 'NEW') AS NEW_STATUS,  
       SUM(l.status = 'DEPLOYED') AS DEPLOYED_STATUS, 
       SUM(l.status = 'RESOLVED') AS RESOLVED_STATUS, 
       SUM(l.status = 'ASSIGNED') AS ASSIGNED_STATUS, 
       SUM(l.status = 'CLOSED') AS CLOSED_STATUS 
       INTO NEW_STATUS, DEPLOYED_STATUS, RESOLVED_STATUS, 
            ASSIGNED_STATUS, CLOSED_STATUS
FROM latest l;

1 Comment

By using the above query will i get 5 different count values??will i be able to store them in 5 different variables??
0

Try This...

1.>

Select Count() as Count,Status from latest where (status='NEW' Or status='DEPLOYED' or status='RESOLVED' or status='ASSIGNED' Or status='CLOSED') group by Status;*

OR

2.> select count(status) from latest where (status='NEW' Or status='DEPLOYED' or status='RESOLVED' or status='ASSIGNED' Or status='CLOSED');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.