每个类别获得10条最新记录,甚至有些记录少于10条

时间:2018-04-15 22:09:17

标签: mysql

这是我的表格的例子:

+-----+-----+------------+--------+-------------+--------------+
| LID | AID | Created    | TypeID | PaymentDate | PaymentValue |
+-----+-----+------------+--------+-------------+--------------+
| 1   | 529 | 2017-05-12 | 1      | 2017-05-12  | 100          |
+-----+-----+------------+--------+-------------+--------------+
| 2   | 529 | 2018-04-10 | 4      | 2018-04-10  | 200          |
+-----+-----+------------+--------+-------------+--------------+
| 3   | 441 | 2014-01-23 | 3      | 2014-01-23  | 300          |
+-----+-----+------------+--------+-------------+--------------+
| 4   | 324 | 2017-09-14 | 1      | 2017-09-14  | 400          |
+-----+-----+------------+--------+-------------+--------------+
| 5   | 111 | 2018-05-12 | 0      | 2018-05-12  | 340          |
+-----+-----+------------+--------+-------------+--------------+
| 6   | 529 | 2018-05-12 | 1      | 2018-05-12  | 100          |
+-----+-----+------------+--------+-------------+--------------+
| 7   | 529 | 2018-06-12 | 1      | 2018-05-12  | 100          |
+-----+-----+------------+--------+-------------+--------------+
| 8   | 529 | 2018-07-12 | 1      | 2018-05-12  | 100          |
+-----+-----+------------+--------+-------------+--------------+
| 9   | 529 | 2018-08-12 | 1      | 2018-05-12  | 100          |
+-----+-----+------------+--------+-------------+--------------+
| 10  | 529 | 2018-09-12 | 1      | 2018-05-12  | 100          |
+-----+-----+------------+--------+-------------+--------------+
| 11  | 529 | 2018-01-12 | 1      | 2018-05-12  | 100          |
+-----+-----+------------+--------+-------------+--------------+
| 12  | 529 | 2018-05-14 | 1      | 2018-05-12  | 100          |
+-----+-----+------------+--------+-------------+--------------+
| 13  | 529 | 2018-05-21 | 1      | 2018-05-12  | 100          |
+-----+-----+------------+--------+-------------+--------------+
| 14  | 529 | 2018-03-12 | 1      | 2018-05-12  | 100          |
+-----+-----+------------+--------+-------------+--------------+

这是另一张表

+-----+-------+
| ID  |caption|
+-----+-------+
| 0   | bad   |
+-----+-------+
| 1   | good  |
+-----+-------+

每个AID我需要获得10条最新记录。如果某些AID的记录少于10条,我需要获得10行,并将“无付款日期”放入PaymentDate和Created字段,Null为TypeID,0为PaymentValue。我可以用

获得10条或更少的最新记录
select *
from (select *,
         (@rn := if(@c = AID, @rn + 1,
                    if(@c := AID, 1, 1)
                   )
         ) as rn
from history cross join
       (select @rn := 0, @c := -1) params
order by AID, Created desc
) t
having rn <= 10;

但我不知道如何强制mysql为每个AID输出10行。请帮帮我。

结果应采用形式
AID,TYPEID,创建,标题

2 个答案:

答案 0 :(得分:0)

我做到了。 此查询需要创建一行10条记录,以与表中的不同AID值组合。我能够显示金额和创建日期的结果,并将留给你继续,因为你会得到这个想法。 关键部分是构建一个具有10行不同AID的表,因此表r中有大约40行。然后对表t执行左连接,这与您所做的类似。表t获得最多10个记录的排名。任何缺失的等级最多10个recs将由表r填写。 Coalesce将分配默认值,例如0到金额和&#39;没有创建日期&#39;为了约会。

http://sqlfiddle.com/#!9/855c21/2

SELECT coalesce(r.aid, t.aid) as aid,
   coalesce(t.paymentvalue, 0) as paymentvalue,
   coalesce(cast(t.created as char), 'no create date') as created
FROM (select * from (
 select 1 as rw union
  select 2 union select 3 
  union select 4 union select 5
  union select 6 union select 7
  union select 8 union select 9
  union select 10) u
 cross join (select distinct aid
     from history) h
   ) as r
LEFT JOIN (
SELECT a.aid, a.paymentvalue, 
  a.created, count(*) rn
FROM history a
JOIN history b 
ON a.aid = b.aid
AND a.created <= b.created
GROUP BY a.aid, a.created
HAVING COUNT(*) <= 10) t
on r.rw=t.rn and r.aid=t.aid
order by aid, created;

答案 1 :(得分:0)

我已添加RIGHT JOIN以引入空行,以便每AID个最多10行(或n行)。最初我使用SELECT 1 UNION SELECT 2 ...生成10行。为了更容易增加行数(比如说100),我正在尝试generate_series equivalent for mysql这个想法。为了使其正常工作,history表中的行数必须大于每AID所需的行数。

select t1.lid
,t2.aid 
,coalesce(t1.created, "no created date") as created
,t1.typeID 
,coalesce(t1.paymentdate, "no payment date") as paymentDate
,coalesce(t1.paymentvalue, 0) as paymentValue
,t2.rn 
from
(
 select *,
         (@rn := if(@c = AID, @rn + 1,
                    if(@c := AID, 1, 1)
                   )
         ) as rn
 from history cross join
 (select @rn := 0, @c := -1) params
 order by AID, Created desc
) t1
right join
( select * 
  from (select  distinct aid from history ) h1 
        cross join 
       (select rn  -- generate table with n rows numbered from 1 to n
        from
        (select 
         @num:= 0) init
         cross join
        (select @num  := @num +1  rn
         from history ) t -- assume history has at least 10 rows
         limit 
         10 ) h2 -- n = 10; change it to the number of rows per aid required
    ) t2
    on t1.aid = t2.aid and t1.rn = t2.rn
    order by t2.aid, t2.rn