select s.attr1, s.attr2
from table1 s
where s.primarykey in
(select g.primarykey
from table2 g
group by g.primarykey
having avg(g.numattr) > 80)
group by s.attr1
having min(s.attr3)
order by (s.attr2);
该查询应该在表2中检查平均numattr> 80,然后为满足该条件的主键,为每个attr1选择attr3的最小值,然后按attr2的顺序对其进行排序。我得到的结果是没有选择最小值,也不确定为什么。
答案 0 :(得分:0)
您需要将MIN
移至SELECT
才能将其放入结果中。另外,您需要按attr1
和attr2
进行分组。您可以尝试以下方法吗?
SELECT s.attr1, s.attr2, min(s.attr3)
FROM table1 s
WHERE s.primarykey IN
(SELECT g.primarykey
FROM table2 g
GROUP BY g.primarykey
HAVING avg(g.numattr) > 80)
GROUP BY s.attr1, s.attr2
ORDER BY s.attr2;
答案 1 :(得分:0)
SELECT s.attr1, s.attr2,
FROM table1 s
WHERE s.attr3 =
(SELECT min(s2.attr3) from table1 s2 where s.attr1 = s2.attr1 group by s2.attr1)
and s.primarykey IN
(SELECT g.primarykey
FROM table2 g
GROUP BY g.primarykey
HAVING avg(g.numattr) > 80)
GROUP BY s.attr1, s.attr2
ORDER BY s.attr2;
我认为是这样做的。