2017-08-29 28 views
0

我簡化了我的SQL查詢,如下所示。 我有一個EmployeeTran表和以下記錄。帶SUM有效日期和組的SQL查詢

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); 

在上表中,我們每天有3名員工在1月1日至1月15日之間的交易金額。 如果我們想從開始到特定選擇日期的交易總和, 我們可以使用下面的查詢來獲得相同的

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

上面的查詢會給總和從1月1日至1月4日爲每個員工如下 [圖像被添加爲鏈接,因爲缺乏足夠的信譽點] [https://i.stack.imgur.com/lLBIg.jpg]

現在,我們要在上面添加一個額外的列select Query as 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<[email protected] 
group by et.EMPID 

[圖像添加作爲鏈接] [https://i.stack.imgur.com/kI7dl.jpg]

請輔助上面查詢。

+0

請嘗試編輯您以包括樣本表數據以及格式化文本,而不是圖片發表。 –

回答

0

如果需要,您可以使用類似以總和計算爲相應的日期和子查詢中使用的結果如下:

SELECT EMPID, 
     Effectivedate, 
     Amount, 
     SUM(Amount) OVER (PARTITION BY EMPID ORDER BY Effectivedate) AS AggAmount 
    FROM @EmployeeTran 
1

@Mihir阿明,你婉實現所謂的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 
0

試試這個query.it會給你更好的性能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