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

您可以创建另外两个 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

您可以在单个语句中选择两行并使用条件聚合获取相应的值:

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

如果您不需要这些变量分配,因为您在数据读取器中以某种方式使用此查询的输出,您可以只运行一个 CTE:

RowNum = @Row + 1 或 RowNum = @Row - 1

这将为您提供两行的结果,您可以在使用代码中随意使用它。

于 2013-02-22T06:24:39.397 回答