使用SQL

时间:2018-03-26 03:32:18

标签: sql db2

我有一个数据集:

Date

June 2011 
July 2011
Aug 2011
Sep 2011
Oct 2011
Jan 2012
Feb 2012
Mar 2012
Apr 2013
May 2013

记录每个年度项目的注册日期。 (但是没有项目ID)

我想添加额外的变量Percentage,它代表该月的平均进度。 (例如,如果项目注册4个月,那么每个月将逐步增加25%,(25,50,75,100)),具体来说:

Percentage     Date
20             June 2011
40             July 2011
60             Aug 2011
80             Sep 2011
100            Oct 2011
33             Jan 2012
66             Feb 2012
100            Mar 2012
50             Apr 2013
100            May 2013

但是,我的主要问题是我无法知道每个项目每年的开始月份(期间)和结束月份(期间)。

在这种情况下,SQL中是否有任何函数可以创建计算的百分比?我想创建一个年变量,并进一步使用指标来指示进度的开始/结束,但无法继续前进。

再次感谢你!

2 个答案:

答案 0 :(得分:1)

这是一个简单的Pseudo sql来获得你想要的东西。 ?

select Year(date), Month(date)  ,  
 (select sum(Progress_percentage) from dataset b  where  b.date <=a.date ) as 
subquery_percentage
from dataset a
group by Year(date), Month(date) 

答案 1 :(得分:1)

您可以尝试此查询。

通过year(dates)获取SELECT (FLOOR(CAST(T2.RK AS decimal) * 100/( SELECT COUNT(1) AS Totle FROM T WHERE year(dates) = T2.dates))) [Percentage], T2.dates as [years], T2.months as [months] FROM ( SELECT ROW_NUMBER() OVER(PARTITION BY year(dates) ORDER BY dates DESC) AS RK, year(dates) as dates, month(dates) as months FROM T ) AS T2 GROUP BY T2.dates,T2.RK,T2.months 子查询。

然后获得百分比。

public fetchUserPrjects(action$: Observable<IProjectAction>, store: Store<IAppState>) {
    return action$.pipe(
      ofType(ProjectActionType.FETCH_USER_PROJECTS),
      mergeMap((action) => {
        store.dispatch(this._commonAction.showLoading()); // <== call action before request
         return this._projectService.getProjectList(action.payload)
          .map((result) => {
               return {
              project: result
            };
          });
      }),
      flatMap(data => [
        this._projectAction.addUserProjects(data),
        this._commonAction.hideLoading()
      ])
    ).catch((error) => {
      return Observable.of(this._commonAction.showError(error));
    })
    .concat(Observable.of(this._commonAction.hideLoading()));
  }