What I'm trying to do should be very simple but somehow I can't reach the right answer to my problem. I've asked something similar in the past but the answer given to me previously no longer fits the requirements. So here's what's going on - I need to conditionally select values from a table in my database in a different than the usual manner, like so:
Table:
- Id int (not null)
- ParentId int (not null)
- EventOn DateTime (not null)
- User int (null)
By the following select:
SELECT RST.* FROM RangeSheetTime RST
WHERE RST.[User] is not null
(in the above case, I take all the rows where the user isn't null)
Select RST.* FROM RangeSheetTime RST
WHERE RST.[User] is null
(in the above case, I take all the rows where the user is null)
So what am I trying to do? I want to build a select statement that when given a condition, such as EventOn < GETDATE(), will retrieve all the rows where the USER isn't null. In case there aren't any rows where USER isn't null, then it should retrieve the rows where it is null, if any.
How can I put this to work?
Note: I can't use if here, otherwise this would be easier.
EDIT:
I'm going to try to explain it the best I can. Imagine I have 3 rows for the same ParentId, 31. 2 of these rows have a column named StartOrEnd set to 1. There's just a difference between them, for the 1st one, the USER column is null; for the 2nd one, the USER column has the value 90. The 3rd row has the column StartOrEnd set to 0. Now, I want to display results no matter the value of startorend. But there's a catch. For every startorend, if there are more than 1 row and one of them has USER set to null and the others not null, then only the non null rows for that startorend will display. but in case there are no non null rows for this condition, than the null values will display. I hope I was clear now.