Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7680343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:05:37+00:00 2026-05-31T18:05:37+00:00

I’d like to rewrite the following select statement without the sub selects; while it

  • 0

I’d like to rewrite the following select statement without the sub selects; while it works at the moment as it is it only works if there’s data, when one of the sub selects doesn’t return data I get an error

Each GROUP BY expression must contain at least one column that is not an outer reference.

This is the select:

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

What I am looking to achieve is a a single set of data from 2 temp tables which store results for different levels of reporting. The Volume column is the volume of results for ‘my group’ and the PCTAverage is the volume of results across all groups. The selects for those 2 temp tables:

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:05:39+00:00Added an answer on May 31, 2026 at 6:05 pm

    Using derived tables is what I was after, in the example above it can be achieved like this:

    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
    

    To quote directly from the 4guys post

    What we are doing is first getting the result set from our derived table (the SELECT statement in the FROM clause). Once we have that resultset, it is as though it was a table in itself. We then perform the SELECT on the derived table, returning our results! You can find another example of using derived tables here on 4GuysFromRolla.com in the article Obtaining Ranked Values from a Table page.

    Though my final select looks more like this:

    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 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.