oracle 当我将一个值视为所有其他值的出现时,对表中出现的值进行计数

n9vozmp4  于 2023-02-15  发布在  Oracle
关注(0)|答案(3)|浏览(116)

我有一个表,其中有一列(只是为了简化问题)的值为0-23或 *。我想统计每个值0-23的出现次数,但将 * 视为所有其他值的出现次数,例如:

column_name
-------------
3
4
5
6
7
*
4
4
3
*

我想得到这样的东西:

column_name | count
--------------------
1           | 2
2           | 2
3           | 4
4           | 5
5           | 3
6           | 3
7           | 3
.....

我尝试了不同的计数和“分组”方法,但总是得到非常奇怪的结果。基本上这里的主要问题是当我需要在所有其他组中有一个值时,如何计数行。

n53p2ov0

n53p2ov01#

您可以使用连续的WITH来执行此操作:
第一个计算 * 出现次数的人。
第二步是计算每个数字的出现次数。

with cte as (
  select count(1) as c
  from mytable
  where column_name = '*'
),
cte2 as (
  select column_name, count(1) as c
  from mytable, cte
  group by column_name
)
select column_name, cte.c + cte2.c
from cte2, cte;
puruo6ea

puruo6ea2#

您也可以使用嵌套语句,

SELECT ID,(count_ + (
    SELECT COUNT(ID) FROM sql_test_a
    WHERE ID = '*')) as count_ 
FROM (
   SELECT ID,COUNT(ID) as count_ 
   FROM sql_test_a WHERE ID != '*' GROUP BY ID);
6tr1vspr

6tr1vspr3#

您可以使用分析函数来计算值,其中 * 被替换为0到23之间的实际值:

SELECT      DISTINCT n.RN "COL_1", Count(REPLACE(t.COL_1, '*', n.RN)) OVER(Partition By n.RN) "CNT"
FROM        tbl t
INNER JOIN  ( Select To_Char(LEVEL - 1) "RN" From Dual Connect By LEVEL <=24  ) n ON(n.RN = REPLACE(t.COL_1, '*', n.RN))
WHERE       n.RN IN(SELECT COL_1 FROM tbl)
ORDER BY    To_Number(n.RN)

使用您的样本数据:

WITH
  tbl (COL_1) AS
    (
      Select '3' From Dual Union  All
      Select '4' From Dual Union  All
      Select '5' From Dual Union  All
      Select '6' From Dual Union  All
      Select '7' From Dual Union  All
      Select '*' From Dual Union  All
      Select '4' From Dual Union  All
      Select '4' From Dual Union  All
      Select '3' From Dual Union  All
      Select '*' From Dual Union  All
      Select '3' From Dual 
    )

...结果为:

COL_1                                           CNT
---------------------------------------- ----------
3                                                 5 
4                                                 5 
5                                                 3 
6                                                 3 
7                                                 3

...如果您排除Where子句,则将获得所有行(0 - 23),这些行的出现次数由REPLACE of * 用任意数字计算

COL_1                                           CNT
---------------------------------------- ----------
0                                                 2 
1                                                 2 
2                                                 2 
3                                                 5 
4                                                 5 
5                                                 3 
6                                                 3 
7                                                 3 
8                                                 2 
9                                                 2 
10                                                2 
11                                                2 
12                                                2 
13                                                2 
14                                                2 
15                                                2 
16                                                2 
17                                                2 
18                                                2 
19                                                2 
20                                                2 
21                                                2 
22                                                2 
23                                                2

相关问题