2017-12-16 3 views
1

私はdbから複数のアカウントを持つユーザーを探しています。各ユーザーには、lastname属性とfirstname属性があります。Postgres:複数のアカウントを持つユーザーを選択してください

どのようにすれば、同じ名字と姓の等しいすべてのユーザーを一覧表示できますか?

lastname | firstname | number_of_accounts 
Smith | John  | 2 
+0

あなたの質問に** [サンプルデータ](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)。 ** [編集] **あなたの質問 - コメントを投稿するコードや追加情報はありません**。 –

答えて

1

使用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) 
関連する問題