2014-02-12 68 views
0

試圖讓這個輸出Oracle 8i中:最小值最大值與標識 - 甲骨文

column1 |time 
________|_______ 
    ABC | 00:00:01 
    END | 00:00:03 
    123 | 00:00:04 
    END | 00:00:07 
    ABC | 00:00:08 
    END | 00:00:10 

與來自另一個查詢

column1 |time  |ID 
    ________|___________| 
     ABC | 00:00:01 | 1 
     ABC | 00:00:02 | 1 
     ABC | 00:00:03 | 1 
     123 | 00:00:04 | 1 
     123 | 00:00:05 | 1 
     123 | 00:00:06 | 1 
     123 | 00:00:07 | 1 
     ABC | 00:00:08 | 2 
     ABC | 00:00:09 | 2 
     ABC | 00:00:10 | 2 

此查詢獲取的最小值和最大值,而不考慮ID此輸出。

select (case when n.n = 1 then column1 else 'END' end) as column1, 
     (case when n.n = 1 then firsttime else lasttime end) as "time" 
from (select column1, min(time) as firsttime, max(time) as lasttime 
     from t 
     group by column1 
    ) t cross join 
    (select 1 as n from dual union all select 2 from dual) n 
order by column1, n.n; 

如何做同樣的事情,而不是獲得第一列和第一列的出現值,考慮ID以及?

+2

取代'GROUP BY column1'通過'GROUP BY列1,id'? –

+0

我想你想要't.firsttime,t.column1,n.n'的命令以及將相鄰值保持在一起? –

回答

0

您需要包括在邏輯id,無論是在group by並在order by

select (case when n.n = 1 then column1 else 'END' end) as column1, 
     (case when n.n = 1 then firsttime else lasttime end) as "time" 
from (select column1, id, min(time) as firsttime, max(time) as lasttime 
     from t 
     group by column1, id 
    ) t cross join 
    (select 1 as n from dual union all select 2 from dual) n 
order by id, column1, n.n; 
+0

因此,當我添加新的列將結果分組時,我將它們添加到組中並按順序排列?他們的正確順序是什麼?例如,如果我有另一個級別的分組值,我會添加列adter column1? –

+0

@Dalek。 。 。 'group by'列中的列順序沒有區別。 –