求一个sql语句,快下班了,急求!!

c254922398 2013-08-13 04:05:33
有一张表pEdu,数据如下

eid eidx ename elevel eflag
2 1 j 1 1
5 1 2 1 1
9 0 123 0 0
19 34 123 0 0
20 34 123 0 0
21 34 123 0 0
22 34 111 0 0
23 34 111 0 0
24 45 123 0 0
25 46 132 0 0
26 46 123 0 0
27 47 123 0 0
28 47 123 0 0
38 50 update 0 0
48 54 2 0 0
50 55 123 0 0
51 55 132 0 0
55 56 1323 0 0
56 56 123 0 0

SELECT eName,eIDx FROM pEdu WHERE eName LIKE '%1%' GROUP BY eIDx, eName
现在我对数据进行分组查询,查询出我Name中含有1的数据,如下
eName eIDx
123 0
111 34
123 34
123 45
123 46
132 46
123 47
123 55
132 55
123 56
1323 56

现在问题来了,我想根据上面的数据再次进行查询,只查询出123,111,132和1323这4条数据,但是是根据
COUNT来排列的,也就是说123排在最前,111和1323排在最后,请问sql语句应该怎么写?最好是用一条子查询语句,存储过程就算了,求大牛解答疑惑


...全文
180 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
c254922398 2013-08-13
  • 打赏
  • 举报
回复
引用 4 楼 ap0405140 的回复:

select y.eName
from
(select t.eName,count(1) 'ct'
  from (SELECT eName,eIDx
        FROM pEdu 
        WHERE eName LIKE '%1%' 
        GROUP BY eIDx,eName) t
  group by t.eName
) y
order by ct desc
SELECT   eName
FROM      (SELECT   eName, COUNT(1) AS ct
                 FROM      (SELECT   eName, eIDx
                                  FROM      pEdu
                                  WHERE   (eName LIKE '%1%')
                                  GROUP BY eIDx, eName) AS t
                 GROUP BY eName) AS y
ORDER BY ct DESC
直接复制过来报错,自己改了下好用了,谢谢这位朋友。也感谢大家! 可以准点下班了
Andy__Huang 2013-08-13
  • 打赏
  • 举报
回复
真是够快了,几分钟以内都有十几次回复
Andy__Huang 2013-08-13
  • 打赏
  • 举报
回复
create table #tb(eidx int,ename varchar(20))
insert into #tb
select 1,'j'
union all select 1,'2'
union all select 0,'123'
union all select 34,'123'
union all select 34,'123'
union all select 34,'123'
union all select 34,'111'
union all select 34,'111'
union all select 45,'123'
union all select 46,'132'
union all select 46,'123'
union all select 47,'123'
union all select 47,'123'
union all select 50,'update'
union all select 54,'2'
union all select 55,'123'
union all select 55,'132'
union all select 56,'1323'
union all select 56,'123'

select *
from (SELECT eName,eIDx,count(*) as count FROM #tb WHERE eName LIKE '%1%' GROUP BY eIDx, eName
)t
order by case when eName='123' then 0 when eName='111' or eName='1323' then 9 else 1 end,count 

/*eName	eIDx	Count
123	0	1
123	45	1
123	46	1
123	55	1
123	56	1
123	47	2
123	34	3
132	46	1
132	55	1
1323	56	1
111	34	2
*/
  • 打赏
  • 举报
回复

SELECT eName,eIDx 
FROM pEdu 
WHERE eName LIKE '%1%' 
GROUP BY eIDx, eName
order by COUNT(eName)
Shawn 2013-08-13
  • 打赏
  • 举报
回复
SELECT eName,CNT=COUNT(1)
FROM
(
	SELECT DISTINCT eName,eIDx FROM #temp 
	WHERE eName LIKE '%1%' 
) T
GROUP BY eName
ORDER BY COUNT(1) DESC, eName	--如果个数相同,可以再按eName排序
發糞塗牆 2013-08-13
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-08-13 16:11:53
-- Version:
-- Microsoft SQL Server 2014 (CTP1) - 11.0.9120.5 (X64)
-- Jun 10 2013 20:09:10
-- Copyright (c) Microsoft Corporation
-- Enterprise Evaluation Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([eidx] int,[ename] varchar(6))
insert [huang]
select 1,'j' union all
select 1,'2' union all
select 0,'123' union all
select 34,'123' union all
select 34,'123' union all
select 34,'123' union all
select 34,'111' union all
select 34,'111' union all
select 45,'123' union all
select 46,'132' union all
select 46,'123' union all
select 47,'123' union all
select 47,'123' union all
select 50,'update' union all
select 54,'2' union all
select 55,'123' union all
select 55,'132' union all
select 56,'1323' union all
select 56,'123'
--------------开始查询--------------------------
SELECT a.*
FROM huang a INNER JOIN (
select ename,COUNT(1) [count]
from [huang]
WHERE ename IN ('123','111','132','1323')
GROUP BY ename) b ON a.ename=b.ename
ORDER BY b.count DESC

------------------结果----------------------------
/*
eidx ename
----------- ------
0 123
34 123
34 123
34 123
45 123
46 123
47 123
47 123
55 123
56 123
46 132
55 132
34 111
34 111
56 1323
*/
c254922398 2013-08-13
  • 打赏
  • 举报
回复
引用 2 楼 hdhai9451 的回复:
select * from (SELECT eName,eIDx,count(*) as count FROM pEdu WHERE eName LIKE '%1%' GROUP BY eIDx, eName )t order by case when eNmae='123' then 0 when eNmae='111' or eName='1323' then 9 else 1 end,count
这位朋友,路子好像有点不对吧,我这4个数是根据第一张表查询出来的,不是写死的呀
唐诗三百首 2013-08-13
  • 打赏
  • 举报
回复

select y.eName
from
(select t.eName,count(1) 'ct'
  from (SELECT eName,eIDx
        FROM pEdu 
        WHERE eName LIKE '%1%' 
        GROUP BY eIDx,eName) t
  group by t.eName
) y
order by ct desc
Shawn 2013-08-13
  • 打赏
  • 举报
回复
if OBJECT_ID('tempdb..#temp', 'u') is not null   drop table #temp;
CREATE TABLE #temp(eid INT, eidx INT,  ename VARCHAR(100),   elevel INT,  eflag INT)
insert #temp
select '2','1','j','1','1' union all
select '5','1','2','1','1' union ALL
select '9','0','123','0','0' union all
select '19','34','123','0','0' union all
select '20','34','123','0','0' union all
select '21','34','123','0','0' union all
select '22','34','111','0','0' union all
select '23','34','111','0','0' union all
select '24','45','123','0','0' union all
select '25','46','132','0','0' union all
select '26','46','123','0','0' union all
select '27','47','123','0','0' union all
select '28','47','123','0','0' union all
select '38','50','update','0','0' union all
select '48','54','2','0','0' union all
select '50','55','123','0','0' union all
select '51','55','132','0','0' union all
select '55','56','1323','0','0' union all
select '56','56','123','0','0' 

SELECT eName,CNT=COUNT(1)
FROM
(
	SELECT DISTINCT eName,eIDx FROM #temp 
	WHERE eName LIKE '%1%' 
) T
GROUP BY eName
ORDER BY COUNT(1) DESC
/*
eName	CNT
123	7
132	2
1323	1
111	1
*/
Andy__Huang 2013-08-13
  • 打赏
  • 举报
回复
select * from (SELECT eName,eIDx,count(*) as count FROM pEdu WHERE eName LIKE '%1%' GROUP BY eIDx, eName )t order by case when eNmae='123' then 0 when eNmae='111' or eName='1323' then 9 else 1 end,count
c254922398 2013-08-13
  • 打赏
  • 举报
回复
eidx ename 1 j 1 2 0 123 34 123 34 123 34 123 34 111 34 111 45 123 46 132 46 123 47 123 47 123 50 update 54 2 55 123 55 132 56 1323 56 123 第一张表有点乱,改了下,大家只看这两列就行了

34,576

社区成员

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

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