34,588
社区成员
发帖
与我相关
我的任务
分享
select min(id) from career group by licenseno having(count(*)>1)
select min(id),licenseno from career group by licenseno having cout(licenseno)>1
--try
if object_id ('career')is not null
drop table career
go
create table career (id int identity not null primary key,
licenseno varchar(20) null)
insert into career(licenseno)
select '2345'
union all
select '2345'
union all
select '4566'
union all
select '45678'
union all
select '4566'
select min(id) from career group by licenseno having(count(*)>1)
/*
-----------
1
3
(2 行受影响)
*/
-- 下面的max 可以改为min 随便
select id,licenseno
from career a
where id in (select max(id) from career where a.licenseno=licenseno group by licenseno having count(*)>=2)
--去掉所有重复的 sql 2005
select id,licenseno
from (select id,licenseno,row_number() over(partition by licenseno order by id) rn
from career) a
where rn=1
--1)表和数据
create table career (
id int identity not null primary key,
licenseno varchar(20) null
)
里面的数据,(1,'2345'
2,'2345'
3,'4566'
4,'45678'
5,'4566'
)
INSERT INTO career
SELECT '2345'
UNION ALL
SELECT '2345'
UNION ALL
SELECT'4566'
UNION ALL
SELECT '45678'
UNION ALL
SELECT '4566'
--SELECT * FROM career
--2)查询
SELECT Min_Way = MIN(id) FROM career
GROUP BY licenseno
HAVING COUNT(1) > 1
Min_Way
-----------
1
3
(2 row(s) affected)
--3)查询
SELECT MAX_Way = MAX(id) FROM career
GROUP BY licenseno
HAVING COUNT(1) > 1
MAX_Way
-----------
2
5
(2 row(s) affected)
select * from career a where not exists(select 1 from career where licenseno=a.licenseno and id>a.id)