我创建了一个存储事件故障单的数据库。 我有一个事实表和一些维度表。
以下是一些示例数据
+---------------------+--------------+--------------+-------------+------------+
| 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 |
+---------------------+--------------+--------------+-------------+------------+
我想要一个查询,它会在每个月末给我打开总票数。
在数据中,12月(结束时)的计数应为8,Jan计数为10。 注意:票证可以包含多个具有相同状态的行,因为维度键已更改,或者同一个月中的多个行具有不同的状态。例如T:9993098。 如果我将'2018-01-01'更改为'2018-02-01',此查询会给出DEC的计数。我有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'
)
答案 0 :(得分:0)
您可以尝试在日期栏中使用MONTH()
进行分组,如下所示
SELECT COUNT(DISTINCT TicketNumber),MONTH(Date) FROM [dbo].[factTicket]
WHERE Status = 'Open'
GROUP BY MONTH(Date)
答案 1 :(得分:0)
这可以通过累积总和来完成。
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
输出:
+---------------+-------+
| mon | cnt |
| 2017-12-31 | 8 |
| 2018-01-31 | 7 |
+---------------+-------+
我不知道为什么你希望1月份有10个。您的样本数据中只有7张打开的门票