У меня есть таблица с элементами, похожими на следующие, которая содержит имена элементов, SoldDate (если продано) и DisplayInWebsiteDate (это дата, до которой этот элемент должен отображаться на веб-сайте, если не продан)
Id ItemName IsSold SoldDate DisplayInWebsiteDate
-------------------------------------------------------------------------
1 Shirt 0 NULL 2020-03-28
2 Pant 1 2019-10-20 2020-04-25
3 Jacket 1 2020-01-05 2020-01-20
4 Trouser 0 NULL 2020-01-10
Я хочу ВЫБРАТЬ элементы, основанные на следующих условиях:
1. If item is not Sold i.e SoldDate is NULL then check if its DisplayInWebsiteDate is
greater than current date and that would be valid
2. If item is Sold i.e SoldDate has some date then ignore DisplayInWebsiteDate and check if that item was
sold within last 30 days, and display. If it was sold more than 30 days earlier, then don't get that record
Сегодняшняя дата - 2020-01-22. Следовательно, результат будет следующим
1. Shirt is VALID because it is not sold, and DisplayInWebsiteDate is greater than today's date
2. Pant is INVALID because it is sold, so we ignore its DisplayInWebsiteDate.
And its sold date has passed more than 30 days form today's date
3. Jacket is VALID because it was sold just 17 days ago i.e it falls within range of 30 days sold date
4. Trouser is INVALID because it is not sold and its DisplayInWebsiteDate has already passed on January 10, 2020
Ожидаемый результат:
Id ItemName IsSold SoldDate DisplayInWebsiteDate
-------------------------------------------------------------------------
1 Shirt 0 NULL 2020-03-28
3 Jacket 1 2020-01-05 2020-01-20