Jump to content

Recommended Posts

Here is my problem and it appears that it may only be a problem with PostgreSQL:

 

I am trying to order the products on my online store showing the products that have been sold the most amount of times within the last week on the top. 

 

There are a lot of products that may not have been sold within the last week, so may not appear in the orders_prods table.

 

This is how I want them to be ordered:

 

prod_id-----qtysold

ghi-----------15

abc----------10

def-----------7

lmo-----------0

pqr-----------0

xyz-----------0

 

 

 

Here is my current query:

 

SELECT op.quantitySold, p.thumbnail, p.main_image, p.prod_price, p.prod_id, p.prod_name FROM products AS p INNER JOIN product_categories ON p.prod_id =

 

product_categories.prod_id LEFT OUTER JOIN (SELECT prod_id, SUM(quantity) AS quantitySold FROM orders_prods WHERE order_date > '$dateSevenDaysAgo' GROUP BY

 

prod_id) AS op ON op.prod_id = p.prod_id WHERE product_categories.cat_id = '$catID' ORDER BY op.quantitySold ASC

 

This Query orders the products like this:

 

prod_id-----qtysold

def-----------7

abc----------10

ghi-----------15

lmo-----------0

pqr-----------0

xyz-----------0

 

 

When I order quantitySold by DESC, the products that have been sold recently always appear on the bottom no matter what I try.

 

Here is an example query:

 

SELECT op.quantitySold, p.thumbnail, p.main_image, p.prod_price, p.prod_id, p.prod_name FROM products AS p INNER JOIN product_categories ON p.prod_id =

 

product_categories.prod_id LEFT OUTER JOIN (SELECT prod_id, SUM(quantity) AS quantitySold FROM orders_prods WHERE order_date > '$dateSevenDaysAgo' GROUP BY

 

prod_id) AS op ON op.prod_id = p.prod_id WHERE product_categories.cat_id = '$catID' ORDER BY op.quantitySold DESC, p.prod_id ASC

 

And I get this result:

 

prod_id-----qtysold

lmo-----------0

pqr-----------0

xyz-----------0

ghi-----------15

abc----------10

def-----------7

 

 

If anyone has any idea why this is happening, I would greatly appreciate the help.

 

      Thank You,

                    Kyle

Link to comment
https://forums.phpfreaks.com/topic/37626-order-by-problem-with-joins/
Share on other sites

Is that "0" or is it null?  Postgres places null as greater than all other values when ordering.  Some other DBMS will place null as lower than all others in the ordering.  To fix it, just replace your nulls with the value 0 and the ordering will be what you expect. 

 

SELECT op.quantitySold, p.thumbnail, p.main_image, p.prod_price, p.prod_id, p.prod_name FROM products AS p
INNER JOIN product_categories ON p.prod_id = product_categories.prod_id
LEFT OUTER JOIN (SELECT prod_id, SUM(quantity) AS quantitySold FROM orders_prods WHERE order_date > '$dateSevenDaysAgo' GROUP BY prod_id) AS op ON op.prod_id = p.prod_id
WHERE product_categories.cat_id = '$catID'
ORDER BY COALESCE(op.quantitySold, 0) ASC

 

Alternatively you can put that coalesce within the subquery and leave the order by as it was before.

 

Coalesce will return the first non-null argument.  So the call COALESCE(op.quantitySold, 0) will return op.quantitySold if it is not null, otherwise it will return 0.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.