带有有效日期的SUM聚合的SQL查询和

时间:2017-08-29 06:07:09

标签: sql-server date group-by sum

我以下面的示例方式简化了我的SQL查询。 我有一个带有以下记录的EmployeeTran表。

CREATE TABLE EmployeeTran (
    EMPID int NOT NULL,
    Effectivedate datetime,
    Amount INT   
);

insert into EmployeeTran values(101,'2017-01-01',300);
insert into EmployeeTran values(101,'2017-01-02',200);
insert into EmployeeTran values(101,'2017-01-03',200);
insert into EmployeeTran values(101,'2017-01-04',100);
insert into EmployeeTran values(101,'2017-01-05',900);
insert into EmployeeTran values(101,'2017-01-06',600);
insert into EmployeeTran values(101,'2017-01-07',700);
insert into EmployeeTran values(101,'2017-01-08',100);
insert into EmployeeTran values(101,'2017-01-09',1100);
insert into EmployeeTran values(101,'2017-01-10',2200);
insert into EmployeeTran values(101,'2017-01-11',400);
insert into EmployeeTran values(101,'2017-01-12',600);
insert into EmployeeTran values(101,'2017-01-13',500);
insert into EmployeeTran values(101,'2017-01-14',300);
insert into EmployeeTran values(101,'2017-01-15',100);
insert into EmployeeTran values(102,'2017-01-01',300);
insert into EmployeeTran values(102,'2017-01-02',300);
insert into EmployeeTran values(102,'2017-01-03',700);
insert into EmployeeTran values(102,'2017-01-04',200);
insert into EmployeeTran values(102,'2017-01-05',200);
insert into EmployeeTran values(102,'2017-01-06',2800);
insert into EmployeeTran values(102,'2017-01-07',700);
insert into EmployeeTran values(102,'2017-01-08',900);
insert into EmployeeTran values(102,'2017-01-09',1100);
insert into EmployeeTran values(102,'2017-01-10',2200);
insert into EmployeeTran values(102,'2017-01-11',1100);
insert into EmployeeTran values(102,'2017-01-12',600);
insert into EmployeeTran values(102,'2017-01-13',100);
insert into EmployeeTran values(102,'2017-01-14',300);
insert into EmployeeTran values(102,'2017-01-15',900);
insert into EmployeeTran values(103,'2017-01-01',900);
insert into EmployeeTran values(103,'2017-01-02',200);
insert into EmployeeTran values(103,'2017-01-03',100);
insert into EmployeeTran values(103,'2017-01-04',800);
insert into EmployeeTran values(103,'2017-01-05',1100);
insert into EmployeeTran values(103,'2017-01-06',600);
insert into EmployeeTran values(103,'2017-01-07',500);
insert into EmployeeTran values(103,'2017-01-08',400);
insert into EmployeeTran values(103,'2017-01-09',100);
insert into EmployeeTran values(103,'2017-01-10',1400);
insert into EmployeeTran values(103,'2017-01-11',400);
insert into EmployeeTran values(103,'2017-01-12',600);
insert into EmployeeTran values(103,'2017-01-13',700);
insert into EmployeeTran values(103,'2017-01-14',1000);
insert into EmployeeTran values(103,'2017-01-15',1800);

在上表中,我们每天从1月1日到1月15日有3名员工交易金额。 如果我们希望交易金额从特定的选择日期开始, 我们可以使用下面的Query来获得相同的

Declare @selectiondate Date
select  @selectiondate='2017-01-04'
select et.EMPID,Sum(Amount) AS SUM from EmployeeTran et
where et.effectivedate<=@selectiondate
group by et.EMPID

以上查询将为每位员工提供1月1日至1月4日的金额,如下所示 [图片被添加为链接,因为缺乏足够的声誉点] [https://i.stack.imgur.com/lLBIg.jpg]

现在,我们要在上面添加一个额外的列,选择Query作为Effectivedtae。 我们需要使用@selectiondate和@enddate传递一系列日期。 查询应该从开始时给出每个日期的总和。 即如果我们从1月4日到1月9日通过日期范围,那么它应该给出每个日期的总和,如下所示。

我们需要在Query下面进行修改。

Declare @selectiondate Date
Declare @enddate Date
select  @selectiondate='2017-01-04'
select  @enddate='2017-01-09'

select et.EMPID,Sum(Amount) AS SUM from EmployeeTran et
where et.effectivedate<=@selectiondate
group by et.EMPID

[图片已添加为链接] [https://i.stack.imgur.com/kI7dl.jpg]

请协助上述查询。

3 个答案:

答案 0 :(得分:1)

@Mihir Amin,你想要达到的目标是Running Total

要完全解决这个问题,我们必须延长@ Tyron78的答案:

DECLARE @selectionDate DATE = '2017-01-04',
        @endDate DATE = '2017-01-09';

;WITH cte AS (
    SELECT  
        EMPID,
        Effectivedate,
        Amount,
        SUM(Amount) OVER (PARTITION BY EMPID ORDER BY Effectivedate) AS AggAmount
    FROM 
        EmployeeTran
)
SELECT * FROM cte
WHERE Effectivedate BETWEEN @selectionDate AND @endDate

答案 1 :(得分:0)

您可以使用以下内容来评估相应日期的总和,并在需要时在子查询中使用结果:

exit(EXIT_FAILURE)

答案 2 :(得分:0)

尝试这个查询。它会给你更好的性能beacuse.Here我首先在两个特定日期之间获取数据,然后根据empid进行分组,而不是获取所有不需要的数据,并根据具体日期的empid进行分组。

DECLARE @selectionDate DATE = '2017-01-04',
        @endDate DATE = '2017-01-09';

select EMPID,Effectivedate,Amount,SUM(Amount) OVER (PARTITION BY EMPID ORDER BY Effectivedate) AS AggAmount from (
SELECT  EMPID,Effectivedate,Amount
FROM EmployeeTran
where Effectivedate BETWEEN @selectionDate AND @endDate
) as a