From 0c4fe286a08ff3b6e6618254a66557d77600de8f Mon Sep 17 00:00:00 2001 From: Adnan Kattekaden <71538497+adnankattekaden@users.noreply.github.com> Date: Wed, 8 Nov 2023 23:58:08 +0530 Subject: [PATCH] Update top100_view.py --- api/top100_coders/top100_view.py | 56 ++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/api/top100_coders/top100_view.py b/api/top100_coders/top100_view.py index c160645c..86b8f344 100644 --- a/api/top100_coders/top100_view.py +++ b/api/top100_coders/top100_view.py @@ -7,23 +7,45 @@ class Leaderboard(APIView): def get(self, request): query =""" - SELECT u.first_name, u.last_name, SUM(kal.karma) AS total_karma, org.title as org, org.dis, org.state, u.profile_pic,MAX(kal.created_at) as time_ - FROM karma_activity_log AS kal - INNER JOIN user AS u ON kal.user_id = u.id - INNER JOIN task_list AS tl ON tl.id = kal.task_id - LEFT JOIN ( - SELECT uol.user_id, org.id, org.title as title, d.name dis, s.name state - FROM user_organization_link AS uol - INNER JOIN organization AS org ON org.id = uol.org_id - LEFT JOIN district AS d ON d.id = org.district_id - LEFT JOIN zone AS z ON z.id = d.zone_id - LEFT JOIN state AS s ON s.id = z.state_id - GROUP BY uol.user_id - ) as org on org.user_id = u.id - WHERE tl.event = 'TOP100' AND kal.appraiser_approved = TRUE - GROUP BY u.id - ORDER BY total_karma DESC, time_ - LIMIT 100; +SELECT + u.id, + u.first_name, + u.last_name, + SUM(kal.karma) AS total_karma, + org.title AS org, + COALESCE(org.dis, d.name) AS dis, + COALESCE(org.state, s.name) AS state, + u.profile_pic, + MAX(kal.created_at) AS time_, + kal.created_at AS created_at +FROM karma_activity_log AS kal +INNER JOIN user AS u ON kal.user_id = u.id +INNER JOIN task_list AS tl ON tl.id = kal.task_id +LEFT JOIN ( + SELECT + uol.user_id, + org.id, + org.title AS title, + d.name dis, + s.name state + FROM user_organization_link AS uol + INNER JOIN organization AS org ON org.id = uol.org_id + LEFT JOIN district AS d ON d.id = org.district_id + LEFT JOIN zone AS z ON z.id = d.zone_id + LEFT JOIN state AS s ON s.id = z.state_id + GROUP BY uol.user_id +) AS org ON org.user_id = u.id +LEFT JOIN district AS d ON d.id = u.district_id +LEFT JOIN zone AS z ON d.zone_id = z.id +LEFT JOIN state AS s ON z.state_id = s.id +WHERE + tl.event = 'TOP100' AND + kal.appraiser_approved = TRUE + AND u.id IN (select user_id from karma_activity_log as kal +INNER JOIN task_list AS tl ON tl.id = kal.task_id +WHERE tl.hashtag = '#thc-realworld-problem-proposal') +GROUP BY u.id +ORDER BY total_karma DESC, time_; """ query2 = """