2

これは適切ではない私のクエリです:

;With normal As
    (   
        Select  ROW_NUMBER() Over (Order by RecordingDateTime) as RowNum, 
                DocumentID, 
                CreatedByAccountID, 
                JurisdictionID,
                InstrumentID
            --  Cast(InstrumentID as decimal(18)) as InstrumentID
            From Documents  Where RecordingDateTime IS NOT NULL
    )
    Select Top 1 @PreviousInstrumentID = InstrumentID,
                @PreviousDocumentID = DocumentID,
                @PreviousCreatedByAccountID = CreatedByAccountID,
                @PreviousBelongsToJurisdictionID = JurisdictionID
            From normal
                Where RowNum = @RowNum - 1

    Select Top 1 @NextInstrumentID = InstrumentID,
                @NextDocumentID = DocumentID,
                @NextCreatedByAccountID = CreatedByAccountID,
                @NextBelongsToJurisdictionID = JurisdictionID
            From normal
                Where RowNum = @RowNum + 1

両方のselectステートメントをCTEのコンテキストで使用する必要があります。どうすればそれを達成できますか?

4

3 に答える 3

3

他に2つのCTEを作成し、それらをselectステートメントで使用できます。

;With normal As
    (   
        Select  ROW_NUMBER() Over (Order by RecordingDateTime) as RowNum, 
                DocumentID, 
                CreatedByAccountID, 
                JurisdictionID,
                InstrumentID
            --  Cast(InstrumentID as decimal(18)) as InstrumentID
            From Documents  Where RecordingDateTime IS NOT NULL
    )
, PrevRow AS (
SELECT InstrumentID, DocumentID, CreatedByAccountID, JurisdictionID
FROM normal
WHERE RowNum = @RowNum - 1
), NextRow AS (
SELECT InstrumentID, DocumentID, CreatedByAccountID, JurisdictionID
FROM normal
WHERE RowNum = @RowNum + 1
)
Select Top 1    @PreviousInstrumentID = PrevRow.InstrumentID,
                @PreviousDocumentID = PrevRow.DocumentID,
                @PreviousCreatedByAccountID = PrevRow.CreatedByAccountID,
                @PreviousBelongsToJurisdictionID = PrevRow.JurisdictionID,
                @NextInstrumentID = NextRow.InstrumentID,
                @NextDocumentID = NextRow.DocumentID,
                @NextCreatedByAccountID = NextRow.CreatedByAccountID,
                @NextBelongsToJurisdictionID = NextRow.JurisdictionID

FROM PrevRow 
FULL OUTER JOIN NextRow ON 1=1
于 2013-02-22T06:29:30.623 に答える
2

1つのステートメントで両方の行を選択し、条件付き集計を使用して対応する値を取得できます。

With normal As
    (   
        Select  ROW_NUMBER() Over (Order by RecordingDateTime) as RowNum, 
                DocumentID, 
                CreatedByAccountID, 
                JurisdictionID,
                InstrumentID
            --  Cast(InstrumentID as decimal(18)) as InstrumentID
            From Documents  Where RecordingDateTime IS NOT NULL
    )
SELECT
  @PreviousInstrumentID            = MAX(CASE RowNum WHEN @RowNum - 1 THEN InstrumentID       END),
  @PreviousDocumentID              = MAX(CASE RowNum WHEN @RowNum - 1 THEN DocumentID         END),
  @PreviousCreatedByAccountID      = MAX(CASE RowNum WHEN @RowNum - 1 THEN CreatedByAccountID END),
  @PreviousBelongsToJurisdictionID = MAX(CASE RowNum WHEN @RowNum - 1 THEN JurisdictionID     END),
  @NextInstrumentID                = MAX(CASE RowNum WHEN @RowNum + 1 THEN InstrumentID       END),
  @NextDocumentID                  = MAX(CASE RowNum WHEN @RowNum + 1 THEN DocumentID         END),
  @NextCreatedByAccountID          = MAX(CASE RowNum WHEN @RowNum + 1 THEN CreatedByAccountID END),
  @NextBelongsToJurisdictionID     = MAX(CASE RowNum WHEN @RowNum + 1 THEN JurisdictionID     END)
FROM normal
WHERE RowNum IN (@RowNum - 1, @RowNum + 1)
;
于 2013-02-22T06:38:37.790 に答える
0

データリーダーでこのクエリの出力を何らかの方法で使用しているためにこれらの変数の割り当てが必要ない場合は、次のコマンドを使用して1つのCTEのみを実行できます。

RowNum = @Row+1またはRowNum=@Row-1

これにより、2行の結果が得られ、消費するコードで好きなように消費できます。

于 2013-02-22T06:24:39.397 に答える