我簡化了我的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]
請輔助上面查詢。
請嘗試編輯您以包括樣本表數據以及格式化文本,而不是圖片發表。 –