Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Server Creating complicated SQL Query for 3 tables
    primarykey
    data
    text
    <p>Please help me creating this SQL query for SQL Server 2008.</p> <p>I've wasted 1 day thinking of this but I cannot find the solution:</p> <p>I have 3 [tables] with respective (columns):</p> <ul> <li><code>[table_users]</code> TB1 <code>(Id, Name)</code></li> <li><code>[table_schedule]</code> TB2 <code>(Id, userId, dateScheduled)</code></li> <li><p><code>[table_schedule_confirmation]</code> TB3 <code>(Id, scheduleId, confirmationStatus)</code></p> <p>confirmationStatus -> 0 = cancelled, 1 = confirmed</p></li> </ul> <p>I must filter <code>[TB1]</code> by selecting a day, it must list only user names that:</p> <ul> <li>are not scheduled at all (<code>userId</code> doesn't exist in <code>[TB2]</code> for selected day)</li> </ul> <p>or</p> <ul> <li>if scheduled, there's a matching confirmation for (<code>scheduleId</code>) in <code>[TB3]</code> and (<code>confirmationStatus</code>) must be 0 (cancelled)</li> </ul> <p>Problems:</p> <ul> <li>There can be more than one entries in <code>TB2</code> and <code>TB3</code> for a <code>userId</code> and date</li> </ul> <p>Date Filter = 31/03/2013</p> <p>Situation 1 (1 confirmed schedule):</p> <p><a href="http://img255.imageshack.us/img255/1508/sqld.png" rel="nofollow noreferrer">Situation1 http://img255.imageshack.us/img255/1508/sqld.png</a></p> <p>As you can see, I've scheduled 4 times for user 1, but he cancelled the 3 first, confirming only on the 4th time.</p> <p>Situation 2 (1 pending schedule):</p> <p><a href="http://img854.imageshack.us/img854/8116/sql2s.png" rel="nofollow noreferrer">Situation1 http://img854.imageshack.us/img854/8116/sql2s.png</a></p> <p>In this case, user hasn't confirmed the 4th (id=16) schedule yet.</p> <p>Situation 3 (all schedules cancelled):</p> <p><a href="http://img41.imageshack.us/img41/5720/sql3s.png" rel="nofollow noreferrer">Situation1 http://img41.imageshack.us/img41/5720/sql3s.png</a></p> <p>User cancelled all the 4 schedules.</p> <p>Situation 4 (no schedules for user on that date):</p> <p><a href="http://img255.imageshack.us/img255/9679/sql4.png" rel="nofollow noreferrer">Situation1 http://img255.imageshack.us/img255/9679/sql4.png</a></p> <p>No schedules for selected day.</p> <p>In resume:</p> <p>Filtering by date, I must list only users that applies to Situations 3 and 4 and NOT applies to Situations 1 and 2 !</p> <p>I must make this filter, to populate a dropdown with only users that are available for that day.</p> <p>This is what I've tried so far:</p> <pre><code>DECLARE @dataAg datetime SET @dataAg = '2013/03/31 16:30' SELECT T1.id, T1.name FROM [tbl_users] T1 WHERE (NOT EXISTS (SELECT * FROM [tbl_schedule] T2 WHERE T2.userId = T1.id AND CONVERT(date,T2.scheduledDate) = CONVERT(date, @dataAg)) ) OR (EXISTS (SELECT * FROM [tbl_schedule] T2, [tbl_scheduleConfirmation] T3 WHERE T2.id = T3.idSchedule AND T2.userId = T1.id AND CONVERT(date,T2.scheduledDate) = CONVERT(date, @dataAg)) AND NOT EXISTS (SELECT * FROM [tbl_schedule] T2, [tbl_scheduleConfirmation] T3 WHERE T2.id = T3.idSchedule AND T3.confirmation = 1 AND T2.idUser = T1.id AND CONVERT(date, T2.scheduledDate) = CONVERT(date, @dataAg)) ) </code></pre>
    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.
 

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