postgresql - 将列乘以一的不同值
问题描述
我有一个transactions
带有 : id_transaction
、credited_person
和列debited_person
的表和一个users
带有id_user
. 对于每个用户,我想要一个与他进行交易的所有人的列表(无论用户是借记还是贷记)
有人能帮助我吗 ?
解决方案
您可以加入users
,transactions
按用户分组并使用string_agg()
:
select u.user_id,
string_agg(distinct
case
when u.user_id = t.credited_person then t.debited_person
else t.credited_person
end,
','
)
from users u left join transactions t
on u.user_id in (t.credited_person, t.debited_person)
group by u.user_id
推荐阅读
- c++ - 在 std::qsort 的比较函数中处理错误情况
- ruby - IO.pipe 上的 Ruby readpartial 不会引发 EOFError
- python - 我不明白为什么会发生 SettingWithCopyWarning
- html - 用同样不起作用的 flex 替换不起作用的 float
- c - C11:如何快速将char数组转换为int,然后修改int和更新char数组
- uwp - 我应该使用哪种视觉状态自适应触发方法来跨多个设备和分辨率运行 uwp 应用程序?
- react-native - 黄色警告将在 React Native 中降低应用程序性能,其持续调用无限时间(即继续调用函数)
- python - 如何在 Django New User 上加载最新记录
- javascript - 需要在第一个结束 javascript 后启动第二个 firebase 查询
- batch-file - 如何在 Windows 10 中成功连接服务器端 SSH 后执行批处理文件