How to do complex COUNT()

On my quest to learn basic SQL I got stuck on this task: I need to find and count every instance where product prices differ by at most one (Including the product you are comparing)

From this example:

PRODUCTS
+-----------+------------+
|   name    |    price   |
+-----------+------------+
|paper      |      7     |
+-----------+------------+
|rock       |      4     |
+-----------+------------+
|scissors   |      8     |
+-----------+------------+
|gun        |      6     |
+-----------+------------+

The out come should be:

   name      How_many_times_price_differed
+-----------+------------+
|paper      |      3     |
+-----------+------------+
|rock       |      1     |
+-----------+------------+
|scissors   |      2     |
+-----------+------------+
|gun        |      2     |
+-----------+------------+

I have been doing these practises for 8 hours straight so my tilted brain came up with this stupid answer and its not working

SELECT name, COUNT(price <= price+1 OR price >= price+1)
FROM Products

Obviously this didn't work. No need to tell me exactly how its done but any idea where I should be doing the COUNT(), My newbie logic is running bit thin.



Read more here: https://stackoverflow.com/questions/64397190/how-to-do-complex-count

Content Attribution

This content was originally published by HuManatee at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: