我有一个如下所示的查询:
SELECT COUNT(*)
FROM members AS t1
INNER JOIN data AS t2
ON t1.ID = t2.ID
WHERE members_id = 1
INTERSECT
SELECT COUNT(*)
FROM members AS t1
INNER JOIN data AS t2
ON t1.ID = t2.ID
WHERE members_id = 2
我想使用group by和order by order:
SELECT COUNT(*), var1
FROM members AS t1
INNER JOIN data AS t2
ON t1.ID = t2.ID
WHERE members_id = 1
INTERSECT
SELECT COUNT(*), var1
FROM members AS t1
INNER JOIN data AS t2
ON t1.ID = t2.ID
WHERE members_id = 2
GROUP BY var1
ORDER BY var1
但是,我得到一个错误,说var1不包含在聚合函数或GROUP BY子句中。有关使用GROUP BY和ORDER BY与intersect命令的任何提示吗?
谢谢!
答案 0 :(得分:0)
这是您要尝试实现的查询吗?
SELECT COUNT(*), var1
FROM members AS t1 INNER JOIN
data AS t2
ON t1.ID = t2.ID
GROUP BY var1
having sum(case when members_id = 1 then 1 else 0 end) > 0 and
sum(case when members_id = 2 then 1 else 0 end) > 0 ;
这会获得包含两个成员的每个var1
的计数。