Sql 按列计数

Sql 按列计数,sql,oracle,count,Sql,Oracle,Count,我有以下数据: 我想知道每列中每一个值存在的频率。因此,我的首选输出应该如下所示: 如果有人能帮助我,我将非常感激。谢谢 这是一个奇怪的表结构和/或任务。您可能需要仔细考虑数据库设计。无论如何 select num, coalesce(agga.cnt, 0) as a, coalesce(aggb.cnt, 0) as b, coalesce(aggc.cnt, 0) as c, coalesce(aggd.cnt, 0) as d from (s

我有以下数据:

我想知道每列中每一个值存在的频率。因此,我的首选输出应该如下所示:


如果有人能帮助我,我将非常感激。谢谢

这是一个奇怪的表结构和/或任务。您可能需要仔细考虑数据库设计。无论如何

select 
  num, 
  coalesce(agga.cnt, 0) as a,
  coalesce(aggb.cnt, 0) as b,
  coalesce(aggc.cnt, 0) as c,
  coalesce(aggd.cnt, 0) as d
from            (select a as num, count(*) as cnt from mytable group by a) agga
full outer join (select b as num, count(*) as cnt from mytable group by b) aggb using(num)
full outer join (select c as num, count(*) as cnt from mytable group by c) aggc using(num)
full outer join (select d as num, count(*) as cnt from mytable group by d) aggd using(num)
order by num;

这是一个奇怪的表结构和/或任务。您可能需要仔细考虑数据库设计。无论如何

select 
  num, 
  coalesce(agga.cnt, 0) as a,
  coalesce(aggb.cnt, 0) as b,
  coalesce(aggc.cnt, 0) as c,
  coalesce(aggd.cnt, 0) as d
from            (select a as num, count(*) as cnt from mytable group by a) agga
full outer join (select b as num, count(*) as cnt from mytable group by b) aggb using(num)
full outer join (select c as num, count(*) as cnt from mytable group by c) aggc using(num)
full outer join (select d as num, count(*) as cnt from mytable group by d) aggd using(num)
order by num;
使用UNPIVOT然后旋转:

Oracle 11g R2架构设置:

问题1:

:

使用UNPIVOT然后旋转:

Oracle 11g R2架构设置:

问题1:

:

注意:这基本上与MT0的解决方案相同,但取消PIVOT和PIVOT都是按照旧方法进行的,就像在Oracle 11.1中引入PIVOT和UNPIVOT运算符之前一样


注意:这基本上与MT0的解决方案相同,但取消PIVOT和PIVOT都是按照旧方法进行的,就像在Oracle 11.1中引入PIVOT和UNPIVOT运算符之前一样。

您使用的是什么RDBMS?请贴上标签。还有,到目前为止你试过什么?显示您的努力。请粘贴文本,而不是图像。你尝试了什么?你被困在哪里了?我还没有尝试太多,因为我没有一个真正的俱乐部。我有多个列,所有列中的数字都在1到5之间。我想制作一个矩阵,显示每个数字在每个列中出现的频率,并再次显示:您使用的是什么DBMS?神谕MySQL?SQL Server?PostgreSQL。。。您正在使用什么RDBMS?请贴上标签。还有,到目前为止你试过什么?显示您的努力。请粘贴文本,而不是图像。你尝试了什么?你被困在哪里了?我还没有尝试太多,因为我没有一个真正的俱乐部。我有多个列,所有列中的数字都在1到5之间。我想制作一个矩阵,显示每个数字在每个列中出现的频率,并再次显示:您使用的是什么DBMS?神谕MySQL?SQL Server?PostgreSQL。。。与UNPIVOT/PIVOT相比,此方法的最大区别在于,您需要知道使用此方法的表中值的数量/范围,以便@MT0-Right-事实上,我的注释并不完全正确,因为我实际上没有UNPIVOT,我仍然保留原始行。与UNPIVOT/PIVOT相比,最大的区别是您需要知道使用此方法的表中值的数量/范围,以便@MT0-Right-事实上我的注释并不完全正确,因为我没有真正取消PIVOT,所以我仍然保留原始行。
SELECT *
FROM   table_name
UNPIVOT( value FOR name IN ( A, B, C, D ) )
PIVOT  ( COUNT(1) FOR name IN ( 'A' AS A, 'B' AS B, 'C' AS C, 'D' AS D ) )
| VALUE | A | B | C | D |
|-------|---|---|---|---|
|     1 | 2 | 1 | 1 | 0 |
|     2 | 1 | 2 | 3 | 2 |
|     4 | 1 | 1 | 0 | 1 |
|     5 | 1 | 1 | 1 | 2 |
|     3 | 1 | 1 | 1 | 1 |
with
     inputs ( a, b, c, d ) as (
       select 1, 2, 1, 2 from dual union all
       select 1, 2, 2, 2 from dual union all
       select 2, 1, 3, 3 from dual union all
       select 3, 3, 2, 4 from dual union all
       select 4, 4, 2, 5 from dual union all
       select 5, 5, 5, 5 from dual
     )
-- End of simulated inputs (for testing only, not part of the solution).
-- SQL query begins BELOW THIS LINE. Use your actual table and column names.
select grade,
       count(case when a = grade then 0 end) as a,
       count(case when b = grade then 0 end) as b,
       count(case when c = grade then 0 end) as c,
       count(case when d = grade then 0 end) as d
from   inputs cross join (select level as grade from dual connect by level <= 5)
group by grade
order by grade
;

     GRADE          A          B          C          D
---------- ---------- ---------- ---------- ----------
         1          2          1          1          0
         2          1          2          3          2
         3          1          1          1          1
         4          1          1          0          1
         5          1          1          1          2