Last active
March 30, 2017 11:18
-
-
Save iKrevedko/22082ed0478edd4666b9e257c2728867 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Запрос: | |
Актуально только для PostgreSQL, т.к. оконные функции есть не везде. | |
SELECT t.* FROM | |
( | |
SELECT | |
news.*, | |
row_number() OVER (PARTITION BY source ORDER BY source, CASE n_type WHEN 'realty' THEN 0 WHEN 'sport' THEN 1 ELSE 2 END, created_at DESC) as rnum | |
FROM news | |
) as t | |
WHERE t.rnum <= 5 OR t.n_type NOT IN ('sport', 'realty') ORDER BY t.source, t.rnum; | |
Схема: | |
CREATE TABLE news | |
( | |
"id" SERIAL, | |
"title" "text", | |
"source" "text", | |
"n_type" "text", | |
"created_at" "timestamp", | |
CONSTRAINT "news_pkey" PRIMARY KEY ("id") | |
); | |
Данные: | |
INSERT INTO news (title, source, n_type, created_at) VALUES('news0', '0', 'realty', '2017-03-30 10:40:00 +0000'); | |
INSERT INTO news (title, source, n_type, created_at) VALUES('news1', '0', 'realty', '2017-03-31 10:40:00 +0000'); | |
INSERT INTO news (title, source, n_type, created_at) VALUES('news2', '0', 'sport', '2017-04-01 10:40:00 +0000'); | |
INSERT INTO news (title, source, n_type, created_at) VALUES('news3', '0', 'sport', '2017-04-02 10:40:00 +0000'); | |
INSERT INTO news (title, source, n_type, created_at) VALUES('news4', '0', 'sport', '2017-04-03 10:40:00 +0000'); | |
INSERT INTO news (title, source, n_type, created_at) VALUES('news5', '0', 'sport', '2017-04-04 10:40:00 +0000'); | |
INSERT INTO news (title, source, n_type, created_at) VALUES('news6', '0', 'sport', '2017-04-05 10:40:00 +0000'); | |
INSERT INTO news (title, source, n_type, created_at) VALUES('news7', '0', 'other', '2017-04-06 10:40:00 +0000'); | |
INSERT INTO news (title, source, n_type, created_at) VALUES('news8', '0', 'other', '2017-04-07 10:40:00 +0000'); | |
INSERT INTO news (title, source, n_type, created_at) VALUES('news9', '0', 'other', '2017-04-08 10:40:00 +0000'); | |
INSERT INTO news (title, source, n_type, created_at) VALUES('news10', '1', 'realty', '2017-04-09 10:40:00 +0000'); | |
INSERT INTO news (title, source, n_type, created_at) VALUES('news11', '1', 'realty', '2017-04-10 10:40:00 +0000'); | |
INSERT INTO news (title, source, n_type, created_at) VALUES('news12', '1', 'realty', '2017-04-11 10:40:00 +0000'); | |
INSERT INTO news (title, source, n_type, created_at) VALUES('news13', '1', 'realty', '2017-04-12 10:40:00 +0000'); | |
INSERT INTO news (title, source, n_type, created_at) VALUES('news14', '1', 'realty', '2017-04-13 10:40:00 +0000'); | |
INSERT INTO news (title, source, n_type, created_at) VALUES('news15', '1', 'realty', '2017-04-14 10:40:00 +0000'); | |
INSERT INTO news (title, source, n_type, created_at) VALUES('news16', '1', 'sport', '2017-04-15 10:40:00 +0000'); | |
INSERT INTO news (title, source, n_type, created_at) VALUES('news17', '1', 'other', '2017-04-16 10:40:00 +0000'); | |
INSERT INTO news (title, source, n_type, created_at) VALUES('news18', '1', 'other', '2017-04-17 10:40:00 +0000'); | |
INSERT INTO news (title, source, n_type, created_at) VALUES('news19', '1', 'other', '2017-04-18 10:40:00 +0000'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment