0

I know there are many variants of this question, but none seem to address my issue.

I have a page reporting website order stats. One of the variables it considers is which 'website' a customer used. The website ids are integers, but for historical reasons, one of them is stored as either 0 or NULL. I need to group them together in my query.

I know I could update all NULL entries to 0 in that row of the db, but a lot of changes would need to be made to prevent more being added.

My original query was:

SELECT o.website, p.orderid, count(p.orderid) ordercount
FROM gdd_order as o, gdd_process as p 
WHERE o.orderid = p.orderid 
AND p.prog_return >= '2024-03-01' 
AND p.prog_return < '2024-03-31'  
GROUP BY o.website 
ORDER BY FIELD(o.website,0,5,1,2,3,4)

which returns

website orderid ordercount
(NULL) 92914 76
0 93256 48
5 92916 74
1 92908 136

I tried:

SELECT ifnull(o.website, 0) website, p.orderid, count(p.orderid) ordercount
FROM gdd_order as o, gdd_process as p 
WHERE o.orderid = p.orderid 
AND p.prog_return >= '2024-03-01' 
AND p.prog_return < '2024-03-31'  
GROUP BY o.website 
ORDER BY FIELD(o.website,0,5,1,2,3,4)

which makes NULL zero, but doesn't group the 'null 0' row in with the '0' row:

website orderid ordercount
0 92914 76
0 93256 48
5 92916 74
1 92908 136

How do I combine the '0' and 'NULL 0' data in a single row?

5
  • 1
    I'm not sure it makes sense to say you want grouping by website and then include order ids in the result. That's going to force grouping on order id too I think Commented May 17, 2024 at 16:36
  • GROUP BY IFNULL(o.website, 0) Commented May 17, 2024 at 16:56
  • If you give the alias a different name from the column, you can use GROUP BY <alias> Commented May 17, 2024 at 16:57
  • Also use IFNULL() in the ORDER BY column. Commented May 17, 2024 at 16:58
  • The inclusion of p.orderid in your select list makes your query nondeterministic. Configured properly (with ONLY_FULL_GROUP_BY enabled) you should be getting an error. Please read MySQL Handling of GROUP BY. Commented May 17, 2024 at 18:01

3 Answers 3

2

Use IFNULL(o.website, 0) consistently throughout the query.

SELECT ifnull(o.website, 0) website, p.orderid, count(p.orderid) ordercount
FROM gdd_order as o, gdd_process as p 
WHERE o.orderid = p.orderid 
AND p.prog_return >= '2024-03-01' 
AND p.prog_return < '2024-03-31'  
GROUP BY IFNULL(o.website, 0)
ORDER BY FIELD(IFNULL(o.website, 0),0,5,1,2,3,4)
Sign up to request clarification or add additional context in comments.

Comments

0

friend. I think you can add this part to combine 0 and NULL

IFNULL(o.website, 0)

So, your query:

SELECT IFNULL(o.website, 0) AS website, p.orderid, COUNT(p.orderid) AS ordercount
FROM gdd_order AS o
JOIN gdd_process AS p ON o.orderid = p.orderid
WHERE p.prog_return >= '2024-03-01' AND p.prog_return < '2024-03-31'  
GROUP BY IFNULL(o.website, 0)
ORDER BY FIELD(IFNULL(o.website, 0), 0, 5, 1, 2, 3, 4);

Tell if this works with your database, we don't have sample information to prove it.

Comments

-1

Use COALESCE() function which returns the first non-null value in a list.

https://dev.mysql.com/doc/refman/8.0/en/comparison-operators.html#function_coalesce

Update your query like:

SELECT o.website, p.orderid, count(p.orderid) ordercount
FROM (SELECT COALESCE(website, 0), [OTHER COLUMS] FROM gdd_order) o, gdd_process as p 
WHERE o.orderid = p.orderid 
AND p.prog_return >= '2024-03-01' 
AND p.prog_return < '2024-03-31'  
GROUP BY o.website 
ORDER BY FIELD(o.website,0,5,1,2,3,4)

It's should work



SELECT COALESCE(website, 0), [OTHER COLUMS] FROM gdd_order;

This query will change your table from

website orderid ordercount
(NULL) 92914 76
0 93256 48
5 92916 74
1 92908 136

to

website orderid ordercount
0 92914 76
0 93256 48
5 92916 74
1 92908 136

Thank You

3 Comments

They're already using IFNULL(), which is the same as COALESCE() when there are only 2 arguments.
You need to assign an alias in the subquery.
Yes, you're right @Barmar. Your approach is best...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.