我有以下数据:
我想知道每列中每个值的存在频率。所以我的优先输出应该是这样的:
如果有人能帮助我,我真的很感激。谢谢!
答案 0 :(得分:1)
使用UNPIVOT
然后PIVOT
:
Oracle 11g R2架构设置:
CREATE TABLE table_name ( 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;
查询1 :
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 ) )
<强> Results 强>:
| 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 |
答案 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
注意:这与MT0的解决方案基本相同,但是无法解决和转动都采用了旧的方式&#34; (因为它们是在Oracle 11.1中引入PIVOT和UNPIVOT操作符之前完成的。)
答案 2 :(得分:0)
这是一个奇怪的表结构和/或任务。您可能想要考虑数据库设计。无论如何...
var app = angular.module('app', [])
.controller('ctrl', function ($scope) {
var data = {
name: 'Name of Item',
Description: '<div>Details: Here is Details</div><div>Type: Here is Type</div>'
}
$scope.data = data;
$scope.result = $scope.data.Description.split('</div>');
$scope.result = $scope.result[0].slice(0, -1); //take the first value from array, and slice last char, which is comma
$scope.result = $scope.result + '</div>' //insert div at the end
})