饿虎岗资源网 Design By www.oxmxm.com

看代码吧~

WITH Name AS (
 SELECT
  *
 FROM
  (
   SELECT
    xzqdm,
    SUBSTRING (zldwdm, 1, 9) xzdm,
    COUNT (*) sl
   FROM
    sddltb_qc
   WHERE
    xzqdm IN ('130432', '210604')
   GROUP BY
    xzqdm,
    SUBSTRING (zldwdm, 1, 9)
  ) AS A
 ORDER BY
  xzqdm,
  xzdm,
  sl
) SELECT
 xzqdm,
 xzdm,
 sl
FROM
 (
  SELECT
   *, ROW_NUMBER () OVER (
    PARTITION BY xzqdm
    ORDER BY
     sl DESC
   ) AS Row_ID
  FROM
   Name
 ) AS A
WHERE
 Row_ID <= 2
ORDER BY
 xzqdm

其中

select * from (select xzqdm,substring(zldwdm,1,9) xzdm,count(*) sl from sddltb_qc where xzqdm in ('130432','210604') group by xzqdm,substring(zldwdm,1,9)) as a order by xzqdm,xzdm,sl

执行结果:

postgresql 实现取出分组中最大的几条数据

添加行序号:ROW_NUMBER () OVER (ORDER BY A.bsm ASC) AS 序号

分组添加序号:ROW_NUMBER () OVER (PARTITION BY xzqdm ORDER BY A.bsm ASC) AS 序号

补充:pgsql 表随机取几条数据

取100条

select * from map_route_info_composite order by random() limit 100

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

饿虎岗资源网 Design By www.oxmxm.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
饿虎岗资源网 Design By www.oxmxm.com