获取列的最大值并将其与另一个表连接

时间:2017-02-03 19:12:33

标签: sql hana

我是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中Seq列的最大值,而LatestTimeStamp是表B的timestamp列的最新时间戳,并且它们在连接表中都是常量。执行时间根据HANA中的Current_Timestamp函数计算得出。它甚至可能吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

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

(从表B中选择max(Timestamp))c LatestTimestamp,???执行时间处理时间     来自TableA

更好:

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