Note that there are some explanatory texts on larger screens.

plurals
  1. POIf Logic in a SQL Statement
    primarykey
    data
    text
    <p>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:</p> <p>Table:</p> <ul> <li>Id int (not null)</li> <li>ParentId int (not null)</li> <li>EventOn DateTime (not null)</li> <li>User int (null)</li> </ul> <p>By the following select:</p> <pre><code>SELECT RST.* FROM RangeSheetTime RST WHERE RST.[User] is not null </code></pre> <p>(in the above case, I take all the rows where the user isn't null)</p> <pre><code>Select RST.* FROM RangeSheetTime RST WHERE RST.[User] is null </code></pre> <p>(in the above case, I take all the rows where the user is null)</p> <p>So what am I trying to do? I want to build a select statement that when given a condition, such as EventOn &lt; 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.</p> <p>How can I put this to work?</p> <p>Note: I can't use if here, otherwise this would be easier.</p> <p>EDIT:</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload