sql SQL中每组的第一个/最小/最大行数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql SQL中每组的第一个/最小/最大行数相关的知识,希望对你有一定的参考价值。

+--------+------------+-------+
| type   | variety    | price |
+--------+------------+-------+
| apple  | gala       |  2.79 | 
| apple  | fuji       |  0.24 | 
| apple  | limbertwig |  2.87 | 
| orange | valencia   |  3.59 | 
| orange | navel      |  9.36 | 
| pear   | bradford   |  6.05 | 
| pear   | bartlett   |  2.14 | 
| cherry | bing       |  2.55 | 
| cherry | chelan     |  6.33 | 
+--------+------------+-------+


#1 self join
select f.type, f.variety, f.price
from (
   select type, min(price) as minprice
   from fruits group by type
) as x inner join fruits as f on f.type = x.type and f.price = x.minprice;


#2 correlated subquery, less efficient
select type, variety, price
from fruits
where price = (select min(price) from fruits as f where f.type = fruits.type);


+--------+----------+-------+
| type   | variety  | price |
+--------+----------+-------+
| apple  | fuji     |  0.24 | 
| orange | valencia |  3.59 | 
| pear   | bartlett |  2.14 | 
| cherry | bing     |  2.55 | 
+--------+----------+-------+


###################################
# $n from each
###################################

#1  self join?
select type, variety, price
from fruits
where (
   select count(*) from fruits as f
   where f.type = fruits.type and f.price <= fruits.price
) <= 2;



#2 union
(select * from fruits where type = 'apple' order by price limit 2)
union all
(select * from fruits where type = 'orange' order by price limit 2)
union all
(select * from fruits where type = 'pear' order by price limit 2)
union all
(select * from fruits where type = 'cherry' order by price limit 2)


#3a variables 
set @num := 0, @type := '';

select type, variety, price
from (
   select type, variety, price,
      @num := if(@type = type, @num + 1, 1) as row_number,
      @type := type as dummy
  from fruits
  order by type, price
) as x where x.row_number <= 2;


#3b variables with force without parent subquery
set @num := 0, @type := '';

select type, variety, price,
      @num := if(@type = type, @num + 1, 1) as row_number,
      @type := type as dummy
from fruits force index(type)
group by type, price, variety
having row_number <= 2;

-- s využitím korelovaného poddotazu pro nalezení hranice
select c2.*
from   clanky c2
join   (select k.id_kategorie,
               (select id_clanku
                from   clanky c
                where  c.id_kategorie = k.id_kategorie
                order  by id_clanku desc
                limit 4, 1) hranice
        from   kategorie k) h on c2.id_kategorie = h.id_kategorie
                                 and c2.id_clanku >= ifnull(h.hranice, c2.id_clanku)
                                 
 
 -- s využitím uživatelských proměnných
-- (pozor, při složitých dotazech se může přehodit vyhodnocování proměnných a nefunguje to ok
select c.*,
       @n := @n * (@last_idk = c.id_kategorie) + 1 n,
       @last_idk := c.id_kategorie k
from   clanky c, (select @n := 0, @last_idk := '') t
having n <= 5
order  by c.id_kategorie, c.id_clanku desc

以上是关于sql SQL中每组的第一个/最小/最大行数的主要内容,如果未能解决你的问题,请参考以下文章

如何计算熊猫数据框中每组的行数并将其添加到原始数据中

sql按字段分组,并且找出每组的第一条数据

sql 怎么分组取行数最大的一条

PowerPivot:如何识别计算列中每组的最大值

SQL:查找每组的最大记录[重复]

SQL:查找每组的最大记录[重复]