TRAIN_ID TRAIN_NO SRC_DATE COACH_ID ASSIGN_COACH_FLAG USER_ID DATE_ADDED CH_NUMBER USER_TYPE
-------- -------- ---------- -------- ----------------- ------- ----------------------- --------- ---------
6442 12014 2017-05-14 - A psaini 2017-05-16 15:20:18.262 1 TS
6442 12014 2017-05-14 C1 T psaini 2017-05-16 15:20:18.326 1 TS
6442 12014 2017-05-14 C10 T psaini 2017-05-16 15:20:18.33 1 TS
6442 12014 2017-05-14 C11 T psaini 2017-05-16 15:20:18.334 1 TS
6442 12014 2017-05-14 C12 T psaini 2017-05-16 15:20:18.337 1 TS
6442 12014 2017-05-14 C13 T psaini 2017-05-16 15:20:18.341 1 TS
6442 12014 2017-05-14 C14 T psaini 2017-05-16 15:20:18.345 1 TS
6442 12014 2017-05-14 C2 T psaini 2017-05-16 15:20:18.349 1 TS
6442 12014 2017-05-14 C3 T psaini 2017-05-16 15:20:18.353 1 TS
6442 12014 2017-05-14 C4 T psaini 2017-05-16 15:20:18.357 1 TS
6442 12014 2017-05-14 C5 T psaini 2017-05-16 15:20:18.361 1 TS
6442 12014 2017-05-14 C6 T psaini 2017-05-16 15:20:18.364 1 TS
6442 12014 2017-05-14 C7 T psaini 2017-05-16 15:20:18.368 1 TS
6442 12014 2017-05-14 C8 T psaini 2017-05-16 15:20:18.372 1 TS
6442 12014 2017-05-14 C9 T psaini 2017-05-16 15:20:18.376 1 TS
6442 12014 2017-05-14 E1 T psaini 2017-05-16 15:20:18.379 1 TS
6442 12014 2017-05-14 E2 T psaini 2017-05-16 15:20:18.383 1 TS
这是表数据。
结果我想要的是
TRAIN_NO COACH_ID -
12014 C1
C10
C11
C12
C13
C14
C2
C3
C4
C5
C6
C7
C8
C9
E2
查询我正在使用
select distinct train_no, coach_id
from table
请帮助我,为什么结果不符合要求。
答案 0 :(得分:0)
select * from traindetails;
输出:
TRAINID COACHNAMES
12014 c1
12014 c2
12014 c3
12014 c4
12014 c5
12014 c6
12014 c7
12014 c8
12014 c9
12014 c10
根据您的要求,查询将如下:
SELECT a.COACHNAMES as coah_names ,
CASE
WHEN a.rnk = 1 THEN a.TRAINID
ELSE NULL
END AS TRAIN_ID
FROM (
SELECT a.*,
RANK() OVER(PARTITION BY a.TRAINID ORDER BY a.COACHNAMES ) rnk
FROM traindetails a
) a
输出如下:
COAH_NAMES TRAIN_ID
c1 12014
c10
c2
c3
c4
c5
c6
c7
c8
c9