1. Product A 7.5
2. Product A 7.3
3. Product A 7.4
4. Product B 6.2
5. Product B 6.5
6. Product B 6.01
7. Product c 2.4
8. Product c 2.0
不包括产品A的最大值行= 7.5,则该产品A的剩余计数将为2。所以我需要这样的
Product A 2
Product B 2
Product C 1..
答案 0 :(得分:3)
如果最大值没有重复,则只需从COUNT中减去1:
SELECT product, COUNT(*) - 1 AS counter FROM products GROUP BY product;
,如果有重复项并且您希望将它们全部排除,则此操作:
SELECT p.product, COUNT(*) AS counter
FROM products p
WHERE p.value < (SELECT MAX(value) FROM products WHERE product = p.product)
GROUP BY p.product;
或者如果您想返回零:
SELECT
p.product,
COUNT(*) - (
SELECT COUNT(*)
FROM products
WHERE
product = p.product
AND
value = (
SELECT MAX(value) FROM products WHERE product = p.product
)
) AS counter
FROM products p
GROUP BY p.product;
答案 1 :(得分:1)
您可能重复了相同的最大值。如果是这样:
select t.product
from t join
(select product, max(price) as maxprice
from t
group by product
) tt
on t.product = tt.product
where t.price < t.maxprice
group by t.product;