Oracle Transform rows into column 合并重复行
Oracle Transform rows into column and combined duplicate rows
我是 Oracle 数据库的新手。我不确定是否可以这样做。基本上我有一个查询:
SELECT location,properties,value FROM table_usa
那个 returns 这个输出:
不知何故,我想将纬度和经度转换成它们自己的列,并合并最初的重复行。期望的输出:
这可能吗?
在 Oracle 11g 中,您可以尝试 PIVOT(参见 http://www.oracle.com/technetwork/articles/sql/11g-pivot-097235.html)。
在以前的版本中,这应该有效:
select location,
max(decode(properties, 'Latitude', value, null)) latitude,
max(decode(properties, 'Longitude', value, null)) longitude
from table_usa
group by location
在 oracle
11g(及更高版本)pivot
是此任务的最通用 工具。
The pivot_clause lets you write cross-tabulation queries that rotate
rows into columns, aggregating data in the process of the rotation.
来源
LOCATION |PROPERTIES | VALUE
-----------|-----------|--------
Texas | Latitude | 21.391
Texas | Longitude | 54.12
Detroit | Latitude | 24.23
Detroit | Longitude | 54.23
New York | Latitude | 24.239
New York | Longitude | 55.5
枢轴 Select
select *
from (select Location, Properties, Value
from table_usa)
pivot(
max(Value)
for Properties in ('Latitude' AS LATITUDE,
'Longitude' AS LONGITUDE)
)
order by LATITUDE;
结果
LOCATION | LATITUDE |LONGITUDE
------------|-----------|---------
Texas | 21.391 |54.12
Detroit | 24.23 |54.23
New York | 24.239 |55.5
我是 Oracle 数据库的新手。我不确定是否可以这样做。基本上我有一个查询:
SELECT location,properties,value FROM table_usa
那个 returns 这个输出:
不知何故,我想将纬度和经度转换成它们自己的列,并合并最初的重复行。期望的输出:
这可能吗?
在 Oracle 11g 中,您可以尝试 PIVOT(参见 http://www.oracle.com/technetwork/articles/sql/11g-pivot-097235.html)。
在以前的版本中,这应该有效:
select location,
max(decode(properties, 'Latitude', value, null)) latitude,
max(decode(properties, 'Longitude', value, null)) longitude
from table_usa
group by location
在 oracle
11g(及更高版本)pivot
是此任务的最通用 工具。
The pivot_clause lets you write cross-tabulation queries that rotate rows into columns, aggregating data in the process of the rotation.
来源
LOCATION |PROPERTIES | VALUE
-----------|-----------|--------
Texas | Latitude | 21.391
Texas | Longitude | 54.12
Detroit | Latitude | 24.23
Detroit | Longitude | 54.23
New York | Latitude | 24.239
New York | Longitude | 55.5
枢轴 Select
select *
from (select Location, Properties, Value
from table_usa)
pivot(
max(Value)
for Properties in ('Latitude' AS LATITUDE,
'Longitude' AS LONGITUDE)
)
order by LATITUDE;
结果
LOCATION | LATITUDE |LONGITUDE
------------|-----------|---------
Texas | 21.391 |54.12
Detroit | 24.23 |54.23
New York | 24.239 |55.5