子查询中的T-sql子查询

时间:2015-10-26 11:09:04

标签: sql-server tsql subquery

我是SQL服务器的新手,我正在学习,如果答案显而易见,或者我只是以完全错误的方式接近这一点,我会很抱歉。

我正在从访问SQL服务器移动查询,有问题的查询在子查询中有子查询。

WITH CalculateSPBalance AS
( SELECT [BOOKINGS FILE].[Booking Number], ([Promoter Codes].[Promoter's Price] + [Promoter Codes].[Resform Deposit] * [Promoter Codes].[Resform Deposit deducted from sp] -
        [Venue Codes].[Event Price]) - [Venue Codes].[Event Deposit] * [BOOKINGS FILE].[Charity paid event deposit] AS [SP Balance]
   FROM [BOOKINGS FILE]
   INNER JOIN [Promoter Codes] ON [BOOKINGS FILE].[Promoter Code] = [Promoter Codes].[Promoter Code]
   INNER JOIN [Venue Codes]    ON [BOOKINGS FILE].[Venue Code] = [Venue Codes].[Venue Code]

)
, CalculateSPBeingHeld AS
( SELECT [BOOKINGS FILE].[Booking Number], SUM([Sponsorship Collected].Amount) AS [SP held]
   FROM [BOOKINGS FILE]
   LEFT OUTER JOIN [Sponsorship Collected] ON [BOOKINGS FILE].[Booking Number] = [Sponsorship Collected].[Booking Number]
   WHERE ([Sponsorship Collected].[Received by] = N'holding')
   GROUP BY [BOOKINGS FILE].[Booking Number]
)
SELECT [BOOKINGS FILE].[Booking Number],
CASE
    WHEN CalculateSPBeingHeld.[SP held] IS NULL
    THEN 0
    ELSE CalculateSPBeingHeld.[sp held] - CalculateSPBalance.[SP Balance]
END AS test
FROM [Bookings file]

INNER JOIN CalculateSPBalance   ON[BOOKINGS FILE].[Booking Number] = CalculateSPBalance.[Booking Number]
INNER JOIN CalculateSPBeingHeld ON[BOOKINGS FILE].[Booking Number] = CalculateSPBeingHeld.[Booking Number]

ORDER BY [booking number]

据我所知,在整个查询中定义并使用了两个子。

这很好用。

我如何将这些结果用作更大查询的一部分

WITH test2 AS
    (
        CalculateSPBalance AS
        ( SELECT [BOOKINGS FILE].[Booking Number], ([Promoter Codes].[Promoter's Price] + [Promoter Codes].[Resform Deposit] * [Promoter Codes].[Resform Deposit deducted from sp] -
                [Venue Codes].[Event Price]) - [Venue Codes].[Event Deposit] * [BOOKINGS FILE].[Charity paid event deposit] AS [SP Balance]
           FROM [BOOKINGS FILE]
           INNER JOIN [Promoter Codes] ON [BOOKINGS FILE].[Promoter Code] = [Promoter Codes].[Promoter Code]
           INNER JOIN [Venue Codes]    ON [BOOKINGS FILE].[Venue Code] = [Venue Codes].[Venue Code] )
        , CalculateSPBeingHeld AS
        ( SELECT [BOOKINGS FILE].[Booking Number]                                              , SUM([Sponsorship Collected].Amount) AS [SP held]
           FROM [BOOKINGS FILE]
           LEFT OUTER JOIN [Sponsorship Collected] ON [BOOKINGS FILE].[Booking Number] = [Sponsorship Collected].[Booking Number]
           WHERE ([Sponsorship Collected].[Received by] = N'holding')
           GROUP BY [BOOKINGS FILE].[Booking Number] 
        )


        SELECT [BOOKINGS FILE].[Booking Number],
                CASE
                    WHEN CalculateSPBeingHeld.[SP held] IS NULL
                    THEN 0
                    ELSE CalculateSPBeingHeld.[sp held] - CalculateSPBalance.[SP Balance]
                END AS test
        FROM [Bookings file]

        INNER JOIN CalculateSPBalance   ON[BOOKINGS FILE].[Booking Number] = CalculateSPBalance.[Booking Number]
        INNER JOIN CalculateSPBeingHeld ON[BOOKINGS FILE].[Booking Number] = CalculateSPBeingHeld.[Booking Number]

        ORDER BY [booking number]
    )

SELECT *
FROM test2

所以我想将这个由子查询组成的数据集用作更大查询中的子查询

先谢谢

1 个答案:

答案 0 :(得分:0)

您可以test2另一个CTE

WITH  CalculateSPBalance AS
        ( 
                SELECT [BOOKINGS FILE].[Booking Number], ([Promoter Codes].[Promoter's Price] + [Promoter Codes].[Resform Deposit] * [Promoter Codes].[Resform Deposit deducted from sp] -
                [Venue Codes].[Event Price]) - [Venue Codes].[Event Deposit] * [BOOKINGS FILE].[Charity paid event deposit] AS [SP Balance]
                FROM [BOOKINGS FILE]
                INNER JOIN [Promoter Codes] ON [BOOKINGS FILE].[Promoter Code] = [Promoter Codes].[Promoter Code]
                INNER JOIN [Venue Codes]    ON [BOOKINGS FILE].[Venue Code] = [Venue Codes].[Venue Code] )
        , CalculateSPBeingHeld AS
        ( 
                SELECT [BOOKINGS FILE].[Booking Number]                                              , SUM([Sponsorship Collected].Amount) AS [SP held]
                FROM [BOOKINGS FILE]
                LEFT OUTER JOIN [Sponsorship Collected] ON [BOOKINGS FILE].[Booking Number] = [Sponsorship Collected].[Booking Number]
                WHERE ([Sponsorship Collected].[Received by] = N'holding')
                GROUP BY [BOOKINGS FILE].[Booking Number] 
        )
        , test2 as (


                SELECT [BOOKINGS FILE].[Booking Number],
                        CASE
                            WHEN CalculateSPBeingHeld.[SP held] IS NULL
                            THEN 0
                            ELSE CalculateSPBeingHeld.[sp held] - CalculateSPBalance.[SP Balance]
                        END AS test
                FROM [Bookings file]

                INNER JOIN CalculateSPBalance   ON[BOOKINGS FILE].[Booking Number] = CalculateSPBalance.[Booking Number]
                INNER JOIN CalculateSPBeingHeld ON[BOOKINGS FILE].[Booking Number] = CalculateSPBeingHeld.[Booking Number]

    )

SELECT *
FROM test2                 
ORDER BY [booking number]

请注意,不支持CTE内的排序(少数例外),因此排序是在CTE定义之外完成的。