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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:59:57+00:00 2026-05-30T18:59:57+00:00

I have 2 integer fields in a table user: leg_count and leg_length. The first

  • 0

I have 2 integer fields in a table “user”: leg_count and leg_length. The first one stores the amount of legs of a user and the second one – their total length.

Each leg that belongs to user is stored in separate table, as far as typical internet user can have zero to infinity legs:

CREATE TABLE legs (
    user_id int not null,
    length  int not null
);

I want to recalculate the statistics for all users in one query, so I try:

UPDATE users SET
    leg_count = subquery.count, leg_length = subquery.length
FROM (
    SELECT COUNT(*) as count, SUM(length) as length FROM legs WHERE legs.user_id = users.id
) AS subquery;

and get “subquery in FROM cannot refer to other relations of same query level” error.

So I have to do

UPDATE users SET
    leg_count =  (SELECT COUNT(*)    FROM legs WHERE legs.user_id = users.id),
    leg_length = (SELECT SUM(length) FROM legs WHERE legs.user_id = users.id)

what makes database to perform 2 SELECT’s for each row, although, required data could be calculated in one SELECT:

SELECT COUNT(*), SUM(length) FROM legs;

Is it possible to optimize my UPDATE query to use only one SELECT subquery?

I use PostgreSQL, but I beleive, the solution exists for any SQL dialect.

TIA.

  • 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-30T18:59:58+00:00Added an answer on May 30, 2026 at 6:59 pm

    I would do:

    WITH stats AS
       ( SELECT COUNT(*)    AS cnt
              , SUM(length) AS totlength
              , user_id
           FROM legs
          GROUP BY user_id
       )
    UPDATE users
       SET leg_count = cnt, leg_length = totlength
      FROM stats
     WHERE stats.user_id = users.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table tbluser with 2 fields: userid = integer (autoincrement) user =
Specifically, I have a form for creating a User . One of the fields
My table is a messaging table basically. I have the following fields: id: integer,
Say I have a table with a unique positive integer field. I currently have
I have a table which stores historical data. It's mapped to an Entity with
I have Doctrine 2.1 entity: /** * @Entity(repositoryClass=Varlab\Model\Repository\UserRepository) * @Table(name=user) */ class User {
I have a user table that has many columns, it looks roughly like this:
I have a table in which a field contains an integer or NULL .
I have a table containing user input which needs to be optimized. I have
I have a WPF app, where one of the fields has a numeric input

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.