久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

mysql中如何查詢前50%的數據

141次閱讀
沒有評論

共計 1414 個字符,預計需要花費 4 分鐘才能閱讀完成。

這篇文章給大家分享的是有關 mysql 中如何查詢前 50% 的數據的內容。丸趣 TV 小編覺得挺實用的,因此分享給大家做個參考,一起跟隨丸趣 TV 小編過來看看吧。

實驗環境:

create table t (c int);
insert into t values 
(15),(7),(9),(10),(7),(8),(20),(16),(9),(19),
(14),(10),(11),(10),(10),(12),(7),(10),(7),(9);
commit; 

c 等于 10,7,9 的數量有   11 個,超過了半數。
超過半數的 item 信息。

通用方式, 不依賴具體數據庫。

select t5.*,t6.*,ifnull(round(t5.sumtotal/t6.total,2),0) result from (

 select t3.rn,t3.total,t3.c,sum(ifnull(t4.total,0)) sumtotal from (

 select

 t1.*,

 (

 select count(case when t2.total t1.total then 1 when t2.total=t1.total and t2.c t1.c then 1 else null end)+1 from (

 select c,count(*) total from t group by c

 ) t2

 ) rn

 from(

 select c,count(*) total from t group by c order by total desc ,c

 ) t1

 ) t3

 left join(

 select

 t1.*,

 (

 select count(case when t2.total t1.total then 1 when t2.total=t1.total and t2.c t1.c then 1 else null end)+1 from (

 select c,count(*) total from t group by c

 ) t2

 ) rn

 from(

 select c,count(*) total from t group by c order by total desc ,c

 ) t1

 ) t4 on (t3.rn t4.rn)

 group by t3.rn,t3.total,t3.c

) t5

left join(

 select count(*) total from t

) t6 on(1=1)

where ifnull(round(t5.sumtotal/t6.total,2),0) =0.5;

查詢結果:

綁定 MySQL 的實現

select * from (

 select t3.*,case when result =0.5 then 1 when result 0.5 and @b=-1 then @b:=1 else null end r from (

 select t1.*,round((@a:=@a+t1.total)/t2.total,2) result from (

 select c,count(*) total from t,(select @a:=0,@b:=-1) vars group by c order by 2 desc

 ) t1

 left join(

 select count(*) total from t

 ) t2 on(1=1)

 ) t3 order by result

) t4 where r=1;

結果:

感謝各位的閱讀!關于“mysql 中如何查詢前 50% 的數據”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-07-27發表,共計1414字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 大庆市| 黔江区| 吉安市| 个旧市| 磐石市| 龙州县| 皋兰县| 佳木斯市| 普兰县| 喜德县| 奉新县| 华亭县| 恩施市| 龙海市| 康马县| 嘉定区| 色达县| 阜新| 凤台县| 忻城县| 张家川| 静乐县| 孟州市| 简阳市| 宕昌县| 治多县| 那曲县| 玛多县| 大荔县| 岑溪市| 冕宁县| 淄博市| 交口县| 吐鲁番市| 金坛市| 洪雅县| 邵阳市| 泰兴市| 永和县| 襄樊市| 固原市|