在 SQL 服务器中获取上一年的记录
Get prev year record in SQL Server
我需要获取前一年的记录以及当年的记录
输入
id year amount
----------------------
1001 2012 3747
1001 2012 3747
1002 2013 5746
1002 2013 5746
1003 2014 6756
1003 2014 6756
1004 2015 8746
期望输出:
id year amount prevAmount
----------------------------------
1001 2012 3747 null
1001 2012 3747 null
1002 2013 5746 3747
1002 2013 5746 3747
1003 2014 6756 5746
1003 2014 6756 5746
1004 2015 8746 6756
如果我没理解错的话,你可以用apply
:
select t.*, tprev.amount
from t outer apply
(select top (1) tprev.*
from t tprev
where tprev.year = t.year - 1
);
通常情况下,order by
和 select top
之间会有一个 order by
,但这似乎对您的数据没有影响。
如果您每年输入的重复次数相同,则可以使用 LAG 函数:-
使用以下db<>fiddle
Create Table:-
create table yourtable(id int, year numeric(10), amount numeric(10));
insert into yourtable
select 1001 , 2012 , 3747 union all
select 1001 , 2012 , 3747 union all
select 1002 , 2013 , 5746 union all
select 1002 , 2013 , 5746 union all
select 1003 , 2014 , 6756 union all
select 1003 , 2014 , 6756 union all
select 1004 , 2015 , 8746;
Fetch Data:-
select *,LAG(amount,2) OVER(order by year) as PrevAmount from yourtable ;
**有关更多信息,您可以use this link.
如果你有随机数每年的重复条目:-
Create Table:-
create table yourtable(id int, year numeric(10), amount numeric(10));
insert into yourtable
select 1001 , 2012 , 3747 union all
select 1001 , 2012 , 3747 union all
select 1001 , 2012 , 3747 union all
select 1002 , 2013 , 5746 union all
select 1002 , 2013 , 5746 union all
select 1003 , 2014 , 6756 union all
select 1003 , 2014 , 6756 union all
select 1004 , 2015 , 8746 union all
select 1004 , 2015 , 8746;
Fetch Data:-
with cte as (
select dense_rank() over(order by year) as rn1,* from yourtable
),cte1 as(
select distinct rn1+1 as rn2,* from cte
)
select cte.id,cte.year,cte.amount,cte1.amount as PrevAmount
from cte left join cte1 on cte.rn1=cte1.rn2
我需要获取前一年的记录以及当年的记录
输入
id year amount
----------------------
1001 2012 3747
1001 2012 3747
1002 2013 5746
1002 2013 5746
1003 2014 6756
1003 2014 6756
1004 2015 8746
期望输出:
id year amount prevAmount
----------------------------------
1001 2012 3747 null
1001 2012 3747 null
1002 2013 5746 3747
1002 2013 5746 3747
1003 2014 6756 5746
1003 2014 6756 5746
1004 2015 8746 6756
如果我没理解错的话,你可以用apply
:
select t.*, tprev.amount
from t outer apply
(select top (1) tprev.*
from t tprev
where tprev.year = t.year - 1
);
通常情况下,order by
和 select top
之间会有一个 order by
,但这似乎对您的数据没有影响。
如果您每年输入的重复次数相同,则可以使用 LAG 函数:-
使用以下db<>fiddle
Create Table:-
create table yourtable(id int, year numeric(10), amount numeric(10));
insert into yourtable
select 1001 , 2012 , 3747 union all
select 1001 , 2012 , 3747 union all
select 1002 , 2013 , 5746 union all
select 1002 , 2013 , 5746 union all
select 1003 , 2014 , 6756 union all
select 1003 , 2014 , 6756 union all
select 1004 , 2015 , 8746;
Fetch Data:-
select *,LAG(amount,2) OVER(order by year) as PrevAmount from yourtable ;
**有关更多信息,您可以use this link.
如果你有随机数每年的重复条目:-
Create Table:-
create table yourtable(id int, year numeric(10), amount numeric(10));
insert into yourtable
select 1001 , 2012 , 3747 union all
select 1001 , 2012 , 3747 union all
select 1001 , 2012 , 3747 union all
select 1002 , 2013 , 5746 union all
select 1002 , 2013 , 5746 union all
select 1003 , 2014 , 6756 union all
select 1003 , 2014 , 6756 union all
select 1004 , 2015 , 8746 union all
select 1004 , 2015 , 8746;
Fetch Data:-
with cte as (
select dense_rank() over(order by year) as rn1,* from yourtable
),cte1 as(
select distinct rn1+1 as rn2,* from cte
)
select cte.id,cte.year,cte.amount,cte1.amount as PrevAmount
from cte left join cte1 on cte.rn1=cte1.rn2