如何在sql中获取开始日期和结束日期?

时间:2022-09-26 11:44:02

I have a small table that looks like this:

我有一个小桌子,看起来像这样:

PLAN    YRMTH
A2BKG   197001
A2BKG   200205
A2BKG   200308
A2BKG   200806

From this table, how do I get a table such as the one below?

从这张表中,我如何获得如下表所示的表格?

   PLAN STARTDATE   ENDDATE
    A2BKG   197001      200205
    A2BKG   200205      200308
    A2BKG   200308      200806
    A2BKG   200806      NULL 

5 个解决方案

#1


2  

Try this

;with cte as 
(select *, ROW_NUMBER() over (partition by [plan] order by yrmth) rn from yourtable)
    select 
        t1.[plan],
        t1.YRMTH as startdate,
        t2.YRMTH as enddate
    from cte t1
        left join cte t2 on t1.[plan]=t2.[plan]
            and t1.rn=t2.rn-1

#2


1  

this is what i tried in oracle Same is available in sql-server 2012 as well ....my bad :(

这是我在oracle中尝试过的同样可以在sql-server 2012中使用....我的坏:(

select plan,yrmth,lead(yrmth) over (partition by lower(plan) order by rowid)  from tbl;

#3


0  

select t1.PLAN, t2.STARTDATE, 
 (select top 1 STARTDATE from table t2 where  t1.PLAN =t2.PLAN
                  and t1.STARTDATE<t2.STARTDATE) as ENDDATE
from table t1 

#4


0  

Something like this could help:

这样的事情可能会有所帮助:

SELECT  
[PLAN], 
YRMTH AS STARTDATE,
(SELECT MIN(YRMTH) FROM yourTable T 
WHERE T.[Plan] = yourTable.[Plan] AND T.YRMTH > Test1.YRMTH) AS  ENDDATE
FROM yourTable

#5


0  

Temp table solution from me.

来自我的临时表解决方案。

-- Create a temp table, containing an extra column, ID
DECLARE @orderedPlans 
(
   ID int,
   Plan varchar(64),
   YrMth int
)

-- Use contents of your table, plus ROW_NUMBER, to populate
-- temp table.  ID is sorted on Plan and YrMth.
--
INSERT INTO 
  @orderedPlans
(
   ID,
   Plan,
   YrMth
)
SELECT ROW_NUMBER() OVER (ORDER BY Plan, YrMth) AS ID,
  Plan,
  YrMth
FROM 
  YourTable

-- Now join your temp table on itself
-- Because of the ROW_NUMBER(), we can use ID - 1 as a join
-- Also join on Plan, so that the query can handle multiple plans being
-- in the table
SELECT 
    p1.Plan,
    p1.YrMth AS StartDate,
    p2.YrMth AS EndDate
FROM
  @orderedPlans p1
LEFT JOIN
  @orderedPlans p2
ON
    p1.Plan = p2.Plan
AND p1.ID = p2.ID - 1

#1


2  

Try this

;with cte as 
(select *, ROW_NUMBER() over (partition by [plan] order by yrmth) rn from yourtable)
    select 
        t1.[plan],
        t1.YRMTH as startdate,
        t2.YRMTH as enddate
    from cte t1
        left join cte t2 on t1.[plan]=t2.[plan]
            and t1.rn=t2.rn-1

#2


1  

this is what i tried in oracle Same is available in sql-server 2012 as well ....my bad :(

这是我在oracle中尝试过的同样可以在sql-server 2012中使用....我的坏:(

select plan,yrmth,lead(yrmth) over (partition by lower(plan) order by rowid)  from tbl;

#3


0  

select t1.PLAN, t2.STARTDATE, 
 (select top 1 STARTDATE from table t2 where  t1.PLAN =t2.PLAN
                  and t1.STARTDATE<t2.STARTDATE) as ENDDATE
from table t1 

#4


0  

Something like this could help:

这样的事情可能会有所帮助:

SELECT  
[PLAN], 
YRMTH AS STARTDATE,
(SELECT MIN(YRMTH) FROM yourTable T 
WHERE T.[Plan] = yourTable.[Plan] AND T.YRMTH > Test1.YRMTH) AS  ENDDATE
FROM yourTable

#5


0  

Temp table solution from me.

来自我的临时表解决方案。

-- Create a temp table, containing an extra column, ID
DECLARE @orderedPlans 
(
   ID int,
   Plan varchar(64),
   YrMth int
)

-- Use contents of your table, plus ROW_NUMBER, to populate
-- temp table.  ID is sorted on Plan and YrMth.
--
INSERT INTO 
  @orderedPlans
(
   ID,
   Plan,
   YrMth
)
SELECT ROW_NUMBER() OVER (ORDER BY Plan, YrMth) AS ID,
  Plan,
  YrMth
FROM 
  YourTable

-- Now join your temp table on itself
-- Because of the ROW_NUMBER(), we can use ID - 1 as a join
-- Also join on Plan, so that the query can handle multiple plans being
-- in the table
SELECT 
    p1.Plan,
    p1.YrMth AS StartDate,
    p2.YrMth AS EndDate
FROM
  @orderedPlans p1
LEFT JOIN
  @orderedPlans p2
ON
    p1.Plan = p2.Plan
AND p1.ID = p2.ID - 1