求几条SQL语句!大侠进!!

woolern 2006-12-07 02:07:44
有表A (各字段均为varchar)
A1 A2 A3 A4

12 北京代理有限公司 AAAA 其他
13 中国科学总公司 DFDSF 灵长
21 种畜有限公司 234sdf sdfs
21 种畜有限公司 234sdf 花生
33 中粮有限公司 65268 大豆
41 英才月社 null 动物
51 科倍有限公司 84279 种用活动物
61 雄贸有限公司 null 长目动物
55 瑞鑫技公司 7018 用除外
.
.
.
要求选出所有A1不同的记录,若A1相同,则只取相同数据中的第一条.(A1与A2唯一对应)该如何实现,

55 瑞鑫技公司 7018 用除外
21 种畜有限公司 234sdf sdfs
21 种畜有限公司 234sdf 花生
执行后只保留
55 瑞鑫技公司 7018 用除外
21 种畜有限公司 234sdf sdfs


请大侠帮忙!!
...全文
221 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
marco08 2006-12-07
  • 打赏
  • 举报
回复

create table T(A1 varchar(20), A2 varchar(20), A3 varchar(20), A4 varchar(20))
insert T select '12', '北京代理有限公司', 'AAAA', '其他'
union all select '13', '中国科学总公司', 'DFDSF', '灵长'
union all select '21', '种畜有限公司', '234sdf', 'sdfs'
union all select '21', '种畜有限公司', '234sdf', '花生'
union all select '33', '中粮有限公司', '65268', '大豆'
union all select '41', '英才月社', null, '动物'
union all select '51', '科倍有限公司', '84279', '种用活动物'
union all select '61', '雄贸有限公司', null, '长目动物'
union all select '55', '瑞鑫技公司', '7018', '用除外'

select ID=identity(int, 1, 1), * into #t from T

select * from #T where ID in(
select min(ID) as ID from #T group by A1)

--result
ID A1 A2 A3 A4
----------- -------------------- -------------------- -------------------- --------------------
1 12 北京代理有限公司 AAAA 其他
2 13 中国科学总公司 DFDSF 灵长
3 21 种畜有限公司 234sdf sdfs
5 33 中粮有限公司 65268 大豆
6 41 英才月社 NULL 动物
7 51 科倍有限公司 84279 种用活动物
8 61 雄贸有限公司 NULL 长目动物
9 55 瑞鑫技公司 7018 用除外

(8 row(s) affected)

drop table T, #T
中国风 2006-12-07
  • 打赏
  • 举报
回复

declare @ta table (A1 int,A2 varchar(20), A3 varchar(10),A4 varchar(10))
insert @ta
select 12, '北京代理有限公司', 'AAAA', '其他' union all
select 13, '中国科学总公司', 'DFDSF', '灵长' union all
select 21, '种畜有限公司', '234sdf', 'sdfs' union all
select 21, '种畜有限公司', '234sdf', '花生' union all
select 33, '中粮有限公司', '65268' , '大豆' union all
select 41, '英才月社', null, '动物' union all
select 51, '科倍有限公司', '84279' , '种用活动物' union all
select 61, '雄贸有限公司', null ,'长目动物' union all
select 55, '瑞鑫技公司', '7018', '用除外'

select * from @ta a
where not exists(select 1 from @ta where A1=a.A1 and A2=a.A2 and A4>a.A4 ) order by A1

(所影响的行数为 9 行)

A1 A2 A3 A4
----------- -------------------- ---------- ----------
12 北京代理有限公司 AAAA 其他
13 中国科学总公司 DFDSF 灵长
21 种畜有限公司 234sdf 花生
33 中粮有限公司 65268 大豆
41 英才月社 NULL 动物
51 科倍有限公司 84279 种用活动物
55 瑞鑫技公司 7018 用除外
61 雄贸有限公司 NULL 长目动物

(所影响的行数为 8 行)

中国风 2006-12-07
  • 打赏
  • 举报
回复

declare @ta table (A1 int,A2 varchar(20), A3 varchar(10),A4 varchar(10))
insert @ta
select 12, '北京代理有限公司', 'AAAA', '其他' union all
select 13, '中国科学总公司', 'DFDSF', '灵长' union all
select 21, '种畜有限公司', '234sdf', 'sdfs' union all
select 21, '种畜有限公司', '234sdf', '花生' union all
select 33, '中粮有限公司', '65268' , '大豆' union all
select 41, '英才月社', null, '动物' union all
select 51, '科倍有限公司', '84279' , '种用活动物' union all
select 61, '雄贸有限公司', null ,'长目动物' union all
select 55, '瑞鑫技公司', '7018', '用除外'

select * from @ta a
where A4=(select max(A4) from @ta where A1=a.A1 and A2=a.A2 ) order by A1

(所影响的行数为 9 行)

A1 A2 A3 A4
----------- -------------------- ---------- ----------
12 北京代理有限公司 AAAA 其他
13 中国科学总公司 DFDSF 灵长
21 种畜有限公司 234sdf 花生
33 中粮有限公司 65268 大豆
41 英才月社 NULL 动物
51 科倍有限公司 84279 种用活动物
55 瑞鑫技公司 7018 用除外
61 雄贸有限公司 NULL 长目动物

(所影响的行数为 8 行)

caixia615 2006-12-07
  • 打赏
  • 举报
回复
select top 1* from tablename group by A1
Yang_ 2006-12-07
  • 打赏
  • 举报
回复
select * from a a1
where listid=(select top 1 listid from a where a1=a1.a1)
wzh1215 2006-12-07
  • 打赏
  • 举报
回复
select id=identity(int,1,1),* into #aa from A
select a.A1,a.A2,a.A3,a.A4 from #aa a where not exists(select 1 from #aa b where a.A1=b.A1 and a.id>b.id)
woolern 2006-12-07
  • 打赏
  • 举报
回复
有一个listid是标识字段
中国风 2006-12-07
  • 打赏
  • 举报
回复
用group by
Yang_ 2006-12-07
  • 打赏
  • 举报
回复
最好有唯一字段

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧