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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:23:08+00:00 2026-06-13T23:23:08+00:00

I’m having problems with a join in MySQL. I have 2 tables, customerorder and

  • 0

I’m having problems with a join in MySQL. I have 2 tables, customerorder and customerorderpos. The pos table contains the items ordered – so there is always just one entry per order in the customerorder table but there may be several with the same customerorderid in the customerorderpos.

Everything works in the query except when I try to do a sum calculation on customerorder. For example sum(cart_total_complete). This is returning duplicate values (it’s counting more than once for the orders with multiple customerorderpos records).

I’m sure it’s something basic, either with the join type or how I use distinct, but I’ve been trying for hours and just can’t get it to work…

Any ideas what I’m doing wrong?
Thanks for your help!!

select concat(left(dayname(from_unixtime(customerorder.datetime)),3), ' ',
day(from_unixtime(customerorder.datetime))) as day, 
count(distinct customerorder.customerorderid) as count_totalorders, 
count(distinct customerorderpos.itemid) as count_differentitems, 
sum(customerorderpos.quantity_ordered - customerorderpos.quantity_cancelled) as quantity_ordered, 
sum(customerorderpos.itemsubtotal) as item_subtotal, 
sum(customerorderpos.pricechangetotal) as item_pricechangetotal, 
sum(customerorderpos.itemtotal) as item_total, 
sum(customerorderpos.purchase_price * (customerorderpos.quantity_ordered - customerorderpos.quantity_cancelled)) as item_purchasepricetotal, 
sum(cart_discounttotal) as total_discount, 
sum(customerorderpos.itemtotal) - sum(customerorderpos.purchase_price * (customerorderpos.quantity_ordered - customerorderpos.quantity_cancelled)) - sum(cart_discounttotal) as item_earningstotal, 
sum(cart_total_shipping) as total_shipping, 
sum(cart_total_tax) as total_tax, 
sum(cart_total_complete) as total_complete 
from customerorder inner join customerorderpos on customerorderpos.customerorderid = customerorder.customerorderid 
where customerorder.status_cancelled = 0 
group by day(from_unixtime(customerorder.datetime)) order by customerorder.datetime
  • 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-13T23:23:09+00:00Added an answer on June 13, 2026 at 11:23 pm

    If you first aggregate your customerorderpos table by customerorderid:

    SELECT   customerorderid,
             SUM(quantity_ordered - quantity_cancelled) AS quantity_ordered,
             SUM(itemsubtotal) AS item_subtotal,
             SUM(pricechangetotal) AS item_pricechangetotal,
             SUM(itemtotal) AS item_total,
             SUM(purchase_price * (
               quantity_ordered - quantity_cancelled
             )) AS item_purchasepricetotal,
    FROM     customerorderpos
    GROUP BY customerorderid
    

    You can then join the result to your customerorder table:

    SELECT   FROM_UNIXTIME(customerorder.datetime, '%a %e') AS day, 
             COUNT(*)                       AS count_totalorders,
             SUM(i.quantity_ordered)        AS quantity_ordered, 
             SUM(i.item_subtotal)           AS item_subtotal,
             SUM(i.item_pricechangetotal)   AS item_pricechangetotal, 
             SUM(i.item_total)              AS item_total, 
             SUM(i.item_purchasepricetotal) AS item_purchasepricetotal, 
             SUM(o.cart_discounttotal)      AS total_discount, 
             SUM(
               i.item_total
             - i.purchasepricetotal
             - o.cart_discounttotal
             )                              AS item_earningstotal, 
             SUM(o.cart_total_shipping)     AS total_shipping, 
             SUM(o.cart_total_tax)          AS total_tax, 
             SUM(o.cart_total_complete)     AS total_complete 
    FROM     customerorder o JOIN (
      SELECT   customerorderid,
               COUNT(DISTINCT itemid) AS count_differentitems,
               SUM(quantity_ordered - quantity_cancelled) AS quantity_ordered,
               SUM(itemsubtotal) AS item_subtotal,
               SUM(pricechangetotal) AS item_pricechangetotal,
               SUM(itemtotal) AS item_total,
               SUM(purchase_price * (
                 quantity_ordered - quantity_cancelled
               )) AS item_purchasepricetotal,
      FROM     customerorderpos
      GROUP BY customerorderid
    ) i USING (customerorderid)
    WHERE    o.status_cancelled = 0 
    GROUP BY FROM_UNIXTIME(o.datetime, '%e')
    ORDER BY o.datetime
    

    Note that I have removed count_differentitems as it is not clear if you want to aggregate the number of distinct items in each order (which would be a simple case of extending the above) or if you want the number of distinct items across all aggregated orders (which would require an additional table join).

    Note also that I have used the date format argument to FROM_UNIXTIME() in place of your manual attempt at creating a formatted date string.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.