Skip to content

Instantly share code, notes, and snippets.

@JorgeMichelena
Created September 19, 2022 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JorgeMichelena/70cdf57a6478cd7a5b1a775faef468e0 to your computer and use it in GitHub Desktop.
Save JorgeMichelena/70cdf57a6478cd7a5b1a775faef468e0 to your computer and use it in GitHub Desktop.
SELECT "targets_user"."id", "targets_user"."password",
      ...
      "targets_user"."date_of_birth", "targets_user"."zodiac_sign",
      (COUNT(DISTINCT "targets_match"."id") + COUNT(DISTINCT T4."id")) AS "match_count"
FROM "targets_user"
LEFT OUTER JOIN "targets_target"
    ON ("targets_user"."id" = "targets_target"."user_id")
    LEFT OUTER JOIN "targets_match"
        ON ("targets_target"."id" = "targets_match"."target_1_id")
        LEFT OUTER JOIN "targets_match" T4
            ON ("targets_target"."id" = T4."target_2_id")
GROUP BY "targets_user"."id"
ORDER BY "match_count" DESC LIMIT 21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment