求一个sql语句,快下班了,急求!!
有一张表pEdu,数据如下
eid eidx ename elevel eflag
21j11
51211
9012300
193412300
203412300
213412300
223411100
233411100
244512300
254613200
264612300
274712300
284712300
3850update00
4854200
505512300
515513200
5556132300
565612300
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语句应该怎么写?最好是用一条子查询语句,存储过程就算了,求大牛解答疑惑
[解决办法]
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
/*
eNameCNT
1237
1322
13231
1111
*/
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
----------------------------
-- 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
*/
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排序
SELECT eName,eIDx
FROM pEdu
WHERE eName LIKE '%1%'
GROUP BY eIDx, eName
order by COUNT(eName)
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
/*eNameeIDxCount
12301
123451
123461
123551
123561
123472
123343
132461
132551
1323561
111342
*/