我想按降序返回tcv SUM结果排序的结果。如果我删除FORMAT()函数,但是当我将SUMAT()函数添加到SUM时,它会按照第一个数字的顺序返回结果,而不是查看整个数字。
所以它会在7,456,234.00之前返回87,400.00,因为8在7之后,这显然不是理想的结果。
SELECT
x.user as user, u.name,
FORMAT(SUM(IF(x.stage IN ('Closed Won') AND x.close_date BETWEEN '2018-04-01 00:00:00' AND '2018-04-30', x.amount_base, 0)), 2) as tcv,
FORMAT(SUM(IF(x.stage IN ('Closed Won') AND x.close_date BETWEEN '2018-04-01 00:00:00' AND '2018-04-30', x.mrr_base, 0)), 2) as mrr,
FROM table AS x
LEFT JOIN users u ON x.owner = u.name WHERE x.resign_new = 'New' AND x.owner IN ("Name 1", "Name 2", "Name 3")
GROUP BY x.owner
ORDER BY tcv DESC
我需要使用格式,因为数字需要使用逗号分隔符进行格式化以便于阅读。
我在错误地将ORDER BY归还给我的错误是什么?
答案 0 :(得分:0)
sum函数返回十进制或双精度,具体取决于要聚合的列的数据类型。因此,要正确排序,您可以删除顺序中的format子句。见下文。
SELECT
x.user as user, u.name,
FORMAT(SUM(IF(x.stage IN ('Closed Won') AND x.close_date BETWEEN '2018-04-01 00:00:00' AND '2018-04-30', x.amount_base, 0)), 2) as tcv,
FORMAT(SUM(IF(x.stage IN ('Closed Won') AND x.close_date BETWEEN '2018-04-01 00:00:00' AND '2018-04-30', x.mrr_base, 0)), 2) as mrr,
FROM table AS x
LEFT JOIN users u ON x.owner = u.name WHERE x.resign_new = 'New' AND x.owner IN ("Name 1", "Name 2", "Name 3")
GROUP BY x.owner
ORDER BY (SUM(IF(x.stage IN ('Closed Won') AND x.close_date BETWEEN '2018-04-01 00:00:00' AND '2018-04-30', x.amount_base, 0))) DESC