我的数据与此类似。
ID Age
------------
101 60
102 40
102 40
103 25
103 35
104 28
104 28
104 28
需要sql只输出具有相同id但不同年龄值的行。
ID Age
------------
103 25
103 35
对此有任何帮助表示赞赏!
答案 0 :(得分:0)
如果你只想要id:
select id, count(distinct age) nb
from yourtable
group by id
having count(distinct age)>1
如果您也想要年龄,请再次加入您的表格
with doubleage as (
select id, count(distinct age) nb
from yourtable
group by id
having count(distinct age)>1
)
select f1.* from yourtable f1 inner join doubleage f2 on f1.id=f2.id
答案 1 :(得分:0)
其他解决方案
select * from yourtable f1
where exists
(
select * from yourtable f2
where f1.id=f2.id and f1.age<>f2.age
)
答案 2 :(得分:0)
其他解决方案
select * from yourtable f1
inner join lateral
(
select count(*) nb from yourtable f2
where f1.id=f2.id and f1.age<>f2.age
) f3 on 1=1
where f3.nb>0
答案 3 :(得分:0)
SELECT * FROM F1 ID在哪里( SELECT F1.id 来自F1 GROUP BY F1.ID 有计数(不同的F1.ID)&gt; 1)
答案 4 :(得分:-1)
如果我正确理解了这个问题并且你只有一张桌子,那么这就是你要找的东西:
select * from tablename group by ID where count(ID)>1
输出相同:
ID Age
------------
103 25
103 35
答案 5 :(得分:-1)
SELECT
[a].[id]
,[a].[age]
FROM
@tbl AS [a]
INNER JOIN
@tbl AS [b]
ON
[a].[id] = [b].[id]
AND [a].[age] <> [b].[age]
GROUP BY
[a].[id]
,[a].[age];
P.S. if you have data like:
(101, 60)
,(102, 40)
,(102, 40)
,(103, 25)
,(103, 35)
,(103, 35)
,(104, 28)
,(104, 28)
,(104, 28)
Result is:
,(103, 25)
,(103, 35)
Not:
,(103, 25)
,(103, 35)
,(103, 35)
Keep this in mind...