计算表中重复行的数量

[复制链接]
查看11 | 回复9 | 2008-12-24 12:00:59 | 显示全部楼层 |阅读模式
table test:
id
1
1
1
2
2
3
select出总共有多少行,distinct的数量,重复的行数。
谢谢!!
回复

使用道具 举报

千问 | 2008-12-24 12:00:59 | 显示全部楼层

SQL> select * from tmp_3;
ID
----------
1
1
1
2
2
3
6 rows selected
SQL>
SQL> select count(id)"总共有多少行",
2 count(distinct id) "distinct的数量",
3 count(id) - count(distinct id) "重复的行数"
4from tmp_3;
总共有多少行 distinct的数量 重复的行数
------------ -------------- ----------
6
3
3
SQL>

[ 本帖最后由 bell6248 于 2008-3-26 14:28 编辑 ]
回复

使用道具 举报

千问 | 2008-12-24 12:00:59 | 显示全部楼层
总共6行,distinct的数据为3,重复的行数为2
回复

使用道具 举报

千问 | 2008-12-24 12:00:59 | 显示全部楼层
SQL>select * from t_tempa;
ID
----------
1
1
1
2
2
3
===
SQL> select distinct a.id content,b.counts duptimes f
rom t_tempa a,(select count(id) counts,id from t_tempa group by id) bwhere b.c
ounts>1 and a.id=b.id;
CONTENT DUPTIMES
---------- ----------
1
3
2
2
回复

使用道具 举报

千问 | 2008-12-24 12:00:59 | 显示全部楼层
WITH A AS (
select 1 as no from dual
union all
select 1 as no from dual
union all
select 1 as no from dual
union all
select 2 as no from dual
union all
select 2 as no from dual
union all
select 3 as no from dual
)
select count(no)"总共有多少行",
count(distinct no) "distinct的数量",
count(no) - count(distinct no) "重复的行数"
from A;
回复

使用道具 举报

千问 | 2008-12-24 12:00:59 | 显示全部楼层
count(*)||count(distinct)||重复的行数:select count(*),count(distinct id) ,(count(*)-count(distinct id))*2 from test;
回复

使用道具 举报

千问 | 2008-12-24 12:00:59 | 显示全部楼层


原帖由 bitiwyh 于 2008-3-26 14:40 发表
SQL>select * from t_tempa;
ID
----------
1
1
1
2
2
3
===
SQL> select distinct a.id content,b.counts duptimes f
rom t_tempa a,(select count(id) counts,id from t_tempa group by id) bwhere b.c
ounts>1 and a.id=b.id;
CONTENT DUPTIMES
---------- ----------
1
3
2
2

回复

使用道具 举报

千问 | 2008-12-24 12:00:59 | 显示全部楼层
沉默
回复

使用道具 举报

千问 | 2008-12-24 12:00:59 | 显示全部楼层
4楼的更简单的写法:
select id, count(*) duplicate from t group by id having count(*)>1
[ 本帖最后由 lastwinner 于 2008-3-27 01:32 编辑 ]
回复

使用道具 举报

千问 | 2008-12-24 12:00:59 | 显示全部楼层
原帖由 bell6248 于 2008-3-26 14:26 发表

SQL> select * from tmp_3;
ID
----------
1
1
1
2
2
3
6 rows selected
SQL>
SQL> select count(id)"总共有多少行",
2 count(distinct id) "distinct的数量",
3 count(id) - count(distinct id) "重复的行数"
4from tmp_3;
总共有多少行 distinct的数量 重复的行数
------------ -------------- ----------
6
3
3
SQL>

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行