如何获取重复列的值

时间:2015-05-07 18:00:02

标签: sql oracle db2

Id       account   date

101       23       01/01/2015

101       24       01/01/2015

101       25       02/01/2015

102       23       02/01/2015

103       24       02/15/2015

如何获取diff帐户中存在相同id的值。??

实施例; 101

2 个答案:

答案 0 :(得分:0)

您可以使用Group By then Check count大于2.假设您拥有唯一的ID,帐户和日期组合

SELECT ID
FROM t
GROUP BY ID
Having count(*) > 2

答案 1 :(得分:0)

尝试

select id, account, date
  from yourtable
 where id in ( select id
                 from yourtable
                group by id 
                having count(*) > 1)

请在此处查看:http://sqlfiddle.com/#!4/56c1d/1

正如我在评论中所说,请更新您的问题以参加您的新要求。对于您的原始问题,上面的答案将解决您的问题,因为您要求新的要求,您应该更新您的问题或创建一个新的问题。

以下是您对评论的要求。

select yourtable.id, yourtable.account, 
       yourtable.dt, accqt.qtdAccounts
  from yourtable inner join
       (select id, count(*) qtdAccounts
          from yourtable
         group by id) accqt
          ON (yourtable.id = accqt.id)
 where yourtable.id in ( select id
                           from yourtable
                          group by id
                         having count(*) > 1 )

请在此处查看:http://sqlfiddle.com/#!4/56c1d/4