查询id出现次数不为4的语句怎么写啊
有个表有属性id 数据1 数据2
1 123 23
1 564 42
1 5654 41
2 54 24
2 565 12
2 45 21
一般一个id有四行数据,我想找出那些id只有2行或者3行数据条目,这个sql怎么写呢
------最佳解决方案--------------------select id from [表]
group by id
having count(id) between 2 and 3
------其他解决方案--------------------#1 是不等于4条的。
#2 是2条或是3条的。
------其他解决方案--------------------
正解。。。。
------其他解决方案--------------------
SELECT *
FROM (
SELECT ROWNUM=ROW_NUMBER()OVER(PARTITION BY ID ORDER BY ID),*
FROM TABLE01
) T
WHERE T.ROWNUM>=2 AND T.ROWNUM<=3
------其他解决方案--------------------2楼正解
------其他解决方案--------------------
SELECT *
FROM tb
WHERE id NOT IN ( SELECT id
FROM tb
GROUP BY id
HAVING COUNT(id) NOT IN ( 2, 3 ) )
------其他解决方案--------------------
我觉得1楼的简单易懂。
考虑到不符合规则的不是一般情况,
SELECT *
FROM table
WHERE Id IN (
SELECT Id
FROM table
GROUP BY Id
HAVING COUNT(Id) <> 4)
------其他解决方案--------------------select * from table1 where id in(select id from table1
group by id having count(id)=2 or count(id)=3)