sql >> Databáze >  >> RDS >> Sqlserver

Procházejte každou hodnotu do seq num

Myslíš tohle?

with myData as
(
select ID,
row_Number() over (partition by Id order by id, StartDate) as SeqNum,
min(startdate) over (partition by Id) as minDate,
startDate, endDate
from myTable
)
select id, seqNum, startDate, endDate, dateadd(day, seqNum*29, minDate) as newDate
from myData;

Nebo toto:

with myData as
(
select ID,
row_Number() over (partition by Id order by id, StartDate) as SeqNum,
min(startdate) over (partition by Id) as minDate, 
max(endDate) over (partition by Id)as maxDate,
startDate, endDate
from myTable
)
select id, seqNum, startDate, endDate, 
 case 
 when maxDate < dateadd(day, seqNum*29, minDate)
 then maxDate 
 else dateadd(day, seqNum*29, minDate) end as newDate
from myData;



  1. Jak zastavit běžící dotaz mysql

  2. Jak zacházet s výpisem SQL pomocí Pythonu

  3. Je možné více cizích klíčů v jednom poli?

  4. Získejte všechny zaměstnance pod manažerem s CTE