Last active
July 11, 2018 15:50
-
-
Save fengb/0a492e36fc22f149cfba56b09b04417c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Project.joins( | |
activities: Activity.active.group(:project_id).select(:project_id, 'COUNT(*) as "count"') | |
).where(activities: { count: [1..2, 3..4] }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT * | |
FROM projects | |
JOIN (SELECT project_id, COUNT(*) as "count" | |
FROM activities | |
WHERE active = 't' | |
GROUP BY project_id | |
) AS activities | |
ON activities.project_id = projects.id | |
WHERE "activities"."count" BETWEEN 1 AND 2 | |
OR "activities"."count" BETWEEN 3 AND 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Project.where( | |
id: Activity.active.group(:project_id).having("COUNT(*) IN (?)", [1..2, 3..4].flat_map(&to_a)).select(:project_id) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT "projects".* | |
FROM "projects" | |
WHERE "projects"."id" IN ( | |
SELECT "activities"."project_id" | |
FROM "activities" | |
WHERE "activities"."active" = 't' | |
GROUP BY "activities"."project_id" | |
HAVING COUNT(*) IN (1,2,3,4) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment