Sqlserver
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Sqlserver

Lặp qua từng giá trị đến số seq

Ý bạn là điều này?

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;

Hoặc cái này:

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. Sử dụng NEWSEQUENTIALID () để tạo HƯỚNG DẪN Tăng dần trong SQL Server

  2. Chèn hàng loạt tệp CSV được trích dẫn một phần trong SQL Server

  3. Chuyển đổi Kiểu dữ liệu thời gian thành Định dạng AM PM:

  4. Cố gắng lưu trữ nội dung XML vào SQL Server 2005 không thành công (sự cố mã hóa)

  5. Cách kết hợp kết quả của hai truy vấn thành một tập dữ liệu duy nhất