分组筛选的基本思路是增加行号字段。
sqlserver分组行号添加方法:
SELECT t.a, t.b
FROM ( SELECT f.a,f.b, @rank: = IF( @b <>f.b, IF( @a =f.a, @rank + 1, 1), @rank ) rank, @a: = f.a, @b: = f.b
FROM ( SELECT a,b FROM `SAM` ORDER BY a,b) f, ( SELECT @rank: = 1, @a: = null, @b: = null) tt ) t
WHERE rank = 3;
sqlserver分组行号添加方法:
select
*
from
(
select
ROW_NUMBER()over(PARTITION
By
OrderName
ORDER
BY
RequestDate
DESC
)
as
rownum, OrderCount,OrderName
from
#Order1
) T
where
T.rownum = 1
mysql分组行号添加方法:
SELECT t.a, t.b
FROM ( SELECT f.a,f.b, @rank: = IF( @b <>f.b, IF( @a =f.a, @rank + 1, 1), @rank ) rank, @a: = f.a, @b: = f.b
FROM ( SELECT a,b FROM `SAM` ORDER BY a,b) f, ( SELECT @rank: = 1, @a: = null, @b: = null) tt ) t
WHERE rank = 3;