如何在CTE中编写2个select语句?

时间:2013-02-22 05:49:18

标签: sql sql-server-2008 tsql common-table-expression

这是我的查询,这是不恰当的:

;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的上下文中。我是如何实现这一目标的?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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)
;

答案 2 :(得分:0)

如果您不需要这些变量赋值,因为您在某种方式中在datareader中使用此查询的输出,则只能运行一个CTE:

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

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