我有这样的查询:
SELECT id FROM my_table
WHERE ip IN (SELECT ip FROM my_table WHERE id IN (1, 2, 3, 4))
GROUP BY id
HAVING SUM(count) >= 10
而不是硬编码要求sum(count)> = 10,我希望它是sum(count)是> =该id的count列的总和的10%(即没有“WHERE ip IN ...”约束)
在这样的查询中是否可以这样做?
答案 0 :(得分:1)
您可以编写子查询来获得10%:
SELECT id FROM my_table
WHERE ip IN (SELECT ip FROM my_table WHERE id IN (1, 2, 3, 4))
GROUP BY id
HAVING SUM(count) >= (SELECT 0.1 * SUM(t.count) FROM my_table t WHERE t.id = my_table.id)