0

I have a Tickets table in My database , each Ticket have a status_id (1,2,3)

1:  Ticket IN PROGRESS
2:  Ticket Out Of time
3:  Ticket Closed 
  • I want using SQL to calculate the number of tickets for each status .

  • Calculate the cumulative total for each Status in a specific Date, I have already a column affectation_Date that contains the date where the status of ticket has been changed .

3
  • 2
    Sample data and desired results, please. Commented Jan 12, 2019 at 19:28
  • 2
    What did you try so far? Please edit your question to post sample data and expected results too all as formatted text not images. Commented Jan 12, 2019 at 19:28
  • 1
    Also, do you want one (1) query or two (2)? Commented Jan 12, 2019 at 19:44

5 Answers 5

1

Use conditional aggregation as

SELECT TicketID,
       AffectationDate,
       SUM(CASE WHEN StatusID = 1 THEN 1 ELSE 0 END) InProgress,
       SUM(CASE WHEN StatusID = 2 THEN 1 ELSE 0 END) OuOfTime,
       SUM(CASE WHEN StatusID = 3 THEN 1 ELSE 0 END) Closed,
       COUNT(1) Total
FROM Tickets
GROUP BY TicketID,
         AffectationDate
ORDER BY TicketID,
         AffectationDate;

Or if you want to GROUP BY AffectationDate only

SELECT AffectationDate,
       SUM(CASE WHEN StatusID = 1 THEN 1 ELSE 0 END) TotalInProgress,
       SUM(CASE WHEN StatusID = 2 THEN 1 ELSE 0 END) TotalOutOfTime,
       SUM(CASE WHEN StatusID = 3 THEN 1 ELSE 0 END) TotalClosed,
       COUNT(1) TotalStatusThisDate
FROM Tickets
GROUP BY AffectationDate
ORDER BY AffectationDate;

Live Demo

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

Comments

0

Using conditional counts.

SELECT affectation_Date,
COUNT(CASE WHEN status_id = 1 THEN 1 END) AS TotalInProgress,
COUNT(CASE WHEN status_id = 2 THEN 1 END) AS TotalOutOfTime,
COUNT(CASE WHEN status_id = 3 THEN 1 END) AS TotalClosed
FROM Tickets t
GROUP BY affectation_Date
ORDER BY affectation_Date

Comments

0

you may use the desired filter condition for the date criteria

SELECT COUNT(1), STATUS 
FROM tickets
WHERE affectation_Date >= 'someDate'
group by status

Regards

Comments

0

You just need to group by status and count the number of tickets in each group:

select status, count(*) as number
from Tickets
where dt >= '2019-01-01 00:00:00' and dt < '2019-01-02 00:00:00'
group by status
having status >= 1 and status <= 3

Comments

0

This adds the Cumulative Sum to the existing answers:

SELECT AffectationDate,
       Sum(CASE WHEN StatusID = 1 THEN 1 ELSE 0 END) AS TotalInProgress,
       Sum(CASE WHEN StatusID = 2 THEN 1 ELSE 0 END) AS TotalOutOfTime,
       Sum(CASE WHEN StatusID = 3 THEN 1 ELSE 0 END) AS TotalClosed,
       Count(*) as TotalStatusThisDate,
       Sum(Sum(CASE WHEN StatusID = 1 THEN 1 ELSE 0 END)) Over (ORDER BY AffectationDate) AS cumTotalInProgress,
       Sum(Sum(CASE WHEN StatusID = 2 THEN 1 ELSE 0 END)) Over (ORDER BY AffectationDate) AS cumTotalOutOfTime,
       Sum(Sum(CASE WHEN StatusID = 3 THEN 1 ELSE 0 END)) Over (ORDER BY AffectationDate) AS cumTotalClosed,
       Sum(Count(*)) Over (ORDER BY AffectationDate) AS cumTotalStatusThisDate
FROM Tickets
GROUP BY AffectationDate
ORDER BY AffectationDate;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.