Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Ulangi setiap nilai ke nomor seq

Maksudmu ini?

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;

Atau ini:

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. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. SQL Server 2005 dan cakupan tabel sementara

  2. T-SQL untuk menemukan Indeks yang Berlebihan

  3. Menggunakan CROSS APPLY untuk lebih dari satu kolom

  4. Bagaimana menemukan semua anak dari kolom tabel di tabel server sql?

  5. Cara Menemukan Pengidentifikasi Unik yang Optimal dalam Tabel di SQL Server:sp_special_columns