简体   繁体   English

SQL 后续分组行

[英]SQL sequent grouped rows

+------+------+------+------------+
| id   | room | type | created_at |
+------+------+------+------------+
| 8214*|   83 | msg* | 1571726466 |
| 8213 |   83 | msg* | 1571724983 |
| 8212 |   83 | ad   | 1571724982 |
| 8211 |   83 | msg  | 1571724978 |
| 8210 |   83 | msg  | 1570861659 |
| 8209 |   83 | msg  | 1570861656 |
| 8208 |   83 | msg  | 1570861650 |
| 8207 |   83 | ad   | 1570861643 |
| 8206 |   83 | msg  | 1570861632 |
| 8255*|   82 | msg* | 1571732235 |
| 8254 |   82 | msg* | 1571732235 |
| 8253 |   82 | msg* | 1571732235 |
| 8252 |   82 | msg* | 1571732235 |
| 8251 |   82 | msg* | 1571732234 |
| 8250 |   82 | ad   | 1571732234 |
| 8249 |   82 | msg  | 1571732234 |
| 8248 |   82 | msg  | 1571732234 |
| 8247 |   82 | msg  | 1571732234 |
| 8246 |   82 | msg  | 1571732234 |
| 8245 |   82 | msg  | 1571732233 |
| 8244 |   82 | msg  | 1571732233 |
| 8243 |   82 | msg  | 1571732233 |
| 8242 |   82 | ad   | 1571732232 |
| 8241 |   82 | msg  | 1571732232 |
| 8240 |   82 | msg  | 1571732232 |
| 8239 |   82 | msg  | 1571732231 |
| 8238 |   82 | msg  | 1571732231 |
| 8237 |   82 | ad   | 1571732231 |
| 8236 |   82 | msg  | 1571732231 |
| 8235 |   82 | msg  | 1571732231 |
| 8234 |   82 | msg  | 1571732230 |
| 8233 |   82 | msg  | 1571732230 |
| 8232 |   82 | msg  | 1571732230 |
| 8231 |   82 | msg  | 1571732230 |
| 8230 |   82 | msg  | 1571732230 |
| 8229 |   82 | msg  | 1571732229 |
| 8228 |   82 | msg  | 1571732228 |
| 8227 |   82 | msg  | 1571732228 |
| 8226 |   82 | msg  | 1571732228 |
| 8225 |   82 | msg  | 1571732227 |
| 8224 |   82 | msg  | 1571732227 |
| 8223 |   82 | ad   | 1571732227 |
| 8222 |   82 | msg  | 1571732226 |
| 8221 |   82 | msg  | 1571732223 |
| 8220 |   82 | msg  | 1571732223 |

I have table of 'messages'.我有“消息”表。 There could be regular message or ad.可能有常规消息或广告。 Each message belongs to room.每条消息都属于房间。 I need to get last messages by date in each room and in this selection must be count of each regular messages (type=msg) before first encounter of type 'ad' given that messages' order are descending.我需要按日期在每个房间中获取最后一条消息,并且在此选择中,必须在第一次遇到“广告”类型之前计算每条常规消息(类型 = msg),因为消息的顺序是降序的。

Expected will probably explain better what I need:预期可能会更好地解释我需要什么:

+------+------+------+------------+-----------+
| id   | room | type | created_at | count_msg |
+------+------+------+------------+-----------+
| 8214 |   83 | msg  | 1571726466 | 2         |
| 8255 |   82 | msg  | 1571732235 | 5         |
+------+------+------+------------+-----------+

In the source I marked with '*' which rows should count/showup in the result.在源代码中,我用“*”标记了哪些行应该在结果中计数/显示。

PS. PS。 Sorry if grammar is wrong.如果语法错误,请见谅。

UPD: In case of same created_at I guess it should order by id desc as well. UPD:如果是相同的 created_at,我想它也应该按 id desc 排序。

From your expected output it seems you need all the msg rows after the last ad occurs.从您预期的 output 看来,在最后一个广告出现后,您似乎需要所有 msg 行。 This an be easily achieved using a sub-query -这可以使用子查询轻松实现 -

SELECT MAX(T1.id), T1.room, type, MAX(created_at), COUNT(*) count_msg
FROM YOUR_TABLE T1
JOIN (SELECT MAX(id) id, room
      FROM YOUR_TAB
      WHERE type = 'ad'
      GROUP BY room) T2 ON T1.id > T2.id AND T1.room = T2.room
GROUP BY room, type

Here is the fiddle. 是小提琴。

You seem to want all messages after the last 'ad' -- at least based on your data.您似乎想要最后一个'ad'之后的所有消息 - 至少基于您的数据。

I think about filtering using a correlated subquery and aggregation:我考虑使用相关子查询和聚合进行过滤:

select t2.room, max(t.id), t.type, max(t.created_at), count(*)
from t
where t.id > (select max(t2.id)
              from t t2
              where t2.room = t.room and t2.type = 'ad'
             )
group by t.room, t.type;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM