2017-02-03 54 views
0

我是SQL查詢中的新成員。我打算在HANA Express中加入2個表格,並獲得一個表格的兩列中的最大值並將其與另一個表格結合起來。下面是場景:獲取列的最大值並將它們與另一個表結合

Table A 
+-----+----------------+ 
| Key |  Value  | 
+-----+----------------+ 
| 1 | Value is 1  | 
| 2 | Value is 2  | 
| 3 | Value is 3  | 
+-----+----------------+ 

Table B 
+-----+----------------------------+------+ 
| Seq |   Timestamp   | Key | 
+-----+----------------------------+------+ 
| 500 | Feb 3, 2017 6:35:59.742 PM | 1 | 
| 501 | Feb 3, 2017 6:35:59.742 PM | 2 | 
| 502 | Feb 3, 2017 6:36:05.758 PM | 2 | 
| 503 | Feb 3, 2017 6:36:05.758 PM | 4 | 
| 504 | Feb 3, 2017 6:36:05.758 PM | 3 | 
| 505 | Feb 3, 2017 6:36:09.766 PM | 5 | 
+-----+----------------------------+------+ 

output table 
+-----+------------+--------+----------------------------+--------------------------------+ 
| Key | Value | MaxSeq |  LatestTimeStamp  |   ExecutionTime   | 
+-----+------------+--------+----------------------------+--------------------------------+ 
| 1 | Value is 1 | 505 | Feb 3, 2017 6:36:09.766 PM | (execution time of this query) | 
| 2 | Value is 2 | 505 | Feb 3, 2017 6:36:09.766 PM | (execution time of this query) | 
| 3 | Value is 3 | 505 | Feb 3, 2017 6:36:09.766 PM | (execution time of this query) | 
+-----+------------+--------+----------------------------+--------------------------------+ 

所以這裏的MaxSeq將表B中序列列的最大值和LatestTimeStamp是表B的時間戳列的最新時間戳和他們都將在連接表不變。執行時間從HANA中的Current_Timestamp函數計算得出。它甚至有可能嗎?

+0

和'ExecutionTime'?或者解釋它是如何計算的或從問題中移除的。 –

+0

謝謝。添加了執行時間的解釋。 –

回答

0

嘗試這種情況:

select a.Key, a.Value, 
(select max(Seq) from TableB) MaxSeq, 

(選擇最大值(時間戳)從表B)C LatestTimestamp,??? Executiontime 從表A

更妙的是:

select a.Key, a.Value, b.MaxSeq, b.LatestTimestamp , 
Current_Timestamp - b.LatestTimestamp Executiontime 
from TableA, 
(select max(Seq) MaxSeq, max(Timestamp) LatestTimestamp from TableB) b 
+0

謝謝,但此查詢正在爲每個鍵值生成最大seq。而不是表b中seq列的最大值,這將是一個常數 –

+1

我看到,您不希望每個鍵的最大值,只是表的最大值。不知道你爲什麼要這樣做,但我會相應地編輯我的答案。 – MikeS

+0

完美!萬分感謝! –

相關問題