Oracle SQL,一张大表将列和百分比分布分类

问题描述

我有一个大的垫子视图,其中,我想对A列进行分类。对于A列,我需要B列的百分比分布。 列A是字符串值,列B是数字(长度)。此外,对于此列C的操作,我仅需要有效值(WHERE column_C ='active'),并且我需要D列的另一种类别,它代表B列中特定类型的值(WHERE column_D IN(“ kind_1”, kind_2“))。

到目前为止,我只对A列进行分类,对B列和百分比进行计数。

SELECT column_A,COUNT(*) AS column_A_count,(COUNT(*) / (SELECT COUNT(*) FROM schema.table_matview WHERE column_D IN ('kind_1','kind_2')) * 100 AS Percentage1
FROM schema.table_matview 
WHERE column_D IN ('kind_1','kind_2')
GROUP BY column_A;

应如何显示:

column_A_categorized  |column_B_percentage_to_column_A_kindA  |
_____________________ |_______________________________        |
category_A            |13.4%                                  |
_____________________ |_______________________________        |
category_B            |6.9%                                   |
_____________________ |_______________________________        |
category_C            |60.1%                                  |
_____________________ |_______________________________        |
category_D            |19,6%                                  |
_____________________ |_______________________________        |

与kind_B在第二列中相同,但它不适合它 ,因此第三列是column_B_percentage_to_column_A_kindB。

column A has values like: Paris,Lyon,Nancy,Marseilles,Bordeaux
column B : (length of streets)
441
107015762
3254
3255
102953236
162
116
1252
1252
3
1824
5513
736
136
3
136
3554
6
482
731
72
6531
3252


column C: active,not active

column D: streettype like road/way/street/motorway/footway

解决方法

根据您的评论,您需要简单的条件聚合

SELECT column_A

   --  length of all streets passing the WHERE-condition,sum(column_B) AS all_street_length,sum(case when column_D = 'highway'  then column_B end) as highway_length,sum(case when column_D = 'motorway' then column_B end) as motorway_length,sum(case when column_D = 'highway'  then column_B end)
   / sum(column_B) * 100 AS highway_percentage,sum(case when column_D = 'motorway' then column_B end)
   / sum(column_B) * 100 AS motorway_percentage
FROM schema.table_matview 
WHERE column_C = 'active' -- street is still drivable
  -- AND column_D IN ('highway','motorway')
GROUP BY column_A;

这将返回基于 all 条街道的百分比,而不仅仅是高速公路/高速公路(highway_percentage + motorway_percentage小于100%)。如果您取消注释AND column_D IN ('highway','motorway'),则仅基于这些街道(highway_percentage + motorway_percentage总计为100%)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...