所以我有 2 个数据表,一个名为 OCEAN,列:TYPE1 和 COLOR1另一个名为 LAKE 的列:TYPE2 和 COLOR2我正在尝试弄清楚如何从 OCEAN 表中输出类型,这些类型在 OCEAN 表中的类型数量多于 LAKE 表。
现在我有:
SELECT type1 from
(SELECT type1, count(type1) as count1 from OCEAN
GROUP BY type1
HAVING count1 > count2 from
(SELECT type2, count(type2) as count2 from LAKE GROUP BY type2)
)
但它显然不起作用。有什么想法吗?
我认为这是一个解决方案:
select type1
from ocean
group by type1
having count(type1) > (
select count(type2) from lake where type2=type1 group by type2)
您可以使用此在线演示
您可以使用 OUTER APPLY 执行此操作。
SELECT Type1,
COUNT(*) Type1Count,
Type2Count
FROM Ocean o
OUTER APPLY (SELECT COUNT(*) Type2Count
FROM Lake l
WHERE l.Type2 = o.Type1
) oa
GROUP BY Type1,
Type2Count
HAVING COUNT(*) > Type2Count