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

  • Home
  • SEARCH
  • 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 8995939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:41:11+00:00 2026-06-15T23:41:11+00:00

I am trying to add a row to a SQL select query that contains

  • 0

I am trying to add a row to a SQL select query that contains a sum (or any other operation, such as an average, for that matter) as a last row. In this it is something similar to GROUP BY … WITH ROLLUP.

To begin with: let’s say I have a table t with fields ID, A, B and C, all of which are numbers. Also, ID is not unique, but rather a category. My SELECT query is supposed to count how many of these numbers fall within a designated range.

So, for example, the end result would be

(SELECT t.ID, a.ac, b.bc, c.cc FROM t
LEFT JOIN (SELECT COUNT(*) cc,ID FROM t WHERE A BETWEEN 2 AND 4 GROUP BY ID) AS a ON a.ID=t.ID 
LEFT JOIN (SELECT AVG(B) cc,ID FROM t WHERE B BETWEEN 19 AND 40 GROUP BY ID) AS b ON b.ID=t.ID 
LEFT JOIN (SELECT COUNT(*) cc,ID FROM t WHERE C BETWEEN 12 AND 14 GROUP BY ID) AS c ON a.ID=t.ID GROUP BY t.ID) 

union 

(select 'Overall',
 (SELECT COUNT(*) cc FROM t WHERE A BETWEEN 2 AND 4),
 (SELECT AVG(B) cc FROM t WHERE B BETWEEN 19 AND 40),
 (SELECT COUNT(*) cc FROM t WHERE C BETWEEN 12 AND 14) );

However, this solution is not ideal, for I need to re-state the conditions for A,B and C. I would like to know whether there is a simple way of accomplishing the same result specifying the conditions only once.

Thanks in advance.

  • 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-06-15T23:41:12+00:00Added an answer on June 15, 2026 at 11:41 pm

    I don’t think there’s a simpler solution. But I would rewrite your queries like this:

    SELECT
      t.ID,
      count(case when A between 2 and 4 then ID end),
      AVG(case when B between 19 and 40 then B end),
      COUNT(case when C between 12 and 14 then id end)
    FROM t
    GROUP BY ID
    UNION
    select
      'Overall',
      count(case when A between 2 and 4 then ID end),
      AVG(case when B between 19 and 40 then B end),
      COUNT(case when C between 12 and 14 then id end)
    FROM t
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add a Row to my data.frame to spit out the average
Here Im trying to add extra row by using union query between data and
Ok, I am trying to add one row of multiple images to a JPanel,
I am trying to add a footer row of DropDownLists to my GridView control.
I am trying to add more values to my row, but all the columns
I am trying to add style to a table row in C# code. However,
I'm trying to add multiple elements in a single row within a div. However,
I am trying to setup jQuery rows, with add/remove row functionality. I got started
I am trying to get a new dynamically created row to add to a
I have an oracle sql query select distinct tab1.col1, tab2.col1 from table1 tab1 join

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.