我该怎么做才是postgres SQL 我的下表给出了每组的所有帐户
account | groupName
1 | group1
2 | group1
2nyc | group1
2lon | group2
3 | group2
4 | group2
基于这些组,我需要获取帐户并将它们传递给另一个sql。 我需要所有小组的结果。
到目前为止,我尝试过的是
WITH main_data AS (
SELECT
account, groupName
FROM
accountMapping
WHERE
groupName IS NOT NULL
GROUP BY account, groupName
)
SELECT * FROM results_table where account in (SELECT account FROM main_data);
我需要它以便逻辑上这应该与执行此操作相同
SELECT * FROM results_table WHERE account in (1, 2,2nyc);
SELECT * FROM results_table WHERE account in (3, 4)
......等等
答案 0 :(得分:1)
这是你想要的吗?
SELECT rt.*
FROM results_table
WHERE rt.account IN (SELECT am.account
FROM accountMapping am
WHERE am.groupName = $groupName
);