Oracle SQL-Query解析

时间:2014-04-09 03:05:15

标签: sql oracle

我有一张这样的表:

eno   name        loc       flag
1    penchal      pune      old
2    penchal      mumbai    current
3    srinivas     hyd       curent
4    mahesh       chenai    current

我想这样说:

Name       oldlocation   newlocation
penchal    pune          mummbai
srinivas   null          hyd
mahesh     null          chennai

请你帮我解决一下如何在oracle sql中为这个场景编写查询;
寻找回复

2 个答案:

答案 0 :(得分:1)

SELECT  Name,
        MAX(CASE WHEN flag = 'old' THEN loc ELSE NULL END) AS oldlocation,
        MAX(CASE WHEN flag = 'current' THEN loc ELSE NULL END) AS newlocation
FROM    tableName
GROUP   BY Name

答案 1 :(得分:0)

SELECT name
     , MAX(DECODE(flag,'old',loc))     oldlocation
     , MAX(DECODE(flag,'current',loc)) newlocation
FROM   tableName
GROUP BY name