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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:52:15+00:00 2026-06-06T22:52:15+00:00

The following queries update the same table like 7 times.. I would appreciate your

  • 0

The following queries update the same table like 7 times.. I would appreciate your help on how I might optimize this by putting it into one query so it passes through the table only once instead of 7 times.. This is really slowing down performance on large datasets..

Thanks.

P.S. I don’t know if it will still work out but each query string depends on the query before it to accurately calculate.. So if we need to keep them separate in order to keep it accurate then I could use some help optimizing each string (query) individually.

The following SQL queries are made in Delphi 2009 So the format may be a little different but you should still be able to read the query pretty easily..

Str1 :=
'update user set amount = ' +
  '(select round(sum(bill.amount),2) from bill where ' +
  'bill.user = user.code); ' +
'update user set pay = ' +
  '(select round(sum(bill.pay),2) from bill where ' +
  'bill.user = user.code); ' +
'update user set balance = round(amount + pay,2);';

//execute query

 Str1 :=
'update user set group_amt = ' +
  '(select sum(bill.amount) from bill where ' +
  'bill.client = user.client); ' +
'update user set group_pay = ' +
  '(select sum(bill.pay) from bill where ' +
  'bill.client = user.client); ' +
'update user set group_bal = round(group_amt + group_pay,2);';

 //execute query

Str1 :=
'update user set bal_flag = true ' +
'where abs(balance) > 0.001 and bal_flag = false;';
  • 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-06T22:52:17+00:00Added an answer on June 6, 2026 at 10:52 pm

    I suspect you can merge 6 of the updates into just 2 updates…

    UPDATE
      user
    SET
      amount   = bill.amount,
      pay      = bill.pay,
      balance  = bill.pay + bill.amount
    FROM
    (
      SELECT
        user,
        round(sum(bill.amount),2)   AS amount,
        round(sum(bill.pay)   ,2)   AS pay
      FROM
        bill
      GROUP BY
        user
    )
      AS bill
    WHERE
      bill.user = user.code
    

    And nearly identically…

    UPDATE
      user
    SET
      group_amt = bill.amount,
      group_pay = bill.pay,
      group_bal = bill.pay + bill.amount
    FROM
    (
      SELECT
        client,
        round(sum(bill.amount),2)   AS amount,
        round(sum(bill.pay)   ,2)   AS pay
      FROM
        bill
      GROUP BY
        client
    )
      AS bill
    WHERE
      bill.client = user.client
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please help to improve the following query: UPDATE queries q SET query = (SELECT
I am new in Blackberry. Can anyone help me out for following queries? Which
How would I combine (stack vertically) the following 3 queries into one query that
I want to do the following two SQL Queries in Microsoft SQL SERVER UPDATE
I have the following two queries: select count(*) from segmentation_cycle_recipients scr , segmentation_instance si
I have the following two queries, I believe that the one that uses the
I have the following two queries which I want to get in one query.
I have the 2 following LINQ queries and I haven't quite got my head
Can anyone please explain to me why the following two queries yield different results?
I'm running the following statement to see what queries are executing in sql server:

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.