3

我不太喜欢 SQL 和 Apex,但我需要一个声明,非常感谢您在这方面的帮助。Apex 饼图的语法是这样的:

SELECT link, label, value

我的桌子看起来像这些简单的草图:

+------+-----------+---------+
| ID   | Company   | Item    |
+------+-----------+---------+
| 1    | AAA       |Some     |
| 2    | BB        |Stuff    |
| 3    | BB        |Not      |
| 4    | CCCC      |Important|
| 5    | AAA       |For      |
| 6    | DDDDD     |Question?|
+------+-----------+---------+

我想显示公司的百分比。

问题:所有少于 5 个项目的公司都应合并为一列“其他”。对我来说,困难在于合并“不重要”的公司。

到目前为止,我的陈述是这样的:

SELECT null link, 
company label, 
COUNT(ID) value FROM table HAVING COUNT(ID) > 5 GROUP BY company

这是一个精彩的图表草图。:D在此处输入图像描述

谢谢你的想法!

4

2 回答 2

1

我面前没有 SQL Developer,但这个(或接近的变体)应该适合你:

WITH company_count
  AS (
      SELECT CASE
                WHEN count(*) < 5
                THEN 'Other'
                ELSE company
              END AS company_name,
             id
        FROM tablename
      ),
      company_group
  AS (
      SELECT company_name,
             count(id) item_count
        FROM company_count
       GROUP BY company_name
     )
SELECT NULL AS link,
       company_name AS label,
       item_count AS value
  FROM company_group;

希望能帮助到你!

于 2013-09-19T20:44:18.577 回答
0

好的,伙计们,我找到了我的用例的答案。它与奥利斯的答案非常相似。再次感谢您的帮助!

 WITH sq1 AS (SELECT company, COUNT (*) AS count FROM 
 (SELECT CASE WHEN COUNT (*) OVER  (Partition By company) > 5 THEN company
 ELSE 'other' END AS company, id FROM table) GROUP BY company)

 SELECT null link, company label, count value FROM sq1 ORDER BY count desc
于 2013-09-25T21:10:48.027 回答