Pivot on Oracle 10g(以 Oracle 10g 为中心)
问题描述
我正在使用 oracle 10g.我有一个临时表 TEMP.
I am using oracle 10g. I have a temp table TEMP.
TEMP 的结构如下:-
TEMP has following structure:-
我需要一个查询来显示所有类型都有列名,类型可以有任何值,如 T1, T2,..Tn
并且结果将是:-
I need a query which will show all types has column names,and types can have any value like T1, T2,..Tn
and result will be like:-
和 User 列将显示所有用户,T1, T2
列将显示类型的总数.
and User column will show all the users and T1, T2
column will show total count of types.
推荐答案
在 Oracle 10g 中,没有 PIVOT
函数,但您可以使用带有 CASE
:
In Oracle 10g, there was no PIVOT
function but you can replicate it using an aggregate with a CASE
:
参见 SQL Fiddle with Demo
如果你有 Oracle 11g+ 那么你可以使用 PIVOT
函数:
If you have Oracle 11g+ then you can use the PIVOT
function:
参见 SQL Fiddle with Demo
如果您有未知数量的值要转换,那么您可以创建一个过程来生成它的动态版本:
If you have an unknown number of values to transform, then you can create a procedure to generate a dynamic version of this:
然后执行代码:
所有版本的结果都是一样的:
The result for all versions is the same:
根据您的评论,如果您想要 Total
字段,最简单的方法是将查询放在另一个 SELECT
中,类似于:
Based on your comment if you want a Total
field, the easiest way is to place the query inside of another SELECT
similar to this:
参见 SQL Fiddle with Demo
这篇关于以 Oracle 10g 为中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!