其他ID的总和,基于SQL CTE中的日期

问题描述 投票:1回答:1

我的示例CTE来自多个来源,并从以下位置提取数据:

with sample_cte as (ae.spvr as col1,
ae.startDate as start_dt,
ae.END_DT as end_dt,
ROUND(ae.Scre,2,1) as Scre ,
from diff_sources ae

我有一个CTE,可以为我产生如下输出:

col1 | start_dt | end_dt | score
a    | 10-01-19 |10-02-19| 50.50
a    | 10-02-19 |10-03-19| 55.50
b    | 10-01-19 |10-02-19| 60.50
b    | 10-02-19 |10-03-19| 65.50
c    | 10-01-19 |10-02-19| 70.50
c    | 10-02-19 |10-03-19| 75.50

我正在寻找添加新列以获取分数之和基于日期[a的分数b和c的总和],类似地,c的a和b的总和)它的外观如下所示

col1 | start_dt | end_dt | new_sum_score_column
a    | 10-01-19 |10-02-19| sum of b and c on these dates = (60.50+70.50)
a    | 10-02-19 |10-03-19| sum of b and c on these dates = (65.50+75.50)
b    | 10-01-19 |10-02-19| sum of a and c on these dates = (50.50+70.50)
b    | 10-02-19 |10-03-19| sum of a and c on these dates = (55.50+75.50)
c    | 10-01-19 |10-02-19| similar logic
c    | 10-02-19 |10-03-19| 

我的示例CTE来自多个来源,并从以下位置提取数据:sample_cte为(ae.spvr为col1,ae.startDate为start_dt,ae.END_DT为end_dt,ROUND(ae.Scre,2,1)作为Scre ,来自...

sql sql-server
1个回答
2
投票

假定日期引用为第一列,我认为这可行:

© www.soinside.com 2019 - 2024. All rights reserved.