比较和减去记录 2 Table
Compare and subtract the records 2 Table
i have 2 tables (每两个tables有相同的结构)
我用下面的代码得到两个 table 的总和。
现在我想找到上个月的记录总和和当月的总和,并比较它们。
如果上月的总和大于当月的总和则记录为selected.If 不行,则不选
Table 1 Table 2<br>
StudentId=1 Score=50 Date =2017/01/01 StudentId= 1 Score=100 Date =2017/01/01<br>
StudentId=1 Score=20 Date =2017/02/01 StudentId= 1 Score=10 Date =2017/02/01<br>
StudentId=2 Score=60 Date =2017/01/01 StudentId= 2 Score=100 Date =2017/01/01<br>
StudentId=2 Score=540 Date =2017/02/01 StudentId= 2 Score=100 Date =2017/02/01<br>
当前结果:
StudentId HighScoreUser<br>
1 180<br>
2 800<br>
---------------------------------<br>
我想要的结果:
StudentId Prev Month(2017/01/01) Current Month(2017/02/01)<br>
1 150 30<br>
2 160 640<br>
1 --> 150 > 30 --> 是吗? --> 是的,所以一定要选
2 --> 160 > 640 --> 是吗? --> 不行,所以一定不要选
结果(选定值)= StudentId,(上月总和 - 本月总和)
<br>`CREATE PROCEDURE SelectTopMonth
@Date1 NVARCHAR(12),
@Date2 NVARCHAR(12)
AS
SELECT StudentId, ISNULL(SUM(Score),0) As HighScoreUser
FROM (SELECT StudentId, Score FROM tbl_ActPoint WHERE Date >= @Date1 AND Date <= @Date2
UNION ALL
SELECT StudentId, Score FROM tbl_EvaPoint WHERE Date >= @Date1 AND Date <= @Date2
) as T
GROUP BY StudentId ORDER BY HighScoreUser DESC
RETURN 0`
我认为您希望看到如下数据:
Select StudentId, [2017/01/01] as [Prev Month(2017/01/01)],
[2017/02/01] as [Current Month(2017/02/01)] from (
Select * from t1
Union all
Select * from t2
) a
pivot (sum([Score]) for [Date] in ([2017/01/01],[2017/02/01]) ) p
Where [2017/01/01] > [2017/02/01]
您可以使用 SQL 服务器的领先分析功能来获得您的结果。
表 1
create table table1
(
studentid integer,
score integer,
date date
);
insert into table1 values(1,50,'2017/01/01');
insert into table1 values(1,20,'2017/02/01');
insert into table1 values(2,60,'2017/01/01');
insert into table1 values(2,540,'2017/02/01');
表 2
create table table2
(
studentid integer,
score integer,
date date
);
insert into table2 values(1,100,'2017/01/01');
insert into table2 values(1,10,'2017/02/01');
insert into table2 values(2,100,'2017/01/01');
insert into table2 values(2,100,'2017/02/01');
查询:
with data as (
select table1.studentid , (table1.score + table2.score) as pre_score, table1.date,
lead((table1.score + table2.score),1) over(partition by table1.studentid order by table1.studentid,table1.date)
as cur_score
from table1 join table2
on table1.studentid = table2.studentid
and table1.date = table2.date
)
select * from data
where cur_score is not null
and pre_score > cur_score
根据您的评论添加了此条件 "and pre_score > cur_score"。
i have 2 tables (每两个tables有相同的结构)
我用下面的代码得到两个 table 的总和。
现在我想找到上个月的记录总和和当月的总和,并比较它们。
如果上月的总和大于当月的总和则记录为selected.If 不行,则不选
Table 1 Table 2<br>
StudentId=1 Score=50 Date =2017/01/01 StudentId= 1 Score=100 Date =2017/01/01<br>
StudentId=1 Score=20 Date =2017/02/01 StudentId= 1 Score=10 Date =2017/02/01<br>
StudentId=2 Score=60 Date =2017/01/01 StudentId= 2 Score=100 Date =2017/01/01<br>
StudentId=2 Score=540 Date =2017/02/01 StudentId= 2 Score=100 Date =2017/02/01<br>
当前结果:
StudentId HighScoreUser<br>
1 180<br>
2 800<br>
---------------------------------<br>
我想要的结果:
StudentId Prev Month(2017/01/01) Current Month(2017/02/01)<br>
1 150 30<br>
2 160 640<br>
1 --> 150 > 30 --> 是吗? --> 是的,所以一定要选
2 --> 160 > 640 --> 是吗? --> 不行,所以一定不要选
结果(选定值)= StudentId,(上月总和 - 本月总和)
<br>`CREATE PROCEDURE SelectTopMonth
@Date1 NVARCHAR(12),
@Date2 NVARCHAR(12)
AS
SELECT StudentId, ISNULL(SUM(Score),0) As HighScoreUser
FROM (SELECT StudentId, Score FROM tbl_ActPoint WHERE Date >= @Date1 AND Date <= @Date2
UNION ALL
SELECT StudentId, Score FROM tbl_EvaPoint WHERE Date >= @Date1 AND Date <= @Date2
) as T
GROUP BY StudentId ORDER BY HighScoreUser DESC
RETURN 0`
我认为您希望看到如下数据:
Select StudentId, [2017/01/01] as [Prev Month(2017/01/01)],
[2017/02/01] as [Current Month(2017/02/01)] from (
Select * from t1
Union all
Select * from t2
) a
pivot (sum([Score]) for [Date] in ([2017/01/01],[2017/02/01]) ) p
Where [2017/01/01] > [2017/02/01]
您可以使用 SQL 服务器的领先分析功能来获得您的结果。
表 1
create table table1
(
studentid integer,
score integer,
date date
);
insert into table1 values(1,50,'2017/01/01');
insert into table1 values(1,20,'2017/02/01');
insert into table1 values(2,60,'2017/01/01');
insert into table1 values(2,540,'2017/02/01');
表 2
create table table2
(
studentid integer,
score integer,
date date
);
insert into table2 values(1,100,'2017/01/01');
insert into table2 values(1,10,'2017/02/01');
insert into table2 values(2,100,'2017/01/01');
insert into table2 values(2,100,'2017/02/01');
查询:
with data as (
select table1.studentid , (table1.score + table2.score) as pre_score, table1.date,
lead((table1.score + table2.score),1) over(partition by table1.studentid order by table1.studentid,table1.date)
as cur_score
from table1 join table2
on table1.studentid = table2.studentid
and table1.date = table2.date
)
select * from data
where cur_score is not null
and pre_score > cur_score
根据您的评论添加了此条件 "and pre_score > cur_score"。