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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:04:36+00:00 2026-06-15T04:04:36+00:00

I am using the following query to obtain some sales figures. The problem is

  • 0

I am using the following query to obtain some sales figures. The problem is that it is returning the wrong data.

I am joining together three tables tbl_orders tbl_orderitems tbl_payment. The tbl_orders table holds summary information, the tbl_orderitems holds the items ordered and the tbl_payment table holds payment information regarding the order. Multiple payments can be placed against each order.

I am trying to get the sum of the items sum(mon_orditems_pprice), and also the amount of items sold count(uid_orderitems).

When I run the following query against a specific order number, which I know has 1 order item. It returns a count of 2 and the sum of two items.

Item                       ProdTotal    ProdCount
Westvale Climbing Frame    1198         2

This order has two payment records held in the tbl_payment table, which is causing the double count. If I remove the payment table join it reports the correct figures, or if I select an order which has a single payment it works as well. Am I missing something, I am tired!!??

SELECT 
  txt_orditems_pname,
  SUM(mon_orditems_pprice) AS prodTotal,
  COUNT(uid_orderitems) AS prodCount
FROM dbo.tbl_orders
  INNER JOIN dbo.tbl_orderitems ON (dbo.tbl_orders.uid_orders = dbo.tbl_orderitems.uid_orditems_orderid)
  INNER JOIN dbo.tbl_payment ON (dbo.tbl_orders.uid_orders = dbo.tbl_payment.uid_pay_orderid)
WHERE
  uid_orditems_orderid = 61571
GROUP BY
  dbo.tbl_orderitems.txt_orditems_pname
ORDER BY
  dbo.tbl_orderitems.txt_orditems_pname

Any suggestions?

Thank you.

Drill down Table columns

dbo.tbl_payment.bit_pay_paid (1/0) Has this payment been paid, yes no
dbo.tbl_orders.bit_order_archive (1/0) Is this order archived, yes no
dbo.tbl_orders.uid_order_webid (integer) Web Shop's ID
dbo.tbl_orders.bit_order_preorder (1/0) Is this a pre-order, yes no
YEAR(dbo.tbl_orders.dte_order_stamp) (2012) Sales year
dbo.tbl_orders.txt_order_status (varchar) Is the order dispatched, awaiting delivery
dbo.tbl_orderitems.uid_orditems_pcatid (integer) Product category ID
  • 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-15T04:04:38+00:00Added an answer on June 15, 2026 at 4:04 am

    It’s a normal behavior, if you remove grouping clause you’ll see that there really are 2 rows after joining and they both have 599 as a mon_orditems_pprice hence the SUM is correct. When there is a multiple match in any joined table the entire output row becomes multiple and the data that is being summed (or counted or aggregated in any other way) also gets summed multiple times. Try this:

    SELECT      txt_orditems_pname,
                SUM(mon_orditems_pprice) AS prodTotal,
                COUNT(uid_orderitems) AS prodCount
    FROM        dbo.tbl_orders
    INNER JOIN  dbo.tbl_orderitems ON (dbo.tbl_orders.uid_orders = dbo.tbl_orderitems.uid_orditems_orderid)
    INNER JOIN  
    (
        SELECT  x.uid_pay_orderid
        FROM    dbo.tbl_payment x
        GROUP   BY x.uid_pay_orderid
    ) AS payments ON (dbo.tbl_orders.uid_orders = payments.uid_pay_orderid)
    
    WHERE
      uid_orditems_orderid = 61571
    GROUP BY
      dbo.tbl_orderitems.txt_orditems_pname
    ORDER BY
      dbo.tbl_orderitems.txt_orditems_pname
    

    I don’t know what data from tbl_payment you are using, are any of the columns from the SELECT list actually from tbl_payment? Why is tbl_payment being joined?

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

Sidebar

Related Questions

I am using following query to insert some large text data : internal static
I am using following query which works fine for me except one problem SELECT
I have a query that returns data in the following format: id | name
I want to obtain a running sum in query. I'm using the following running
I am using following query to insert data into a SQLite database for Android.
I am fetching data using following query : select * from tbl_words where userID
I am wondering how to obtain the following from a query using xml on
using the following query in SQLDeveloper to test merge everything goes fine: merge into
Using the following query I am looking for a solution to get the latest
I'm using the following query to select users from my 'users' table with 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.