1
私はdbから複数のアカウントを持つユーザーを探しています。各ユーザーには、lastname属性とfirstname属性があります。Postgres:複数のアカウントを持つユーザーを選択してください
どのようにすれば、同じ名字と姓の等しいすべてのユーザーを一覧表示できますか?
lastname | firstname | number_of_accounts
Smith | John | 2
私はdbから複数のアカウントを持つユーザーを探しています。各ユーザーには、lastname属性とfirstname属性があります。Postgres:複数のアカウントを持つユーザーを選択してください
どのようにすれば、同じ名字と姓の等しいすべてのユーザーを一覧表示できますか?
lastname | firstname | number_of_accounts
Smith | John | 2
使用count(*)
とgroup by:
select lastname, firstname, count(*)
from my_table
group by lastname, firstname;
lastname | firstname | count
----------+-----------+-------
McDonald | Ronald | 1
Smith | John | 2
(2 rows)
あなたはhaving
句で結果フィルタできます:
例DATA
id | lastname | firstname
1 | Smith | John
2 | Smith | John
3 | McDonald | Ronald
select lastname, firstname, count(*)
from my_table
group by lastname, firstname
having count(*) > 1;
lastname | firstname | count
----------+-----------+-------
Smith | John | 2
(1 row)
あなたの質問に** [サンプルデータ](http://plaintexttools.github.io/plain-text-table/)とそのデータに基づいて予想される出力を追加してください。 [**フォーマットされたテキスト**](http://stackoverflow.com/help/formatting)、** **スクリーンショットはありません**(http://meta.stackoverflow.com/questions/285551/why-may -i-not-upload-images-of-code-on-so-asking-a-question/285557#285557)。 ** [編集] **あなたの質問 - コメントを投稿するコードや追加情報はありません**。 –