Author |
Topic |
r4
Starting Member
6 Posts |
Posted - 2010-09-22 : 14:13:11
|
I'd like to get all records from one table and the records that are common to both. On the right table needs a where clause and that where clause results in not all records from the left table. I've tried several things (i.e unions), but without luck. Here's the code and rough example of what I'd like as results. Basically I'd like be able to report on the empty sites.select S.MapIdNumber [Site], M.LastName [Member], convert(varchar(10),(R.StartDate),101) [Start], convert(varchar(10),(R.EndDate),101) [End], R.StatusId [Status]from Sites S left outer join Reservations R on S.Id = R.SiteId, Members Mwhere R.MemberId = M.Idand S.ResortId = 1and R.StatusId = 31Site # Member Start End Status1 2 Hiney 9/15/2010 9/20/2010 312 3 4 Davis 9/16/2010 9/19/2010 315 6 Little 9/15/2010 9/18/2010 317 8 Parker 9/16/2010 9/19/2010 319 Stitt 9/15/2010 9/19/2010 3110 Cyr 9/17/2010 9/20/2010 31 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-09-22 : 15:38:03
|
Hard to tell from your query and description what you want. What would really help us to help you is some sample data and expected output from that sample data. here is a link that might help you prepare that information:http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxHere is a guess though:select S.MapIdNumber [Site], M.LastName [Member], convert(varchar(10),(R.StartDate),101) [Start], convert(varchar(10),(R.EndDate),101) [End], R.StatusId [Status]from Sites S left outer join ( SELECT * FROM Reservations R INNER JOIN Members M ON R.MemberId = M.Id WHERE R.StatusId = 31 ) AS R ON S.Id = R.SiteIdWHERE S.ResortId = 1 |
 |
|
r4
Starting Member
6 Posts |
Posted - 2010-09-23 : 09:24:29
|
Thanks for the reply ... here's some more info.Sites Table Resort Id MapId1 1 M11 2 M21 3 M31 4 M41 5 M51 6 M61 7 M71 8 M81 9 M91 10 M102 1 M12 2 M22 3 M32 4 M42 5 M52 6 M6Members Table Id Name1 Hiney2 Davis3 Little4 Parker5 Stitt6 CyrReservation Table Id MemberId Resort SiteId StartDate FinishDate Status20 1 1 2 9/15/2010 9/20/2010 3121 2 1 4 9/16/2010 9/19/2010 3122 3 1 6 9/15/2010 9/18/2010 3123 4 1 8 9/16/2010 9/19/2010 3124 5 1 9 9/15/2010 9/19/2010 3125 6 1 10 9/17/2010 9/20/2010 31Expected ResultSite # Member Start End StatusM1 No camperM2 Hiney 9/15/2010 9/20/2010 31M3 No camperM4 Davis 9/16/2010 9/19/2010 31M5 No camperM6 Little 9/15/2010 9/18/2010 31M7 No camperM8 Parker 9/16/2010 9/19/2010 31M9 Stitt 9/15/2010 9/19/2010 31M10 Cyr 9/17/2010 9/20/2010 31The report could then be used by staff to check campsites. (i.e. verify that site #'s M1,M3,M5,M7 are not occupied at resort #1) |
 |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-09-23 : 09:29:50
|
Not just this?:SELECT s.[mapID] AS [Site] , m.[name] AS [Member] , r.[startDate] AS [Start] , r.[finishDate] AS [End] , r.[status] AS [Status]FROM Sites AS s LEFT JOIN reservation AS r ON r.[siteID] = s.[ID] LEFT JOIN members AS m ON m.[ID] = r.[memberID]ORDER BY s.[mapID] ASC Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
r4
Starting Member
6 Posts |
Posted - 2010-09-23 : 09:32:45
|
Note: I changed your query a bit to this ...select S.MapIdNumber [Site], M.LastName [Member], convert(varchar(10),(R.StartDate),101) [Start], convert(varchar(10),(R.EndDate),101) [End], R.StatusId [Status]from Sites S left outer join ( SELECT * FROM Reservations R INNER JOIN Members M ON R.MemberId = M.Id WHERE R.StatusId = 31 AND R.ResortId = 1 << Added ) AS R ON S.Id = R.SiteIdWHERE S.ResortId = 1and got this message ...Msg 8156, Level 16, State 1, Line 1The column 'Id' was specified multiple times for 'R'. |
 |
|
r4
Starting Member
6 Posts |
Posted - 2010-09-23 : 09:43:00
|
Charlie ... Thanks for the reply. I need to use the reservation status otherwise all the old reservations will be selected.i.e R.StatusId = 31 --Checked In Status |
 |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-09-23 : 09:51:56
|
Then:SELECT s.[mapID] AS [Site] , m.[name] AS [Member] , r.[startDate] AS [Start] , r.[finishDate] AS [End] , r.[status] AS [Status]FROM Sites AS s LEFT JOIN reservation AS r ON r.[siteID] = s.[ID] AND r.[statusId] = 31 LEFT JOIN members AS m ON m.[ID] = r.[memberID]ORDER BY s.[mapID] ASC Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
r4
Starting Member
6 Posts |
Posted - 2010-09-23 : 11:14:25
|
Charlie ... that is very close. I get the not occupied sites. But I also am getting all sites twice.SELECT S.MapIdNumber AS [Site] , (M.LastName + ', ' + M.FirstName) AS [Member] , R.[StartDate] AS [Start] , R.[EndDate] AS [End] , R.TypeOfReservationStatusId AS [Status]FROM Sites AS S LEFT JOIN Reservations AS R ON R.SiteID = S.ID AND R.TypeOfReservationStatusId = 31 AND S.ResortId = 1 LEFT JOIN Members AS M ON M.Id = R.MemberIdORDER BY S.MapIdNumber ASC |
 |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-09-23 : 11:33:58
|
bring back the primary keys of each table. This will show you where the data duplication / mutltiple rows are.Regards,Charlie,.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
r4
Starting Member
6 Posts |
Posted - 2010-09-23 : 15:56:32
|
Since the results are exported to Excel I am just going to use Excel's Advanced Filter for unique rows.Thanks all. |
 |
|
|