I'm working on a typical blog app and have a view that returns the following data:
| post_id | title | publish_on | tag_id | tag_name |
| 1 | Why is Postgres awesome | 2014-09-02 | 1 | tech |
| 1 | Why is Postgres awesome | 2014-09-02 | 2 | postgres |
| 2 | How to ask a question on stackoverflow | 2014-09-10 | 1 | tech |
| 2 | How to ask a question on stackoverflow | 2014-09-10 | 2 | postgres |
| 2 | How to ask a question on stackoverflow | 2014-09-10 | 3 | guide |
| 3 | This is a draft | null | null | null |
| 4 | This is something else without a tag | 2014-10-10 | null | null |
| 5 | This question is also published on 9/2 | 2014-09-02 | null | null |
| 6 | And so is this | 2014-09-02 | 1 | tech |
| 7 | But this one is on 9/10 | 2014-09-10 | 3 | guide|
| 8 | This is on 10/10 | 2014-10-10 | null | null |
| 9 | And so is this | 2014-10-10 | 2 | postgres |
| 10| This is another draft | null | null | null |
I'm looking to group the posts by publish_on date and then select the top 3 posts for each bucket(this will be presented in the dashboard so that the user can know what posts will be published today, sometime next week and later) Now I've tried these solutions which use something like:
ROW_NUMBER() OVER (PARTITION BY publish_on ORDER BY publish_on DESC)
but since rows can be duplicated due to multiple tags, those queries fail. I've also tried a combination of various PARTION BY
criteria, but I guess I don't understand them well enough to get it working.
Any help/pointers appreciated!
Update: Expected Output
For each publish_on date, I want to get N (3) posts expected to be published on that date.
| 1 | Why is Postgres awesome | 2014-09-02 | 1 | tech |
| 1 | Why is Postgres awesome | 2014-09-02 | 2 | postgres |
| 5 | This question is also published on 9/2 | 2014-09-02 | null | null |
| 6 | And so is this | 2014-09-02 | 1 | tech |
| 2 | How to ask a question on stackoverflow | 2014-09-10 | 1 | tech |
| 2 | How to ask a question on stackoverflow | 2014-09-10 | 2 | postgres |
| 2 | How to ask a question on stackoverflow | 2014-09-10 | 3 | guide |
| 7 | But this one is on 9/10 | 2014-09-10 | 3 | guide|
| 4 | This is something else without a tag | 2014-10-10 | null | null |
| 8 | This is on 10/10 | 2014-10-10 | null | null |
| 9 | And so is this | 2014-10-10 | 2 | postgres |
| 3 | This is a draft | null | null | null |
| 10| This is another draft | null | null | null |
Hope this makes the question a little more clear to understand.