sql匹配查询排序

翠花琳 2009-12-25 10:08:15
表A中有Address,Aname,Asex三个字段
insert into a values('上海','张三','男')
insert into a values('北京','李四','女')
insert into a values('深圳','王五','男')
insert into a values('上海','赵六','女')
insert into a values('北京','陈七','男')
insert into a values('上海','张三','男')
...
查询所有结果,地址最多的排第一,姓名排第二,性别第三,输出结果
Address Aname Asex
上海 张三 男
上海 张三 男
上海 赵六 女
北京 ....
....




请教如何实现?
...全文
143 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
黄_瓜 2009-12-25
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 chensirbbk 的回复:]
测试结果不正确!
[/Quote]
你想要什么结果?
create table A(Address varchar(10),Aname varchar(10),Asex varchar(10))
insert into a values('上海','张三','男')
insert into a values('北京','李四','女')
insert into a values('深圳','王五','男')
insert into a values('上海','赵六','女')
insert into a values('北京','陈七','男')
insert into a values('上海','张三','男')
go

select * from a as b order by
isnull((select count(Address) from a where Address=b.Address),0) desc ,
isnull((select count(Aname) from a where Aname=b.Aname),0) desc ,
isnull((select count(Asex) from a where Asex=b.Asex),0) desc
/*
Address Aname Asex
---------- ---------- ----------
上海 张三 男
上海 张三 男
上海 赵六 女
北京 陈七 男
北京 李四 女
深圳 王五 男

(6 行受影响)


*/
ACMAIN_CHM 2009-12-25
  • 打赏
  • 举报
回复
1> select t.*
2> from a t inner join (select Aname,count(*) as cnt from a group by Aname) b
3> on t.Aname=b.Aname
4> order by cnt desc,t.Aname
5> go
Address |Aname |Asex
----------|----------|----------
上海 |张三 |男
上海 |张三 |男
北京 |陈七 |男
北京 |李四 |女
深圳 |王五 |男
上海 |赵六 |女

(6 rows affected)
1>



建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html

1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)

这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。

Mr_Nice 2009-12-25
  • 打赏
  • 举报
回复
select
*
from
a t
order by
isnull((select count(1) from a where Address=t.Address),0)desc,
isnull((select count(1) from a where Aname=t.Aname),0)desc,
isnull((select count(1) from a where Asex=t.Asex),0)desc
ACMAIN_CHM 2009-12-25
  • 打赏
  • 举报
回复
[Quote= #7楼 chensirbbk(chensirbbk) ]测试结果不正确![/Quote]

1> select * from a
2> go
Address |Aname |Asex
----------|----------|----------
上海 |张三 |男
北京 |李四 |女
深圳 |王五 |男
上海 |赵六 |女
北京 |陈七 |男
上海 |张三 |男

(6 rows affected)
1> select Address,Aname,Asex
2> from a t
3> order by (select count(*) from a where Aname= t.Aname) desc,Aname
4> go
Address |Aname |Asex
----------|----------|----------
上海 |张三 |男
上海 |张三 |男
北京 |陈七 |男
北京 |李四 |女
深圳 |王五 |男
上海 |赵六 |女

(6 rows affected)
1>
sgtzzc 2009-12-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 dawugui 的回复:]
SQL codecreatetable A(Addressvarchar(10),Anamevarchar(10),Asexvarchar(10))insertinto avalues('上海','张三','男')insertinto avalues('北京','李四','女')insertinto avalues('深圳','王五','男')insertinto avalues('上海','赵六?-
[/Quote]

乌龟这速度!
--小F-- 2009-12-25
  • 打赏
  • 举报
回复
--抄袭乌龟数据
create table a(Address varchar(10),Aname varchar(10),Asex varchar(10))
insert into a values('上海','张三','男')
insert into a values('北京','李四','女')
insert into a values('深圳','王五','男')
insert into a values('上海','赵六','女')
insert into a values('北京','陈七','男')
insert into a values('上海','张三','男')
go

select
*
from
a t
order by
isnull((select count(1) from a where Address=t.Address),0)desc,
isnull((select count(1) from a where Aname=t.Aname),0)desc,
isnull((select count(1) from a where Asex=t.Asex),0)desc
/*Address Aname Asex
---------- ---------- ----------
上海 张三 男
上海 张三 男
上海 赵六 女
北京 陈七 男
北京 李四 女
深圳 王五 男

(6 行受影响)
*/

drop table a
翠花琳 2009-12-25
  • 打赏
  • 举报
回复
测试结果不正确!
sgtzzc 2009-12-25
  • 打赏
  • 举报
回复
select a.*
from a
left join
(select Address,count(1)px1 from a group by Address)b
on a.Address=b.Address
left join
(select Aname,count(1)px2 from a group by Aname)c
on a.Aname=c.Aname
left join
(select Asex,count(1)px3 from a group by Asex)d
on a.Asex=b.Asex
order by
b.px1 desc,
c.px2 desc,
d.px3


--小F-- 2009-12-25
  • 打赏
  • 举报
回复
select
*
from
tb t
order by
isnull((select count(1) from tb where Address=t.Address),0)desc,
isnull((select count(1) from tb where Aname=t.Aname),0)desc,
isnull((select count(1) from tb where Asex=t.Asex),0)desc
dawugui 2009-12-25
  • 打赏
  • 举报
回复
create table A(Address varchar(10),Aname varchar(10),Asex varchar(10))
insert into a values('上海','张三','男')
insert into a values('北京','李四','女')
insert into a values('深圳','王五','男')
insert into a values('上海','赵六','女')
insert into a values('北京','陈七','男')
insert into a values('上海','张三','男')
go

select a.* from a
left join
(select Address , count(1) cnt from a group by Address) b
on a.Address = b.Address
left join
(select Aname , count(1) cnt from a group by Aname) c
on a.Aname = c.Aname
left join
(select Asex , count(1) cnt from a group by Asex) d
on a.Asex = d.Asex
order by b.cnt desc , c.cnt desc , d.cnt desc



drop table a


/*
Address Aname Asex
---------- ---------- ----------
上海 张三 男
上海 张三 男
上海 赵六 女
北京 陈七 男
北京 李四 女
深圳 王五 男

(所影响的行数为 6 行)


*/
ACMAIN_CHM 2009-12-25
  • 打赏
  • 举报
回复
select t.*
from 表A t inner join (select Aname,count(*) as cnt from 表A group by Aname) b
on t.Aname=b.Aname
order by cnt desc,a.Aname
aimee_99 2009-12-25
  • 打赏
  • 举报
回复
select * 
from ta a
order by isnull((select count(*) from ta where Address=a.Address),0)desc,
isnull((select count(*) from ta where Aname=a.Aname),0)desc,
isnull((select count(*) from ta where Asex=a.Asex),0)desc
ACMAIN_CHM 2009-12-25
  • 打赏
  • 举报
回复
select Address,Aname,Asex
from 表A t
order by (select count(*) from 表A where Aname= t.Aname) desc,Aname

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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