I can’t write a request with grouping as in the social news feed.
For example, there is a table of all news (which is connected by a polymorphic link with tables in which a detailed description of the news) columns: id, user_id, message_id, message_type;
id: 1, user_id: 1, message_id: 4, message_type: 'Warning' id: 2, user_id: 1, message_id: 5, message_type: 'Warning' id: 3, user_id: 1, message_id: 6, message_type: 'Warning' id: 4, user_id: 2, message_id: 4, message_type: 'Error' id: 5, user_id: 2, message_id: 1, message_type: 'Exception' id: 6, user_id: 1, message_id: 2, message_type: 'Exception' id: 7, user_id: 1, message_id: 3, message_type: 'Exception' id: 8, user_id: 2, message_id: 4, message_type: 'Exception' It is impossible to write a request that will give the latest news grouped by the same successive message_type for a user.
For example:
id: 8, user_id: 2, message_id: 4, message_type: 'Exception' id: {6,7} user_id: 1, message_id: {2,3}, message_type: 'Exception' id: 5, user_id: 2, message_id: 1, message_type: 'Exception' id: 4, user_id: 2, message_id: 4, message_type: 'Error' id: {1, 2, 3}, user_id: 1, message_id: {4, 5}, message_type: 'Warning' I do not want to use subqueries, because the table is large. Will be retrieved by 20 news. PostgreSQL database. How can this be done?
PS I asked a question in an English resource, but either I did not correctly ask it, or they did not understand me. https://stackoverflow.com/questions/30227391/postgree-double-group-by-repeating-attribute