重写tsql select语句而不进行子子选择

时间:2011-08-30 13:34:54

标签: tsql

我想在没有子选择的情况下重写以下select语句;虽然它现在正常工作但它只有才有效,如果有数据,当其中一个子选择没有返回数据时我得到一个错误

  

每个GROUP BY表达式必须至少包含一个不是外部引用的列。

这是选择:

SELECT  COUNT(DISTINCT ( PROMSID )) AS Volume ,
            ( SELECT    CAST(CAST(COUNT(DISTINCT ( PROMSID )) AS DECIMAL(8, 2))
                        / ( SELECT  COUNT(DISTINCT RES.Branch)
                            FROM    tblPROMsExportSummary AS PES
                                    INNER JOIN TVF_GetRelevantScorecards(@ProcedureID, 82, @StartDate, @EndDate) AS RES ON RES.PROMSID = PES.PROMSID
                                    INNER JOIN tblUploadedScorecards AS UPL ON PES.PROMSID = UPL.PEMSID
                            WHERE   RES.[Month] = TVF.MonthValue
                            GROUP BY TVF.MonthValue
                          ) AS DECIMAL(8, 2)) AS PCTAverage
              FROM      #RelevantSummaryPCT AS PCT
              WHERE     PCT.[Month] = TVF.[MonthValue]
              GROUP BY  TVF.[MonthValue]
            ) AS PCTAverage ,
            TVF.ShortMonth AS [Month]
    FROM    #RelevantSummary AS RS
            RIGHT OUTER JOIN TVF_Months(@StartDate, @EndDate) AS TVF ON TVF.MonthValue = RS.[Month]
    GROUP BY TVF.[MonthName] ,
            TVF.[MonthValue] ,
            TVF.ShortMonth ,
            TVF.DisplayOrder
    ORDER BY TVF.DisplayOrder

我希望实现的是来自2个临时表的单组数据,这些数据存储不同级别报告的结果。 “卷”列是“我的组”的结果量,PCTAverage是所有组的结果量。选择那两个临时表:

SELECT  DISTINCT
            PES.FKProcedureID ,
            PES.PROMSID ,
            UPL.PKID AS UploadID ,
            MONTH(UPL.ShopDate) AS [Month]
    INTO    #RelevantSummary
    FROM    tblPROMsExportSummary AS PES
            INNER JOIN TVF_GetRelevantScorecards(@ProcedureID, @AreaID, @StartDate, @EndDate) AS RES ON RES.PROMSID = PES.PROMSID
            INNER JOIN tblUploadedScorecards AS UPL ON PES.PROMSID = UPL.PEMSID

-- also get them for all PCTs, @AreaID hardcoded to 82
    SELECT  DISTINCT
            PES.FKProcedureID ,
            PES.PROMSID ,
            UPL.PKID AS UploadID ,
            MONTH(UPL.ShopDate) AS [Month]
    INTO    #RelevantSummaryPCT
    FROM    tblPROMsExportSummary AS PES
            INNER JOIN TVF_GetRelevantScorecards(@ProcedureID, 82, @StartDate, @EndDate) AS RES ON RES.PROMSID = PES.PROMSID
            INNER JOIN tblUploadedScorecards AS UPL ON PES.PROMSID = UPL.PEMSID

2 个答案:

答案 0 :(得分:0)

我不如重写子查询,但基于你所写的内容:

  

我想在没有子选择的情况下重写以下select语句;   虽然它现在正常工作,因为它只有在有数据时才有效,当其中一个子选择没有返回数据时我得到一个错误

我想知道COALESCE是否能解决你的问题。

答案 1 :(得分:0)

使用 derived tables 就是我所追求的,在上面的示例中,它可以像这样实现:

SELECT * FROM (
SELECT  DISTINCT
        PES.FKProcedureID ,
        PES.PROMSID ,
        UPL.PKID AS UploadID ,
        MONTH(UPL.ShopDate) AS [Month]
INTO    #RelevantSummary
FROM    tblPROMsExportSummary AS PES
        INNER JOIN TVF_GetRelevantScorecards(@ProcedureID, @AreaID, @StartDate, @EndDate) AS RES ON RES.PROMSID = PES.PROMSID
        INNER JOIN tblUploadedScorecards AS UPL ON PES.PROMSID = UPL.PEMSID ) AS T1 
INNER JOIN (

SELECT  DISTINCT
        PES.FKProcedureID ,
        PES.PROMSID ,
        UPL.PKID AS UploadID ,
        MONTH(UPL.ShopDate) AS [Month]
INTO    #RelevantSummaryPCT
FROM    tblPROMsExportSummary AS PES
        INNER JOIN TVF_GetRelevantScorecards(@ProcedureID, 82, @StartDate, @EndDate) AS RES ON RES.PROMSID = PES.PROMSID
        INNER JOIN tblUploadedScorecards AS UPL ON PES.PROMSID = UPL.PEMSID ) AS T2
ON T1.PROMSID = T2.PROMSID

直接引用4guys帖子

  

我们正在做的是首先从派生表中获取结果集(FROM子句中的SELECT语句)。一旦我们得到了结果集,就好像它本身就是一个表。然后我们在派生表上执行SELECT,返回结果!您可以在文章Obtaining Ranked Values from a Table页面的4GuysFromRolla.com上找到另一个使用派生表的示例。

虽然我的最终选择看起来更像是这样:

SELECT  ISNULL(Volume, 0) AS Volume ,
            ISNULL(PCTAverage, 0) AS PCTAverage ,
            ShortMonth AS [Month] ,
            DisplayOrder
    FROM    ( SELECT    COUNT(DISTINCT ( PROMSID )) AS Volume ,
                        RS.YearMonth
              FROM      @RelevantSummary AS RS
              GROUP BY  YearMonth ) AS RS
            INNER JOIN ( SELECT CAST(CAST(COUNT(PROMSID) AS DECIMAL(8, 3))
                                / ( SELECT  ISNULL(COUNT(DISTINCT RES.Branch), 1)
                                    FROM    tblPROMsExportSummary AS PES
                                            INNER JOIN TVF_GetRelevantScorecards(@ProcedureID, @RootReportLevelID, @ReportLevelID, @StartDate,
                                                                                 @EndDate) AS RES ON RES.PROMSID = PES.PROMSID
                                            INNER JOIN tblUploadedScorecards AS UPL ON PES.PROMSID = UPL.PEMSID
                                    WHERE   RES.YearMonth = PCT.YearMonth
                                    GROUP BY RES.YearMonth ) AS DECIMAL(8, 3)) AS PCTAverage ,
                                YearMonth
                         FROM   @RelevantSummaryPCT AS PCT
                         GROUP BY YearMonth ) AS PCT ON RS.YearMonth = PCT.YearMonth
            RIGHT OUTER JOIN ( SELECT   *
                               FROM     dbo.TVF_Months(@StartDate, @EndDate) ) AS TVF ON TVF.DisplayOrder = RS.YearMonth