从列中的负数中减去以在 SQL 查询中使用

Subtract from negative numbers in column to use in SQL query

我在两列中有一组数字 table,但我只需要将负数减去 10,然后就可以在 SQLite3 中的查询中直接使用它们。

我目前有以下查询:

10 * (customer_x / 10), 10 * (customer_y / 10), 
COUNT (*) FROM t_customer 
GROUP BY customer_x / 10, customer_y / 10 
ORDER BY 3 DESC;

这使得 customer_x 和 customer_y 中的值成为基本坐标,但任何负值都将比它们应有的网格正方形高 10。在将负值提交到查询之前,我需要一种仅从负值中减去 10 的方法。

一个选项是使用子查询,在负值的情况下生成调整后的数字:

SELECT 10 * (t.customer_x / 10) AS col1,
       10 * (t.customer_y / 10) AS col2,
       COUNT(*) AS some_count
FROM
(
    SELECT CASE WHEN customer_x < 0 THEN customer_x - 10 ELSE customer_x END AS customer_x,
           CASE WHEN customer_y < 0 THEN customer_y - 10 ELSE customer_y END AS customer_y
    FROM t_customer
) t
GROUP BY t.customer_x / 10,
         t.customer_y / 10
ORDER BY 3 DESC;