【问题标题】:How to count each item in Oracle?如何计算Oracle中的每个项目?
【发布时间】:2020-12-27 21:48:55
【问题描述】:

帮助,这是我的查询

SELECT distinct id, trim(regexp_substr(str, '[^,]+', 1, level)) str 
FROM (SELECT id, REGEXP_REPLACE(to_char(advantages1), '"|\[|\]', '') str 
      FROM feedbacks) t
CONNECT BY instr(str, ',', 1, level - 1) > 0
order by str

结果如下: result

所以,我想创建另一列来计算 str 中每个数字的结果。例如

STR           TOTAL
110             1
111             2
112             2
113             4
114             1

我该怎么做?

【问题讨论】:

  • 请提供样本数据和期望的结果。
  • 我在描述中给出了一个示例数据
  • 。 .不,你没有。我不知道feedbacks 长什么样。

标签: sql regex oracle count


【解决方案1】:

如果我没听错,你可以使用聚合:

select trim(regexp_substr(str, '[^,]+', 1, level)) str , count(distinct id) total
from (select id, regexp_replace(to_char(advantages1), '"|\[|\]', '') str from feedbacks) t
connect by instr(str, ',', 1, level - 1) > 0
group by trim(regexp_substr(str, '[^,]+', 1, level))
order by str

【讨论】:

    【解决方案2】:

    如果您只想在当前 SELECT 列表中添加一列,则需要一个 COUNT() OVER() 分析函数,但该列的值会针对每个 str 值重复。

    在查看预期结果集时,您希望清楚地计算当前查询的结果。直接的方法是嵌套这个查询:

    SELECT str, COUNT(*) AS total
      FROM ( <your_current_query_without_order_by_part> )
     GROUP BY str
     ORDER BY TO_NUMBER(str)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-13
      • 2011-10-16
      • 2011-08-07
      • 2018-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多