首页 > 解决方案 > 三列中的两列的 DISTINCT

问题描述

当一个 Person_id 和一个 ah_person_id 的唯一 account_id 值时,我想要 DISTINCT/FirstValue,不管是哪一个,但有时 Person_ID 也具有不同的 Ah_person_id。示例表

Person   AH_PERSON   ACCOUNT_ID
A         1           22
B         2           23
B         2           24 
C         3           25
C         4           26

我想要什么 预期结果

Person   AH_PERSON   ACCOUNT_ID
A         1           22
B         2           23
C         3           25
C         4           26

我尝试使用 Group by 和 DISTINCT,但没有得到我想要的结果,谁能举个例子?谢谢你

SELECT DISTINCT person_id,
(SELECT ah_person_id
  FROM w_accnt
WHERE id = My_Table.account_id) AH_PERSON ,
account_id 
FROM My_Table 

标签: sqloracleselect

解决方案


我会按person_idand分组ah_person_id,并取最小的account_id

SELECT   person_id, ah_person_id, MIN(account_id)
FROM     mytable
GROUP BY person_id, ah_person_id

推荐阅读