我试图显示按项目分组的2019年所有已完成任务的累积计数。 我已经查看过,并且部分起作用。但是我想要实现的是,当我在2019年没有任何未完成的任务时,但是在前几年中有这些任务时,我想将这些任务的数量添加到2019年1月的价值中。
我让我查看了所有累积任务的计数:
SELECT
*,
SUM(a.cnt) OVER (PARTITION BY Project ORDER BY Jahr,Monat,Project RANGE UNBOUNDED PRECEDING) as cum_cnt_of_tasks
FROM
(
SELECT count(distinct [StoryID])as cnt,datepart(month,[Story_Resolutiondate]) as Monat, datepart(year,[Story_Resolutiondate]) as Jahr,v.ProjectID as Project
FROM [Story]s
join [Epic]e on s.EpicID=e.EpicID
join [Project]v on e.ProjectID=v.ProjectID
group by datepart(month,[Story_Resolutiondate]) ,datepart(year,[Story_Resolutiondate]),v.ProjectID
) AS a
这是结果:
Monat Jahr Project cum_cnt_of_tasks
6 2017 259052 1
7 2017 259052 2
8 2017 259052 3
9 2017 259052 4
1 2019 255403 4
2 2019 255405 2
但是我想把它变成:
Monat Jahr Project cum_cnt_of_tasks
1 2019 259052 4
1 2019 255403 4
2 2019 255405 2
所以我应该提取前些年259052项目的最后累积值,并将其映射为2019年1月的值:
因此,项目259052的最后累积计数为2017年9月(Monat = 9)。我想将此值映射到2019年1月。
Monat Jahr Project cum_cnt_of_tasks
6 2017 259052 1
7 2017 259052 2
8 2017 259052 3
*9 2017 259052 4*
INTO
Monat Jahr Project cum_cnt_of_tasks
*1 2019 259052 4*
@Gordon Linoff 如果我在2019年1月之后没有数据,它会起作用,但是如果我有数据,那么我将从以下数据集中获取数据:
M J Project cnt
7 2018 17323 34
8 2018 17323 36
9 2018 17323 37
10 2018 17323 40
11 2018 17323 41
12 2018 17323 43
1 2019 17323 44
2 2019 17323 47
此设置:
m j Project cnt
2 2019 17323 47
但是我实际上需要:
m j Project cnt
1 2019 17323 44
2 2019 17323 47
因此,如果数据集中已经存在2019年1月,那么我会因为您的查询而丢失
答案 0 :(得分:1)
使用GROUP BY
和SUM()
:
SELECT
q.Monat
, q.Jahr
, q.Project
, SUM(q.cum_cnt_of_tasks) SumCnt_of_tasks
FROM
(
SELECT
*,
SUM(a.cnt) OVER (PARTITION BY Project ORDER BY Jahr,Monat,Project RANGE
UNBOUNDED PRECEDING) as cum_cnt_of_tasks
FROM
(
SELECT
count(distinct [StoryID])as cnt
, datepart(month,[Story_Resolutiondate]) as Monat
, datepart(year,[Story_Resolutiondate]) as Jahr
,v.ProjectID as Project
FROM [Story]s
join [Epic]e on s.EpicID=e.EpicID
join [Project]v on e.ProjectID=v.ProjectID
group by datepart(month,[Story_Resolutiondate])
,datepart(year,[Story_Resolutiondate])
,v.ProjectID
) AS a
)q
GROUP BY q.Monat
, q.Jahr
, q.Project
, q.cum_cnt_of_tasks
更新:
尝试使用以下查询:
SELECT
CASE
WHEN t.Jahr = 2017 THEN 1
ELSE MAX(t.Monat)
END AS Monat
, CASE
WHEN t.Jahr = 2017 THEN 2019
ELSE t.Jahr
END AS Jahr
, t.Project
, COUNT(t.cum_cnt_of_tasks) Cnt
FROM @Table t
GROUP BY
t.Jahr
, t.Project
示例数据:
DECLARE @Table TABLE
(
Monat INT,
Jahr INT,
Project INT,
cum_cnt_of_tasks INT
)
INSERT INTO @Table
(
Monat,
Jahr,
Project,
cum_cnt_of_tasks
)
VALUES
( 6, 2017, 259052, 1)
, ( 7, 2017, 259052, 2)
, ( 8, 2017, 259052, 3)
, ( 9, 2017, 259052, 4)
, ( 1, 2019, 255403, 4)
, ( 2, 2019, 255405, 2)
输出:
Monat Jahr Project Cnt
1 2019 255403 1
2 2019 255405 1
1 2019 259052 4
答案 1 :(得分:0)
我认为您可以通过一次汇总来做到这一点:
select (case when max(Story_Resolutiondate) < '2019-01-01' then 1 else month(max(Story_Resolutiondate))
end) as monat,
(case when max(Story_Resolutiondate) < '2019-01-01' then 2019 else year(max(Story_Resolutiondate))
end) as jarh,
v.projectId,
count(distinct StoryId)
from [Story] s join
[Epic] e
on s.EpicID = e.EpicID join
[Project] v
on e.ProjectID = v.ProjectID
group by v.ProjectId;
我不是100%的人数正是您想要的。您的数据表明您希望计数 months ,但是查询的逻辑不同。两者都是可行的。
编辑:
我认为union all
可以满足您的要求:
select 1 as monat, 2019 as jarh, v.projectId, count(distinct StoryId)
from [Story] s join
[Epic] e
on s.EpicID = e.EpicID join
[Project] v
on e.ProjectID = v.ProjectID
group by v.ProjectId
having max(Story_Resolutiondate) < '2019-01-01'
union all
select month(Story_Resolutiondate) as monat,
year(Story_Resolutiondate) as jahr,
v.projectId, count(distinct StoryId)
from [Story] s join
[Epic] e
on s.EpicID = e.EpicID join
[Project] v
on e.ProjectID = v.ProjectID
where Story_Resolutiondate >= '2019-01-01'
group by v.ProjectId, year(Story_Resolutiondate), month(Story_Resolutiondate)