Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
maxald
Starting Member
7 Posts |
Posted - 2007-09-05 : 01:57:42
|
I have a table with registered meetings like this:id time1 3:001 4:002 3:002 5:00 3 4:00 I also have a table that contains allowed meeting times:id time1 3:002 4:003 5:00 Now I need to know what meeting times are available for each id from my first table. This is the result I'd like to get from this example:id free1 5:002 4:003 3:003 5:00 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-09-05 : 02:05:10
|
[code]DECLARE @id TABLE( [id] int)INSERT INTO @idSELECT 1 UNION ALLSELECT 2 UNION ALLSELECT 3DECLARE @registered TABLE( [id] int, [time] datetime)INSERT INTO @registeredSELECT 1, '3:00' UNION ALLSELECT 1, '4:00' UNION ALLSELECT 2, '3:00' UNION ALLSELECT 2, '5:00' UNION ALLSELECT 3, '4:00'DECLARE @allowed TABLE( [id] int, [time] datetime)INSERT INTO @allowedSELECT 1, '3:00' UNION ALLSELECT 2, '4:00' UNION ALLSELECT 3, '5:00'SELECT i.[id], a.[time]FROM @id i CROSS JOIN @allowed a left JOIN @registered r ON i.[id] = r.[id] AND a.[time] = r.[time]WHERE r.id IS NULL/*id time ----------- ------------------------------------------------------ 1 1900-01-01 05:00:00.000 2 1900-01-01 04:00:00.000 3 1900-01-01 03:00:00.000 3 1900-01-01 05:00:00.000*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
maxald
Starting Member
7 Posts |
Posted - 2007-09-05 : 02:50:54
|
Works beautifully, huge thanks! I have been troubled by this problem for 2 days and you solved it in a minute... how did you do it? Plain knowledge!? |
 |
|
|
|
|
|
|
|