3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

100日でSQLの達人になる@LeetCode! Day61 <UNION ALLしたテーブルからの集計>

Posted at

602. Friend Requests II: Who Has the Most Friends (問題レベル: Medium)要課金

今日の問題はこれ。
友達申請をした人のIDと友達申請をされた人のIDが一行になったテーブルから最も友達が多い人のIDとその友達の数をリストする問題。

LeetCodeの問題に取り組むのが久しぶりだったのでリハビリに選んだ問題。

最終的に提出した回答はこちら。

SQL Server
SELECT TOP 1 tbl.id, COUNT(tbl.id) AS num
    FROM (
	    SELECT requester_id AS id FROM requestaccepted
		    UNION ALL
		SELECT accepter_id AS id FROM requestaccepted) tbl
    GROUP BY tbl.id
    ORDER BY COUNT(tbl.id) DESC

ポイントはUNION ALLしたテーブルを集計する方法としました。

  • LeetCodeの問題は、MS SQL Serverで解いています。
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?