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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:42:31+00:00 2026-05-21T08:42:31+00:00

I’m really not sure how to even phrase the question. So I’ll go by

  • 0

I’m really not sure how to even phrase the question. So I’ll go by example. I currently use this SQL command to get a total of all sales:

SELECT sum(ot.value*o.currency_value)
  FROM orders_total ot
  LEFT JOIN orders o on o.orders_id = ot.orders_id
 WHERE ot.class = 'ot_total'
   AND o.date_purchased between '2010-01-01' and '2010-12-31'

And that works fine, but now I want to do a report that cross-references the above with a 3rd table (orders_products) to sum up only the orders containing a specific product id. So I tried this:

SELECT sum(ot.value*o.currency_value)
  FROM orders_total ot
 (LEFT JOIN orders o on o.orders_id = ot.orders_id)
  LEFT JOIN orders_products op on ot.orders_id = op.orders_id
 WHERE ot.class = 'ot_total'
   AND o.date_purchased between '2010-01-01' and '2010-12-31'
   AND op.products_id = 321

But that gives me a higher-than-expected total. Investigating manually, I discovered the obvious reason is that any given order can (of course) have more than one product.

I’d like to show an example but I can’t do tables here it seems.

Q: How do I sum up a total value without getting duplicate records from orders matching multiple entries in the op table?

Does that make any sense at all?

Edit:

I feel like I’m sort of onto something with this:

SELECT distinct o.orders_id
  FROM orders o
  JOIN orders_products op on o.orders_id = op.orders_id
 WHERE o.date_purchased between '2010-01-01' and '2010-12-31'
   AND op.products_id = 321

That results in a listing of all orders_id represented by the product in question. So now I need to sort of inject that result into another statement summing up the values? But how??

Edit:

Here’s an attempt to show my tables:

orders_total

orders_total_id orders_id class      value 
--------------- --------- ---------- -------
    1                 1   ot_sub       100
    2                 1   ot_shipping  10
    3                 1   ot_total     110
    4                 2   ot_sub       200
    5                 2   ot_shipping  10
    6                 2   ot_total     210
    7                 3   ot_sub       50
    8                 3   ot_shipping  5
    9                 3   ot_sub       55

orders

orders_id    currency_value    date_purchased
---------    --------------    --------------
   1              1.0000         2010-04-20
   2              1.0000         2010-05-05
   3              1.0000         2010-06-01

orders_products

orders_products_id    orders_id    products_id
------------------    ---------    -----------
    1                     1            321
    2                     2            555
    3                     2            132
    4                     2            321
    5                     3            132

So I want an SQL statement that will give me a result of 320 (total of all orders containing product ID 321, which is orders 1 and 2 but not 3; “value” of “ot_total” for 1 is 110 and for 2 is 210. 110 + 210 = 320).

EDIT/SOLUTION:

Thanks to JNK for turning me on to EXISTS. As it turns out, this did the job nicely:

SELECT sum(ot.value*o.currency_value) FROM orders_total ot LEFT JOIN orders o ON o.orders_id = ot.orders_id WHERE EXISTS (SELECT NULL FROM orders_products op WHERE op.products_id = 321 AND op.orders_id = o.orders_id) and o.date_purchased between '2010-01-01' and '2010-12-31' and ot.class = 'ot_total'
  • 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-21T08:42:32+00:00Added an answer on May 21, 2026 at 8:42 am

    Use EXISTS – this is a perfect use case.

    SELECT <all your fields>
    FROM table
    LEFT JOIN orders o
       ON table2.key = table.key
    WHERE EXISTS (SELECT NULL
                  FROM orders_products op
                  WHERE op.products_id = xxx
                  AND op.orderid = o.orderid)
    

    This will do a short-circuit comparison on the subquery. If the row in the outer query matches, it gets included. If not, it’s not in the final result set.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string

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.