我的选择查询:
SELECT CUSTID, EVENT_DTM, CURRENT_LOC, FROM_LOC FROM TABLE
返回以下结果:
+--------+----------------------------+-------------+----------+
| CUSTID | EVENT_DTM | CURRENT_LOC | FROM_LOC |
+--------+----------------------------+-------------+----------+
| 1 | 2000-02-17 01:33:00.000000 | FRONTDOOR | |
+--------+----------------------------+-------------+----------+
| 1 | 2000-02-17 03:50:00.000000 | FRONTDOOR | |
+--------+----------------------------+-------------+----------+
| 1 | 2000-02-17 04:32:00.000000 | FOYER | |
+--------+----------------------------+-------------+----------+
| 1 | 2000-02-17 05:42:00.000000 | TEMP-ONE | FOYER |
+--------+----------------------------+-------------+----------+
| 1 | 2000-02-17 05:58:00.000000 | KITCHEN | TEMP-ONE |
+--------+----------------------------+-------------+----------+
| 1 | 2000-02-17 10:43:00.000000 | POOL | KITCHEN |
+--------+----------------------------+-------------+----------+
| 1 | 2000-02-17 13:01:00.000000 | BEDROOM | POOL |
+--------+----------------------------+-------------+----------+
| 1 | 2000-02-18 14:00:00.000000 | BEDROOM | |
+--------+----------------------------+-------------+----------+
我想编写一条select语句,以便当CURRENT_LOC = TEMP-ONE并且有一个对应的FROM_LOC = TEMP-ONE时,将合并行,并且查询结果将是:
+--------+----------------------------+-------------+----------+
| CUSTID | EVENT_DTM | CURRENT_LOC | FROM_LOC |
+--------+----------------------------+-------------+----------+
| 1 | 2000-02-17 01:33:00.000000 | FRONTDOOR | |
+--------+----------------------------+-------------+----------+
| 1 | 2000-02-17 03:50:00.000000 | FRONTDOOR | |
+--------+----------------------------+-------------+----------+
| 1 | 2000-02-17 04:32:00.000000 | FOYER | |
+--------+----------------------------+-------------+----------+
| 1 | 2000-02-17 05:58:00.000000 | KITCHEN | FOYER |
+--------+----------------------------+-------------+----------+
| 1 | 2000-02-17 10:43:00.000000 | POOL | KITCHEN |
+--------+----------------------------+-------------+----------+
| 1 | 2000-02-17 13:01:00.000000 | BEDROOM | POOL |
+--------+----------------------------+-------------+----------+
| 1 | 2000-02-18 14:00:00.000000 | BEDROOM | |
+--------+----------------------------+-------------+----------+
是否存在SQL解决此问题的方法?
谢谢, 马特
答案 0 :(得分:0)
以下查询返回您提供的结果。
WITH MYTAB (CUSTID, EVENT_DTM, CURRENT_LOC, FROM_LOC) AS
(
VALUES
(1, '2000-02-17 01:33:00.000000', 'FRONTDOOR', '')
, (1, '2000-02-17 03:50:00.000000', 'FRONTDOOR', '')
, (1, '2000-02-17 04:32:00.000000', 'FOYER', '')
, (1, '2000-02-17 05:42:00.000000', 'TEMP-ONE', 'FOYER ')
, (1, '2000-02-17 05:58:00.000000', 'KITCHEN', 'TEMP-ONE ')
, (1, '2000-02-17 10:43:00.000000', 'POOL', 'KITCHEN ')
, (1, '2000-02-17 13:01:00.000000', 'BEDROOM', 'POOL ')
, (1, '2000-02-18 14:00:00.000000', 'BEDROOM', '')
)
SELECT A.CUSTID, A.EVENT_DTM, COALESCE(B.CURRENT_LOC, A.CURRENT_LOC) CURRENT_LOC, A.FROM_LOC
FROM MYTAB A
LEFT JOIN MYTAB B ON B.FROM_LOC = A.CURRENT_LOC AND A.CURRENT_LOC = 'TEMP-ONE'
WHERE A.FROM_LOC <> 'TEMP-ONE';