有人可以帮我查询这个复杂的情况吗?
我有以下3个表格。
表:LOCATION
ID LOCATION_ID START_DT END_DT
21234 123 19-JUL-17 (null)
12345 345 19-JUL-17 (null)
12345 456 19-JUL-17 19-DEC-17
12345 567 19-JUL-16 (null)
12345 678 19-JUL-16 (null)
12345 789 19-JUL-17 (null)
12345 890 19-JUL-17 (null)
23456 901 07-JAN-18 (null)
23456 902 19-JAN-17 (null)
表:CONTACTGROUP
LOCATION_ID METHOD_ID
123 9999
345 8888
456 7777
567 6666
678 5555
789 4444
890 3333
901 2222
902 1111
表:CONTACTMETHOD
METHOD_ID REF_NUM METHOD_TYPE
9999 24856584 CELL PHONE
8888 64896572 HOME PHONE
7777 65948725 HOME PHONE
6666 36564852 HOME PHONE
5555 abc@xyz.com PRIMARY EMAIL
4444 cde@xyz.com PRIMARY EMAIL
3333 45789658 ALTERNATE PHONE
2222 86594758 HOME PHONE
1111 59857468 HOME PHONE
预期结果:
ID LOCATION_ID REF_NUM METHOD_TYPE START_DT END_DT
12345 567 36564852 HOME PHONE 19-JUL-16 (null)
12345 678 abc@xyz.com PRIMARY EMAIL 19-JUL-16 (null)
23456 902 59857468 HOME PHONE 19-JAN-17 (null)
标准:
返回
的字段我在某种程度上一直在尝试这个。
SELECT L.id,
L.location_id,
CM.ref_num,
CM.method_type,
L.start_dt,
L.end_dt
FROM location L
JOIN contactgroup CG
ON CG.location_id = L.location_id
JOIN contactmethod CM
ON CM.method_id = CG.method_id
WHERE L.end_dt IS NULL
GROUP BY L.id,
L.location_id,
CM.ref_num,
CM.method_type,
L.start_dt,
L.end_dt
HAVING Count(*) > 1
答案 0 :(得分:1)
单独使用group by和count子句无法实现此目的。您将不得不使用子查询。 另外,纯粹从提交的数据中,可以做出假设。 ref_num在这里是否相关?因为你在ref_num中拥有所有唯一值。
答案 1 :(得分:1)
可以按如下方式实现所需的结果 -
SELECT
ID,LOCATION_ID,REF_NUM,METHOD_TYPE,START_DT,END_DT
FROM
(
SELECT
A.* ,
DENSE_RANK() OVER (PARTITION BY ID, METHOD_TYPE ORDER BY START_DT DESC) AS RNK
FROM
(
SELECT
L.ID,L.LOCATION_ID,CM.REF_NUM,CM.METHOD_TYPE,L.START_DT,L.END_DT
FROM
LOCATION L, CONTACTMETHOD CM, CONTACTGROUP CG
WHERE
L.LOCATION_ID = CG.LOCATION_ID
AND CG.METHOD_ID = CM.METHOD_ID
AND L.END_DT IS NULL
) A
)
WHERE RNK = 2
;
<强>结果: - 强>
ID LOCATION_ID REF_NUM METHOD_TYPE START_DT END_DT
---------- ----------- -------------------- -------------------- --------- ---------
12345 567 36564852 HOMEPHONE 19-JUL-16
12345 678 abc@xyz.com PRIMARYEMAIL 19-JUL-16
23456 902 59857468 HOMEPHONE 19-JAN-17