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 3936554
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:57:43+00:00 2026-05-19T23:57:43+00:00

I have a query whose code looks like this: SELECT DocumentID, ComplexSubquery1 … ComplexSubquery5

  • 0

I have a query whose code looks like this:

SELECT DocumentID, ComplexSubquery1 ... ComplexSubquery5
FROM   Document
WHERE  ...

ComplexSubquery are all numerical fields that are calculated using, duh, complex subqueries.

I would like to use this query as a subquery to a query that generates a summary like the following one:

   Field          DocumentCount           Total
     1                 dc1                 s1
     2                 dc2                 s2
     3                 dc3                 s3
     4                 dc4                 s4
     5                 dc5                 s5

Where:

dc<n> = SUM(CASE WHEN ComplexSubquery<n> > 0 THEN 1                  END)
s <n> = SUM(CASE WHEN Field = n              THEN ComplexSubquery<n> END)

How could I do that in SQL Server?


NOTE: I know I could avoid the problem by discarding the original query and using unions:

SELECT '1' AS TypeID,
       SUM(CASE WHEN ComplexSubquery1 > 0 THEN 1 END) AS DocumentCount
       SUM(ComplexSubquery1)                          AS Total
FROM   (SELECT DocumentID, BLARGH ... AS ComplexSubquery1) T
UNION ALL
SELECT '2' AS TypeID,
       SUM(CASE WHEN ComplexSubquery2 > 0 THEN 1 END) AS DocumentCount
       SUM(ComplexSubquery2)                          AS Total
FROM   (SELECT DocumentID, BLARGH ... AS ComplexSubquery2) T
UNION ALL
...

But I want to avoid this route, because redundant code makes my eyes bleed. (Besides, there is a real possibility that the number of complex subqueries grow in the future.)

  • 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-19T23:57:43+00:00Added an answer on May 19, 2026 at 11:57 pm
    WITH Document(DocumentID, Field) As
    (
    SELECT 1, 1 union all
    SELECT 2, 1 union all
    SELECT 3, 2 union all
    SELECT 4, 3 union all
    SELECT 5, 4 union all
    SELECT 6, 5 union all
    SELECT 7, 5
    ), CTE AS
    (
    SELECT DocumentID, 
           Field, 
           (select 10) As ComplexSubquery1,  
           (select 20) as ComplexSubquery2,
           (select 30) As ComplexSubquery3,  
           (select 40) as ComplexSubquery4,
           (select 50) as ComplexSubquery5
    FROM   Document
    )
    SELECT Field, 
           SUM(CASE WHEN RIGHT(Query,1) = Field AND QueryValue  > 1 THEN 1 END ) AS DocumentCount,
           SUM(CASE WHEN RIGHT(Query,1) = Field THEN QueryValue END ) AS Total
    FROM CTE
    UNPIVOT  (QueryValue FOR Query IN 
          (ComplexSubquery1, ComplexSubquery2, ComplexSubquery3, 
           ComplexSubquery4, ComplexSubquery5)
    )AS unpvt
    GROUP BY Field
    

    Returns

    Field       DocumentCount Total
    ----------- ------------- -----------
    1           2             20
    2           1             20
    3           1             30
    4           1             40
    5           2             100
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that originally looks like this: select c.Id, c.Name, c.CountryCode, c.CustomerNumber,
I have a query that looks like this: public IList<Post> FetchLatestOrders(int pageIndex, int recordCount)
I have this query in sql server 2000: select pwdencrypt('AAAA') which outputs an encrypted
I have a query on my database as such: SELECT * FROM expenses WHERE
I have an array being returned from the database that looks like so: $data
Lets say I have some code like: ExampleDomainContext ctx = new ExampleDomainContext(); var query
I have a query that I'm executing from a .NET application to a SQL
I have three tables that I would like to query: Streams, Entries, and FieldInstances.
I'd like to filter the results of a query which returns the results from
I've noticed that a few Wordpress blogs have query statistics present in their footer

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.