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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:44:53+00:00 2026-05-18T09:44:53+00:00

Firstly, I’m rather new to SQL and I’ve run into a roadblock. I’m using

  • 0

Firstly, I’m rather new to SQL and I’ve run into a roadblock. I’m using the Mimer SQL system.

I have three tables: “Transactions”, roughly equivalent with a receipt total, which I want to update with data from a selection and the tables “item”, containing prices and “sale”, containing number of items and item IDs for a given transaction, which I have to join in order to get the data to update Transaction with.

SELECT sale.T_ID, SUM(sale.quantity * item.price) as total
FROM sale
INNER JOIN item
ON sale.I_ID = item.I_ID
GROUP BY T_ID

Gives me the desired data selection with the transaction IDs and the sum total for that transaction:

T_ID Amount
1    100
2    150
etc...

I want to update the Transaction table, which contains columns “T_ID” and “Total”. I want to match the T_IDs and update the Total with the data from the corresponding Amount in the selection. The query:

UPDATE transaction SET total = (
    SELECT total FROM (
        SELECT sale.T_ID, SUM(sale.quantity * item.price) as total
            FROM sale
            INNER JOIN item
            ON sale.I_ID = item.I_ID
            GROUP BY T_ID)
    WHERE transaction.T_ID=T_ID);

I can sense that the above statement is faulty, but unable to discern the problem. How should I construct the query?

  • 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-18T09:44:53+00:00Added an answer on May 18, 2026 at 9:44 am

    Only select the SUM(sale.quantity * item.price) in your subquery and remove the select total one.

    update Transaction
        set total = (
            select SUM(sale.quantity * Item.price)
                from Sale
                    inner join Item on Sale.I_ID = Item.I_ID
                where Sale.T_ID = Transaction.T_ID
        )
    

    That is what I would try out in SQL Server.

    I would have prefered to have some sample data so that I can test it against my local SQL Server database in order to verify whether this statement does what is expected, though you’re not using SQL Server, the idea is still the same.

    EDIT #1

    Though I do not fully understand why it does not overwrite values in the transaction.total column when a transaction is composed of several different items, your suggested query works.

    The behaviour of the SUM function is to sum all targeted records resulting in only one scalar value for each Sale and Item rows. There can be only one sum, can it not?

    That said, it multipies each resulting rows from the constraint, that is, for a particular transaction.

    1. It selects both Sale and Item rows for this very transaction;
    2. It then iterates through each of the resulting rows and multiply Sale.Quantity and Item.Price together, which is worth a value for each row;
    3. Once a row is multiplied, its total is then added to a total which is stored somewhere in memory;
    4. Once it has processed all the rows for that transaction, it comes out with a scalar value, which is the sum of all the rows in Sale and Item for that given transaction;
    5. This SUM ends up to be this transaction’s total.

    In other words, the subquery “knows” for which transaction to sum which is filtered in the subquery itself. The Transaction table is accessible in the subquery as part of the main SQL statement. So, putting it in the where clause filters the rows from Sale and Item that will be later multiplied and additioned for the total.

    Does this help you better understand this update statement?

    Please feel free to ask your questions. =)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly, let me set out what I'd like to do. Assume I have three
Firstly, I'm a newbie to C# and SharePoint, (less than a month's experience) so
Firstly, Real World Haskell , which I am reading, says to never use foldl
So firstly here's my query: ( NOTE:I know SELECT * is bad practice I
I wish to order a table: Firstly by Field1=3 Then by Field2 DESC I

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.