I have a very unique query that I must write, and hoping to do it all in 1 query, but not sure if I can.
I have a list of Articles, each article has a unique ID. The application passes this ID to the stored procedure. Then, I am to retrieve that article, AND, the next & previous articles. So, the list is sorted by date, and I can get the next & previous.
I do this via LEAD & LAG. It works in one query. However, in some cases, that next or previous article has a NULL in one of the fields. If that field is NULL, I am basically to get the next article where that field is NOT NULL.
Then there is one more thing. The article passed from the application has a category assigned to it. The next & previous articles must be of the same category.
The query is pretty big now, but it work getting the next & previous to the article ID passed as the subquery sorts everything by date. But with these 2 new criteria, the NULL factor and category factor, I do not see how it is possible to do this in one query.
Any thoughts? Or need some examples, or my existing query?
Thanks for all your time.