rspec/rules/S3645/rule.adoc
2022-02-04 16:28:24 +00:00

29 lines
714 B
Plaintext

``++HAVING++`` was introduced to limit the result set after selection because ``++WHERE++`` clause conditions cannot be used on aggregated values. ``++HAVING++`` conditions that don't operate on aggregated values can safely be moved into the ``++WHERE++`` clause for increased efficiency.
== Noncompliant Code Example
[source,text]
----
SELECT p.id, p.name, count(o.id)
FROM product p
JOIN order_row o on p.id=o.product_id
GROUP BY p.id
HAVING count(o.id) > 10
p.name like "t-shirt%" -- Noncompliant
----
== Compliant Solution
[source,text]
----
SELECT p.id, p.name, count(o.id)
FROM product p
JOIN order_row o on p.id=o.product_id
WHERE p.name like "t-shirt%"
GROUP BY p.id
HAVING count(o.id) > 10
----