SQL如何去掉查询结果的重复数据?

haoyubin87 2012-04-16 02:33:25
select top 10 a from ta order by b desc.
目的是取出依照b排序的a数据 其中b是不会重复的查询出的a是重复的,如何去掉!
例如 以下数据
a b
我 1
我 2
你 3
他 4
她 5
她 6
需要得到的数据是




这样的顺序!
...全文
25352 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
lu492716656 2015-01-07
  • 打赏
  • 举报
回复
select distinct a from table where a in (select top 10 a from table order by b desc)
simonxt 2012-04-18
  • 打赏
  • 举报
回复
-- 数据
create table tb(mm varchar(20),nn int)
insert tb values('我',1);
insert tb values('我',2);
insert tb values('你',3);
insert tb values('他',4);
insert tb values('她',5);
insert tb values('她',6);

-- 查询
select mm from (
select r=row_number() over (partition by mm order by nn), * from tb
) a
where a.r=1
order by nn desc
liukeya2008 2012-04-17
  • 打赏
  • 举报
回复
之前在英文系统里,写错了。。
liukeya2008 2012-04-17
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
select top 10 a from ta order by b desc.
目的是取出依照b排序的a数据 其中b是不会重复的查询出的a是重复的,如何去掉!
例如 以下数据
a b
我 1
我 2
你 3
他 4
她 5
她 6
需要得到的数据是




这样的顺序!
[/Quote]


create table tb(mm varchar(20),nn int)
insert tb values('我',1),
('我',2),
('你',3),
('他',4),
('她',5),
('她',6);

select mm
from(
select *,ROW_NUMBER()over(partition by mm order by nn) rin
from tb
)fin
where fin.rin=1
order by mm desc

十三门徒 2012-04-17
  • 打赏
  • 举报
回复
select top 10 a from( select distinct a,max(b) as b from ta group by b) table_A order by b desc
leijunyuncyuyan 2012-04-16
  • 打赏
  • 举报
回复
distinct
haoyubin87 2012-04-16
  • 打赏
  • 举报
回复
看了大家的回复,由于需要的数据是既要满足你们回复的不重复 又要得到固定数量 如top 10 】所以还是暂时用
select top 10 a
from ta t
where not exists(select 1 from ta where a = t.a and b > t.b)
order by b desc
孤独加百列 2012-04-16
  • 打赏
  • 举报
回复

CREATE TABLE TEST2
(
Name VARCHAR(10),
Id INT
)
GO

INSERT INTO TEST2
SELECT '我',1 UNION
SELECT '我',2 UNION
SELECT '你',3 UNION
SELECT '他',4 UNION
SELECT '她',5 UNION
SELECT '她',6



SELECT NAME
FROM TEST2
GROUP BY Name
ORDER BY MAX(ID) DESC
imtsr 2012-04-16
  • 打赏
  • 举报
回复
distinct
showwe 2012-04-16
  • 打赏
  • 举报
回复
select a,max(b)
from ta
group by a
order by b desc

酱紫不可以吗?
qiushi4958 2012-04-16
  • 打赏
  • 举报
回复
CREATE TABLE tmp AS SELECT MIN(b) as id FROM ta GROUP BY a;
DELETE FROM ta WHERE id NOT IN (SELECT id FROM tmp )
DROP TABLE tmp

然后 select * from ta order by b desc
help2002 2012-04-16
  • 打赏
  • 举报
回复
select a from ta group by a order by max(b) desc
Felixzhaowenzhong 2012-04-16
  • 打赏
  • 举报
回复
declare @tb table (a nvarchar(12),b int)
insert @tb
select '我',1 union all
select '我',2 union all
select '你',3 union all
select '他',4 union all
select '她',5 union all
select '她',6

select a from (
select top (100) ROW_NUMBER() over(PARTITION by a order by a desc)rn ,a,b from @tb group by a,b order by b desc) aa
where rn=1
/*
a




*/
koumingjie 2012-04-16
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
select top 10 a
from ta t
where not exists(select 1 from ta where a = t.a and b > t.b)
order by b desc
这样可以 但是查询时间过长,可以优化吗?谢谢大家
[/Quote]

正解
nightgoblin 2012-04-16
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
select top 10 a
from ta t
where not exists(select 1 from ta where a = t.a and b > t.b)
order by b desc
这样可以 但是查询时间过长,可以优化吗?谢谢大家
[/Quote]
我这有可以实现,看你觉得满意不:

IF TYPE_ID('tyta') IS NOT NULL
DROP TYPE tyta;
GO
CREATE TYPE tyta AS TABLE
( a CHAR(4)
);
GO
DECLARE @a AS tyta;
BEGIN
INSERT INTO @a SELECT a FROM ta ORDER BY b DESC ;
SELECT DISTINCT * FROM @a;
END;
/*结果
a




*/
haoyubin87 2012-04-16
  • 打赏
  • 举报
回复
select top 10 a
from ta t
where not exists(select 1 from ta where a = t.a and b > t.b)
order by b desc
这样可以 但是查询时间过长,可以优化吗?谢谢大家
nightgoblin 2012-04-16
  • 打赏
  • 举报
回复

这个问题看似简单,其实挺复杂的,我也没想出来。还是等下面高手来解答的吧。
haoyubin87 2012-04-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

引用 1 楼 的回复:

SQL code
select a,max(b) as mb from ta order by max(b)

囧,写错了

SQL code

select distinct a from ta order by b

select a from ta group by a order by b
[/Quote]

这个也是语法错误!b不包含在group by 中
haoyubin87 2012-04-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

select distinct a from ta order by b desc
[/Quote]

这个语法错误!
Felixzhaowenzhong 2012-04-16
  • 打赏
  • 举报
回复
select distinct a from ta order by b desc

加载更多回复(2)

34,575

社区成员

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

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