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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:54:28+00:00 2026-05-13T06:54:28+00:00

I am trying to figure out the most efficient way to format this query.

  • 0

I am trying to figure out the most efficient way to format this query.

I have three tables – Transaction, Purchase and Item.

The desired result is to select sales data (quantity/sales total) for each item for a specific client.


the tables are formatted as follows: (=primary key)*

Transaction: Transaction_ID*, Timestamp

Purchase: Purchase_ID*, Sale, Item_ID, Transaction_ID, Quantity

Item: Item_ID*, Client_ID, Description


As of right now I have a query set up to select each item for a given client_id:

SELECT Item_ID, Description FROM Item WHERE Client_ID = $ClientId  

Then I store the results in an array, and executing a query to retrieve the sales data for each Item_ID between a given date-range:

SELECT Sale, Quantity
  FROM Purchase INNER JOIN Transaction
    ON Purchase.Transaction_ID = Transaction.Transaction_ID
 WHERE Transaction.Timestamp >= $start
   AND Transaction.Timestamp<= $end

Then for each row fetched, I sum the quantity.

It gets the job done, but is probably not the most efficient way to get this done.

I would like to order this data by the sales totals, putting the greatest number first
How would you go about sorting this from greatest to least?

Is there a way to get this all done in one 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-13T06:54:29+00:00Added an answer on May 13, 2026 at 6:54 am

    would the following work?

    SELECT Item_ID, SUM(Quantity), Sum(Sale)
    FROM Item
    JOIN Purchase ON Item.Item_ID=Purchase.Item_ID
    JOIN Transaction ON Purchase.Transaction_ID=Transaction.Transaction_ID
    WHERE Client_ID = $ClientId
    AND Transaction.Timestamp>= $start
    AND Transaction.Timestamp <= $end
    GROUP BY Item_ID
    ORDER BY SUM(Quantity), SUM(Sale) DESC
    

    I think this is what you want from the good question you asked.
    There’s probably a stupid mistake in there somewhere as I haven’t tested it on your tables, but it ought to get you started.

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

Sidebar

Related Questions

I've spent most of my day trying to figure out why this error is
I'm trying to figure out how to configure a tool to run whenever a
It seems like the most simplistic things are hard to figure out sometimes... Many
I have a fixed-position image that I would like to fade out when scrolling
I'm learning ASP.NET MVC 3 and trying to create a View with a single
I was thinking about developing an own file archive format to use for private
I am trying to do some basic file parsing within a directory in Python
I am trying to relax my website's session expiration policy so that the users
I found some sample code posted at https://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_thread/thread/4d45e9ea5471cba4/4519371a77ed4a74?hl=en&pli=1 for self installing a Windows Service.
Forgive the title, I'm not really sure how to explain what I'm seeing. Sample

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.