4

現在、私はこの記録を持っています

InvoiceList テーブル

InvoiceID   StoreCustomerID IssuedDate Amount      IsPenalty   EmployeeID
----------- --------------- ---------- ----------- ----------- -----------
1           13              2007-01-12 244         0           41
2           31              2007-04-05 81          0           34
3           23              2007-01-09 184         0           46
4           28              2007-11-21 231         0           17
5           36              2006-09-19 121         0           22
6           28              2006-10-24 240         0           17
7           15              2006-12-11 193         0           47
8           21              2007-01-15 172         0           4

InvoiceID自動インクリメントです。私がやりたいことはIssuedDate、前の行の日付をインクリメントして更新することです。こんな風に更新したい

InvoiceID   StoreCustomerID IssuedDate Amount      IsPenalty   EmployeeID
----------- --------------- ---------- ----------- ----------- -----------
1           13              2007-01-12 244         0           41
2           31              2007-01-13 81          0           34
3           23              2007-01-14 184         0           46
4           28              2007-01-15 231         0           17
5           36              2007-01-16 121         0           22
6           28              2007-01-17 240         0           17
7           15              2007-01-18 193         0           47
8           21              2007-01-19 172         0           4

現在、私はこの選択ステートメントを持っており、うまく機能しています。しかし、これを使用して更新するにはどうすればよいIssuedDateですか?

WITH SequenceDate AS
(
    SELECT  *, ROW_NUMBER() OVER (ORDER BY IssuedDate) RowNumber
    FROM    Invoice
)
SELECT RowNumber, DATEADD(d, RowNumber - 1, b.IssuedDate)
FROM SequenceDate
ORDER BY RowNumber

更新 1

教えていただいた内容が間違っていたので、初投稿で大変申し訳ありません。日付を昇順でのみ再配置できることを除いて、テーブル内のレコードを変更することは許可されていないため、日付をインクリメントしないでください。そうあるべきです。

InvoiceID   StoreCustomerID IssuedDate Amount      IsPenalty   EmployeeID
----------- --------------- ---------- ----------- ----------- -----------
1           13              2006-09-19 244         0           41
2           31              2006-10-24 81          0           34
3           23              2006-12-11 184         0           46
4           28              2007-01-09 231         0           17
5           36              2007-01-12 121         0           22
6           28              2007-01-15 240         0           17
7           15              2007-04-05 193         0           47
8           21              2007-11-21 172         0           4
4

6 に答える 6

7

シーケンスの最初の日付がわかっている場合は、単純に RowNumber を追加できます。

; WITH SequenceDate AS
(
    SELECT  *, 
            ROW_NUMBER() OVER (ORDER BY IssuedDate) RowNumber,
            MIN(IssuedDate) over () FirstDate
    FROM    Invoice
)
UPDATE SequenceDate
   SET IssuedDate = DATEADD(d, RowNumber - 1, FirstDate)

これが例のSql Fiddleです

更新

最初の質問の出力と正確に一致させるには:

; WITH SequenceDate AS
(
    SELECT  *, 
            ROW_NUMBER() OVER (ORDER BY InvoiceID) RowNumber
    FROM    Invoice
)
UPDATE SequenceDate
   SET IssuedDate = DATEADD(d, RowNumber - 1, 
                           (select IssuedDate
                              from Invoice
                             where InvoiceID = 1))

InvoiceID に従うように日付を並べ替えるには、次のようにします

; WITH SequenceDate AS
(
    SELECT  *, 
            ROW_NUMBER() OVER (ORDER BY InvoiceID) RowNumber,
            ROW_NUMBER() OVER (ORDER BY IssuedDate) DateNumber
    FROM    Invoice
)
UPDATE SequenceDate
   SET IssuedDate = d.IssuedDate
  from SequenceDate d
 where SequenceDate.RowNumber = d.DateNumber
于 2012-08-28T14:11:42.397 に答える
3

最初に、最小値ではなく最初に挿入された日付を使用したい場合は、次のようなことを試してください。

    WITH SequenceDate AS
    (
        SELECT  InvoiceID, ROW_NUMBER() OVER (ORDER BY IssuedDate) AS RowNumber
        FROM    Invoice
    )
    UPDATE InvoiceList 
    SET InvoiceList.IssuedDate = DATEADD(d, SequenceDate.RowNumber - 1, b.IssuedDate)   
    FROM SequenceDate 
    INNER JOIN InvoiceList ON SequenceDate.InvoiceID = InvoiceList.InvoiceID 
    CROSS JOIN (SELECT IssuedDate    
                FROM InvoiceList
WHERE InvoiceID = 1) b

SQL Fiddleが付属しています。

于 2012-08-28T14:15:10.760 に答える
2

InvoiceからテーブルにSequenceDate直接参加できます。

WITH SequenceDate AS
(
    SELECT  *, ROW_NUMBER() OVER (ORDER BY IssuedDate) RowNumber
    FROM    Invoice
)
UPDATE Invoice 
SET [IssuedDate] = DATEADD(d, RowNumber - 1, b.IssuedDate)
FROM Invoice a INNER JOIN [SequenceDate] b
        ON a.[InvoiceID] = b.[RowNumber]

そんなときは、これを試してみてください

WITH SequenceDate AS
(
    SELECT  *, ROW_NUMBER() OVER (ORDER BY IssuedDate) RowNumber
    FROM    Invoice
)
UPDATE Invoice 
SET [IssuedDate] = b.IssuedDate
FROM Invoice a INNER JOIN [SequenceDate] b
        ON a.[InvoiceID] = b.[RowNumber]

SQLFiddle デモ

于 2012-08-28T14:23:33.430 に答える
1

OPの更新された質問に基づく回答: http://sqlfiddle.com/#!3/dba13/22

with SeqInvoice as
(
  select *, row_number() over(order by InvoiceId) rn from invoice
)
,SeqDate as
(
  select *, row_number() over(order by IssuedDate) rn from invoice
)
update SeqInvoice set IssuedDate = sd.IssuedDate
from SeqDate sd
where sd.rn = SeqInvoice.rn;

select * from Invoice;

出力:

| INVOICEID | STORECUSTOMERID |                       ISSUEDDATE | AMOUNT | ISPENALTY | EMPLOYEEID |
----------------------------------------------------------------------------------------------------
|         1 |              13 | September, 19 2006 02:00:00-0700 |    244 |         0 |         41 |
|         2 |              31 |   October, 24 2006 02:00:00-0700 |     81 |         0 |         34 |
|         3 |              23 |  December, 11 2006 01:00:00-0800 |    184 |         0 |         46 |
|         4 |              28 |   January, 09 2007 01:00:00-0800 |    231 |         0 |         17 |
|         5 |              36 |   January, 12 2007 01:00:00-0800 |    121 |         0 |         22 |
|         6 |              28 |   January, 15 2007 01:00:00-0800 |    240 |         0 |         17 |
|         7 |              15 |     April, 05 2007 02:00:00-0700 |    193 |         0 |         47 |
|         8 |              21 |  November, 21 2007 01:00:00-0800 |    172 |         0 |          4 |

アップデート

CTEを直接更新せずに、ベーステーブルを直接更新する方法は次のとおりです。http://sqlfiddle.com/#!3/dba13/24

with SeqInvoice as
(
select *, row_number() over(order by InvoiceId) rn from invoice
)
,SeqDate as
(
select *, row_number() over(order by IssuedDate) rn from invoice
)
update I set IssuedDate = sd.IssuedDate
from Invoice i
join SeqInvoice si on si.InvoiceId = i.InvoiceId
join SeqDate sd on sd.rn = si.rn;

select * from Invoice;
于 2012-08-28T15:14:16.973 に答える
1

更新: 以下は古い回答です。OP の元の質問に基づいて回答が作成されました。

正しい出力を得るには、これが最短です: http://www.sqlfiddle.com/#!3/6aa22/1

SELECT * FROM INVOICE;

WITH FirstDate AS
(
    SELECT  row_number() over(order by InvoiceID) rn, IssuedDate
    FROM    Invoice
)
,UpdatedDate as
(
    select i.InvoiceID, i.IssuedDate, dateadd(d, row_number() over(order by i.InvoiceID) - 1, fd.IssuedDate) as NewDate
    from invoice i
    join FirstDate fd on fd.rn = 1
)
update UpdatedDate set IssuedDate = NewDate;

select * from Invoice;

出力:

| INVOICEID | STORECUSTOMERID |                     ISSUEDDATE | AMOUNT | ISPENALTY | EMPLOYEEID |
--------------------------------------------------------------------------------------------------
|         1 |              13 | January, 12 2007 08:00:00-0800 |    244 |         0 |         41 |
|         2 |              31 | January, 13 2007 08:00:00-0800 |     81 |         0 |         34 |
|         3 |              23 | January, 14 2007 08:00:00-0800 |    184 |         0 |         46 |
|         4 |              28 | January, 15 2007 08:00:00-0800 |    231 |         0 |         17 |
|         5 |              36 | January, 16 2007 08:00:00-0800 |    121 |         0 |         22 |
|         6 |              28 | January, 17 2007 08:00:00-0800 |    240 |         0 |         17 |
|         7 |              15 | January, 18 2007 08:00:00-0800 |    193 |         0 |         47 |
|         8 |              21 | January, 19 2007 08:00:00-0800 |    172 |         0 |          4 |
于 2012-08-28T15:04:31.970 に答える
0

このようなSQLカーソルを使用します...

DECLARE @InvoiceId AS INT
DECLARE @PreviousInvoiceId AS INT
DECLARE @NextIssuedDate AS DATE

SET @PreviousInvoiceId = 0
--Date you want to start from
SET @NextIssuedDate = '2007-01-12'

DECLARE csrUpdateDate CURSOR FOR 

    SELECT InvoiceID FROM Invoice
    ORDER BY InvoiceID

OPEN csrUpdateDate 

FETCH NEXT FROM csrUpdateDate 
INTO @InvoiceId

WHILE @@FETCH_STATUS = 0
BEGIN

    BEGIN 

        IF(@InvoiceId <> @PreviousInvoiceId)

        UPDATE Invoice
        SET IssuedDate = @NextIssuedDate
        WHERE InvoiceId = @InvoiceId

    END

     SET @PreviousInvoiceId = @InvoiceId
     SET @NextIssuedDate = DATEADD(DAY,1,@NextIssuedDate)

    FETCH NEXT FROM csrUpdateDate 
        INTO @InvoiceId

END
CLOSE csrUpdateDate 
DEALLOCATE csrUpdateDate 
于 2012-08-28T14:33:19.047 に答える