我可以将“with”查询转换为创建过程吗?

时间:2015-04-14 19:07:16

标签: sql-server

    WITH PinCounts AS

    (
        SELECT GameID, TB.bowlerID, TB.FirstName, Frame, bowlingBall1, bowlingBall2, bowlingBall3

        ,framestatus  = CASE
                            WHEN bowlingBall1 = 10 THEN 2
                            WHEN bowlingBall1 + bowlingBall2 = 10 THEN 1
                        ELSE 0
                        END

        ,next1  = LEAD(bowlingBall1, 1
                      ,CASE
                           WHEN Frame = 10 AND bowlingBall1 = 10
                                THEN ISNULL(NULLIF(bowlingBall2, 10), bowlingBall3)
                           WHEN Frame = 10 AND bowlingBall1 + bowlingBall2 = 10
                                THEN bowlingBall3
                           END
                       ) OVER
                        (

                            PARTITION BY GameID, TB.bowlerID
                            ORDER BY Frame
                         )

        ,next2  = LEAD(bowlingBall2, 1
                  ,bowlingBall3
                         ) OVER
                         (
                          PARTITION BY GameID, TB.bowlerID
                          ORDER BY Frame

                        )

        ,next3  = LEAD(bowlingBall1, 2) OVER
                         (
                         PARTITION BY GameID, TB.bowlerID
                         ORDER BY Frame
                         )

                   FROM TBowlinGScores AS BS
                         LEFT OUTER JOIN TBowlers AS TB
                         ON(BS.bowlerID = TB.bowlerID)

                         ),

    TotalScore AS
    (
      SELECT GameID, TB.bowlerID, TB.FirstName, Frame, bowlingBall1, bowlingBall2, bowlingBall3
             ,framestatus, next1, next2, next3
             ,FramePins = CASE
                              WHEN bowlingBall1 = 10
                                   THEN bowlingBall1 + next1 + ISNULL(next2, next3)
                              WHEN bowlingBall1 + bowlingBall2 = 10
                                   THEN bowlingBall1 + bowlingBall2 + next1
                              ELSE bowlingBall1 + bowlingBall2
                              END

                    FROM PinCounts AS PC
                         LEFT OUTER JOIN TBowlers AS TB
                         ON(PC.bowlerID = TB.bowlerID) 
                         ),



     SetScores AS
   (
   SELECT GameID, bowlerID, Frame, bowlingBall1, bowlingBall2, bowlingBall3
          ,TotalScore = CASE
                            WHEN (framestatus = 0 AND bowlingBall2 IS NOT NULL) 
                                  OR
                                 (framestatus = 1 AND next1 IS NOT NULL) 
                                  OR
                                 (framestatus = 2 AND ISNULL(next2, next3) IS NOT NULL)
                            THEN SUM(FramePins) OVER

                                 (
                                   PARTITION BY GameID, bowlerID
                                   ORDER BY Frame
                                   ROWS UNBOUNDED PRECEDING
                                  )
                            END

                 FROM TotalScore
                 )

SELECT TB.bowlerID, FirstName, LastName, TotalScore

FROM SetScores  AS SS
      LEFT OUTER JOIN TBowlers AS TB
        ON(SS.bowlerID = TB.bowlerID)

WHERE Frame = 10

GROUP BY TB.bowlerID
         ,FirstName
         ,LastName
         ,TotalScore

1 个答案:

答案 0 :(得分:1)

下面是带有“with”的代码sp,而@pframe是带有where where

的参数
create proc SPwith 
@pFrame int = 10
as
begin


 WITH PinCounts AS

    (
        SELECT GameID, TB.bowlerID, TB.FirstName, Frame, bowlingBall1, bowlingBall2, bowlingBall3

        ,framestatus  = CASE
                            WHEN bowlingBall1 = 10 THEN 2
                            WHEN bowlingBall1 + bowlingBall2 = 10 THEN 1
                        ELSE 0
                        END

        ,next1  = LEAD(bowlingBall1, 1
                      ,CASE
                           WHEN Frame = 10 AND bowlingBall1 = 10
                                THEN ISNULL(NULLIF(bowlingBall2, 10), bowlingBall3)
                           WHEN Frame = 10 AND bowlingBall1 + bowlingBall2 = 10
                                THEN bowlingBall3
                           END
                       ) OVER
                        (

                            PARTITION BY GameID, TB.bowlerID
                            ORDER BY Frame
                         )

        ,next2  = LEAD(bowlingBall2, 1
                  ,bowlingBall3
                         ) OVER
                         (
                          PARTITION BY GameID, TB.bowlerID
                          ORDER BY Frame

                        )

        ,next3  = LEAD(bowlingBall1, 2) OVER
                         (
                         PARTITION BY GameID, TB.bowlerID
                         ORDER BY Frame
                         )

                   FROM TBowlinGScores AS BS
                         LEFT OUTER JOIN TBowlers AS TB
                         ON(BS.bowlerID = TB.bowlerID)

                         ),

    TotalScore AS
    (
      SELECT GameID, TB.bowlerID, TB.FirstName, Frame, bowlingBall1, bowlingBall2, bowlingBall3
             ,framestatus, next1, next2, next3
             ,FramePins = CASE
                              WHEN bowlingBall1 = 10
                                   THEN bowlingBall1 + next1 + ISNULL(next2, next3)
                              WHEN bowlingBall1 + bowlingBall2 = 10
                                   THEN bowlingBall1 + bowlingBall2 + next1
                              ELSE bowlingBall1 + bowlingBall2
                              END

                    FROM PinCounts AS PC
                         LEFT OUTER JOIN TBowlers AS TB
                         ON(PC.bowlerID = TB.bowlerID) 
                         ),



     SetScores AS
   (
   SELECT GameID, bowlerID, Frame, bowlingBall1, bowlingBall2, bowlingBall3
          ,TotalScore = CASE
                            WHEN (framestatus = 0 AND bowlingBall2 IS NOT NULL) 
                                  OR
                                 (framestatus = 1 AND next1 IS NOT NULL) 
                                  OR
                                 (framestatus = 2 AND ISNULL(next2, next3) IS NOT NULL)
                            THEN SUM(FramePins) OVER

                                 (
                                   PARTITION BY GameID, bowlerID
                                   ORDER BY Frame
                                   ROWS UNBOUNDED PRECEDING
                                  )
                            END

                 FROM TotalScore
                 )

SELECT TB.bowlerID, FirstName, LastName, TotalScore

FROM SetScores  AS SS
      LEFT OUTER JOIN TBowlers AS TB
        ON(SS.bowlerID = TB.bowlerID)

WHERE Frame = @pFrame

GROUP BY TB.bowlerID
         ,FirstName
         ,LastName
         ,TotalScore

end