MYSql Top 10和其他总计

时间:2014-02-17 17:36:40

标签: mysql

我有这个查询工作正常,但我只需要前10名供应商。

然后我需要在“所有其他人”行中记录剩下的所有内容。

如何使用LIMIT 10, 18446744073709551615

进行单独查询,如何执行此操作
SELECT VENDOR_fullname,SUM(POTENTIAL_RECOVERY)
FROM COMPLETE
GROUP BY VENDOR_fullname
ORDER BY SUM(POTENTIAL_RECOVERY) DESC;

2 个答案:

答案 0 :(得分:2)

为了记录,我同意@ DamienBlack的评论,但如果必须在一个查询中完成UNION ALL可以做到这一点:

(
  SELECT VENDOR_fullname as name, SUM(POTENTIAL_RECOVERY) as recovery
  FROM COMPLETE
  GROUP BY VENDOR_fullname
  ORDER BY SUM(POTENTIAL_RECOVERY) DESC
  LIMIT 10
)
UNION ALL
(
  SELECT 'All others' as name, SUM(subtotal) as recovery
  FROM 
  (
    SELECT SUM(POTENTIAL_RECOVERY)
    FROM COMPLETE
    GROUP BY VENDOR_fullname
    ORDER BY SUM(POTENTIAL_RECOVERY) DESC
    LIMIT 10, 18446744073709551615
  ) as subtotal;
)

答案 1 :(得分:0)

您可以使用union:

SELECT VENDOR_fullname as anme,SUM(POTENTIAL_RECOVERY)
FROM COMPLETE
GROUP BY VENDOR_fullname
ORDER BY SUM(POTENTIAL_RECOVERY) DESC
LIMIT 10

UNION ALL

SELECT 'All Others' as name,SUM(POTENTIAL_RECOVERY)
FROM COMPLETE
ORDER BY SUM(POTENTIAL_RECOVERY) DESC
LIMIT 10, 18446744073709551615