2012-04-13 55 views
1


需要幫助,顯示2行作爲合併列
製造這一形象來說明問題 謝謝你的幫助 enter image description here甲骨文整合兩行成列

+2

你爲什麼要解除規範你的數據庫? – Ben 2012-04-13 14:52:38

+0

謝謝,我需要這個信息的報告,我不改變數據庫 – user648026 2012-04-13 15:08:17

+0

是的,我正在使用Oracle(11G rel 2及以上) – user648026 2012-04-13 18:40:35

回答

3

如果你只需要2個城市那麼簡單的東西可能是使用解析函數:

select distinct studentname 
    , min(city) over (partition by studentname) as city1 
    , min(street1) over (partition by studentname) as street1 
    , case when min(city) over (partition by studentname) 
        <> nvl(max(city) over (partition by studentname), 'x') 
       then max(city) over (partition by studentname) end as city2 
    , case when min(street) over (partition by studentname) 
        <> nvl(max(street) over (partition by studentname), 'x') 
       then max(street) over (partition by studentname) end as street2 
    from my_table 

雖然我要補充一點,你可能不應該這樣做。如果學生有3個地址會發生什麼?

+0

雖然不知何故複雜,它的工作原理,謝謝你, – user648026 2012-04-16 12:48:51

2

我想你想這個方式是不正確的。

您應該返回「Ted」的2行,並使用您的視圖/報告代碼來處理輸出。如果您的需求發生變化,這樣會更容易,並且可以讓您更快地更改您的視圖/報告。