sql - 三列中的两列的 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
解决方案
我会按person_id
and分组ah_person_id
,并取最小的account_id
:
SELECT person_id, ah_person_id, MIN(account_id)
FROM mytable
GROUP BY person_id, ah_person_id
推荐阅读
- powershell - PowerShell - 递归提取特定文件夹的内容
- javascript - 自定义排序 JavaScript
- python - 如何使matplotlib图中的刺可见?
- unity3d - 如何在 Unity3d 中使用操纵杆更改 AR Foundation 相机旋转和位置
- r - 从一个数据帧循环行号以使用 R 中的逻辑操作创建新数据
- java - 在 Heroku CI 期间启动应用程序
- javascript - 尝试将 blob 上传到 Firebase 存储时出错(React Native)
- python - 在 Django 模板中显示模型中的图像
- javascript - 如何在从 API 响应设置值时避免覆盖谷歌表中的一行
- docker - 为什么有些命令显示输出为“正在运行
' 后跟 '删除中间容器 ',有些命令没有?