有一张表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 0SELECT 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语句应该怎么写?最好是用一条子查询语句,存储过程就算了,求大牛解答疑惑
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 0SELECT 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语句应该怎么写?最好是用一条子查询语句,存储过程就算了,求大牛解答疑惑
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第一张表有点乱,改了下,大家只看这两列就行了
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
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
*/
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
*/
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)
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
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直接复制过来报错,自己改了下好用了,谢谢这位朋友。也感谢大家!
可以准点下班了