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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:49:03+00:00 2026-05-28T13:49:03+00:00

I have a table containing transaction information on items. The possible transactions are purchase

  • 0

I have a table containing transaction information on items. The possible transactions are purchase (type_id: 1) and sale(type_id: 2). A really stripped down version of the table “Transactions” is like the following:

Transaction_ID  Item_ID  Transaction_Type_ID  Quantity  Price  Date
1,               1,        1,                    50,        10,     6/1,
2,               2,        1,                    40,        20,     13/1,
3,               1,        2,                    10,        13,     14/1,
4,               2,        2,                    20,        25,     3/2,
5,               1,        2,                    20,        12,     20/2

I have the following query:

SELECT B.Item_ID
       B.Quantity - (SELECT SUM(Quantity) FROM Transactions A Where A.Item_ID = B.Item_ID AND A.Transaction_Type_ID = 2) AS 'Quantity Left'
       B.Price * (B.Quantity - (SELECT SUM(Quantity) FROM Transactions A Where A.Item_ID = B.Item_ID AND A.Transaction_Type_ID = 2)) AS 'Purchase Amount Left'
FROM   Transaction B
WHERE  B.Transaction_Type_ID = 1
AND    B.Quantity - (SELECT SUM(Quantity) FROM Transactions A Where A.Item_ID = B.Item_ID AND A.Transaction_Type_ID = 2) > 0

Like you may already noticed, I am trying to get all the purchased items that are still in stock. You may notice also that there is an annoying sub-query repeating twice in the SELECT clause and once in the WHERE clause.

How can I reduce it? Is it possible to use WITH in the beginning of the statement in this case?

  • 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-28T13:49:03+00:00Added an answer on May 28, 2026 at 1:49 pm

    JOIN onto an aggregated derived table?

    SELECT 
        B.Item_ID
        B.Quantity - ISNULL(A.SUMQuantity, 0) AS 'Quantity Left',
        B.Price * (B.Quantity - ISNULL(A.SUMQuantity, 0)) AS 'Purchase Amount Left'
    FROM  
        Transaction B
        LEFT JOIN
        (
         SELECT SUM(Quantity) AS SUMQuantity, Item_ID
         FROM Transaction 
         WHERE Transaction_Type_ID = 2
         GROUP BY Item_ID
        ) ON A.Item_ID = B.Item_ID
    WHERE  
        B.Transaction_Type_ID = 1
        AND   
        B.Quantity - ISNULL(A.SUMQuantity, 0) > 0
    

    If you always have rows where Transaction_Type_ID = 2, then you can remove LEFT JOIN and ISNULL. In your current code, you assume you always have rows.

    It also looks like you’re mixing entities in the same table based on Transaction_Type_ID. A simple SUM(Quantity) .. GROUP BY Item_ID would be more correct if
    – sales are <0 quantity transaction
    – restocks are >0 quantity transaction2

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

Sidebar

Related Questions

Ok I have a data table containing duplicate Reciept numbers and a transaction value
I have table containing information about a series of x number of quizzes. It
I have a Table containing some information that I need. All these rows also
I have a table containing multiple records for different transactions i.e. ID Date REF
I have a table containing millions of transaction records(Obj1) which looks like this TransactionNum
I have a table containing prices for a lot of different things in a
I have a table containing the runtimes for generators on different sites, and I
I have a table containing hundreds of entries and I am trying to delete
I have a table containing cells with text of various lengths. It is essential
I have a table containing 10 records. I want to fetch 5th and the

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.