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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:34:01+00:00 2026-05-31T14:34:01+00:00

Hey All you out there, I am trying to write a query that sums

  • 0

Hey All you out there,

I am trying to write a query that sums the amount of data members of a group are allowed to use, and also in that same query select the amount of data an individual member is allowed to use.

This is my query at the moment

SELECT (groups.extra_space + DataPlan.total_disk_space_limit) AS total_space, 
   SUM(User.disk_space_limit) AS total_allocated, 
   User.id, User.first_name, 
   User.last_name, 
   User.disk_space_limit 
FROM groups INNER JOIN users AS User ON (groups.id = User.group_id)
INNER JOIN data_plans AS DataPlan    ON (groups.data_plan_id = DataPlan.id 
AND User.group_id = Group_id) WHERE groups.id = 24;

The data set this returns is:

+-------------+-----------------+----+------------+-----------+------------------+
| total_space | total_allocated | id | first_name | last_name | disk_space_limit |
+-------------+-----------------+----+------------+-----------+------------------+
|         300 |              82 | 22 | Big        | Bird      |                0 |
+-------------+-----------------+----+------------+-----------+------------------+

I would like the data set to be more along the lines of…

+-------------+-----------------+----+------------+-----------+------------------+
| total_space | total_allocated | id | first_name | last_name | disk_space_limit |
+-------------+-----------------+----+------------+-----------+------------------+
|         300 |              82 | 22 | Big        | Bird      |                0 |
|         300 |              82 | 23 | Cookie     | Monster   |               11 |
|         300 |              82 | 24 | Oscar      | Grouch    |               32 |
|         300 |              82 | 25 | Count      | Count     |               19 |
|         300 |              82 | 26 | Snuffel    | Ufogus    |               20 |
+-------------+-----------------+----+------------+-----------+------------------+

I know this would be easy to do in 2 queries, but I would really like to learn how to do it in one.

Any help would be much appreciated!

Thanks!

  • 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-31T14:34:03+00:00Added an answer on May 31, 2026 at 2:34 pm

    Maybe just add a group by User.id clause to the end of of your current query

    Edit: Here is an example with a subquery:

    SELECT (groups.extra_space + DataPlan.total_disk_space_limit) AS total_space, 
       (select SUM(users.disk_space_limit) from users WHERE users.group_id = groups.id) AS total_allocated, 
       User.id, User.first_name, 
       User.last_name, 
       User.disk_space_limit 
    FROM groups INNER JOIN users AS User ON (groups.id = User.group_id)
    INNER JOIN data_plans AS DataPlan    ON (groups.data_plan_id = DataPlan.id 
    AND User.group_id = Group_id) WHERE groups.id = 24;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all, I'm trying to fix up my numerous scenarios that test out pages
Hey all i am trying to figiure out why my code is not triggering
Hey all i am in need of some help trying to figure out how
Hey all, I'm about to rip my hair out. I have this client that
Hey everyone, my problem is that im trying to figure out how to get
Hey all. I've been been trying to figure this out for a while now.
hey all, throwing this one out there... hope it's a simple one. using the
Hey, I'm using Mercurial. I just found out that there is a .hg folder
Hey bloggers out there! I've created Wordpress blog that I am hosting myself, and
Hey, I'm trying to write a program that will accept new tasks from people,

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.