简体   繁体   中英

SQL Server Query Accumulating Total

I've created a database storing Incident tickets. I have a fact table and a number of dimension tables.

Here is some sample data

+---------------------+--------------+--------------+-------------+------------+
| LastModifiedDateKey | TicketNumber |    Status    | factCurrent |    Date    |
+---------------------+--------------+--------------+-------------+------------+
|                2774 | T:9992260    | Open         | 1           | 4/12/2017  |
|                2777 | T:9992805    | Open         | 1           | 7/12/2017  |
|                2777 | T:9993068    | Open         | 1           | 7/12/2017  |
|                2777 | T:9993098    | Open         | 0           | 7/12/2017  |
|                2793 | T:9993098    | Acknowledged | 0           | 23/12/2017 |
|                2928 | T:9993098    | Closed       | 1           | 5/01/2018  |
|                2777 | T:9993799    | Open         | 0           | 7/12/2017  |
|                2928 | T:9993799    | Closed       | 1           | 5/01/2018  |
|                2778 | T:9994729    | Open         | 1           | 8/12/2017  |
|                2774 | T:9994791    | Open         | 0           | 4/12/2017  |
|                2928 | T:9994791    | Closed       | 1           | 5/01/2018  |
|                2777 | T:9994912    | Open         | 1           | 7/12/2017  |
|                2778 | T:9995201    | Open         | 0           | 8/12/2017  |
|                2793 | T:9995201    | Closed       | 1           | 23/12/2017 |
|                2931 | T:9718629    | Open         | 1           | 8/01/2018  |
|                2933 | T:9718629    | Closed       | 1           | 10/01/2018 |
|                2932 | T:9855664    | Open         | 1           | 9/01/2018  |
|                2931 | T:9891975    | Open         | 1           | 8/01/2018  |
+---------------------+--------------+--------------+-------------+------------+

I want a query that will give me the total of tickets open at the end of each month.

In the data December (End of) should have a count of 8 and Jan a count of 10. Note: A ticket can have multiple rows with same status because a dimension key has changed or multiple rows with different status all in the same month. eg T:9993098. This query gives the count for DEC if I change '2018-01-01' to '2018-02-01' I have the count for Jan.

SELECT count(DISTINCT TicketNumber)
FROM   [dbo].[factTicket] ft
       INNER JOIN dimDate dd ON ft.LastModifiedDateKey = dd.dateKey
WHERE  DATE < '2018-01-01'
  AND  TicketNumber NOT IN 
       (
          SELECT DISTINCT TicketNumber
          FROM   [dbo].[factTicket] ft
                 INNER JOIN dimDate dd ON ft.LastModifiedDateKey = dd.dateKey
          WHERE  STATUS = 'closed'
            AND  DATE < '2018-01-01'
        )

You can try to group using MONTH() on your date column like below

SELECT COUNT(DISTINCT TicketNumber),MONTH(Date) FROM [dbo].[factTicket]
WHERE Status = 'Open'
GROUP BY MONTH(Date)

This can be done with cumulative sum.

with cte(LastModifiedDateKey, TicketNumber, Status, factCurrent, Date) as (
    select a, b, c, d, cast(e as datetime) from (values 
    (2774,'T:9992260','Open',1,'4/12/2017')
    ,(2777,'T:9992805','Open',1,'7/12/2017')
    ,(2777,'T:9993068','Open',1,'7/12/2017')
    ,(2777,'T:9993098','Open',0,'7/12/2017')
    ,(2793,'T:9993098','Acknowledged',0,'23/12/2017')
    ,(2928,'T:9993098','Closed',1,'5/01/2018')
    ,(2777,'T:9993799','Open',0,'7/12/2017')
    ,(2928,'T:9993799','Closed',1,'5/01/2018')
    ,(2778,'T:9994729','Open',1,'8/12/2017')
    ,(2774,'T:9994791','Open',0,'4/12/2017')
    ,(2928,'T:9994791','Closed',1,'5/01/2018')
    ,(2777,'T:9994912','Open',1,'7/12/2017')
    ,(2778,'T:9995201','Open',0,'8/12/2017')
    ,(2793,'T:9995201','Closed',1,'23/12/2017')
    ,(2931,'T:9718629','Open',1,'8/01/2018')
    ,(2933,'T:9718629','Closed',1,'10/01/2018')
    ,(2932,'T:9855664','Open',1,'9/01/2018')
    ,(2931,'T:9891975','Open',1,'8/01/2018')) t(a,b,c,d,e)
)

select
    mon, iif(min(cs) > 0, 0, abs(min(cs)))
from (
    select
        TicketNumber, status, eomonth(date) mon
        , sum(iif(status = 'Closed', 1, -1)) over (order by eomonth(date), status rows unbounded preceding) cs
    from 
        cte
    where
        status in ('Open','Closed')
    group by TicketNumber, status, eomonth(date)
) t
group by mon

Output:

+---------------+-------+
| mon           | cnt   |
| 2017-12-31    | 8     |
| 2018-01-31    | 7     |
+---------------+-------+

I dont know why you expect to have 10 for January. You have only 7 open tickets in your sample data

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM