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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Join with Table Aliases

Author  Topic 

interclubs
Yak Posting Veteran

63 Posts

Posted - 2003-12-10 : 14:53:21
Been sturggling with this one a bit, and can't my head around the join and alias all together....

Have tables that look somewhat like this:

Invite
--------
InviteID
InviteName

Guests
----------
GuestID
InviteID
Name
Type (Will be either 'A' or 'S'


What I want to do is select all the info from invite with a join on Guests on InviteID where InviteID.Type='A'
But then I want to join again, only where InviteID.Type='S'

So the end results of the query would be

InviteID, InviteName, GuestID(Type='A'), Name (Type='A'), (GuestID (Type='S')) as SGuestID, (Name (Type='S')) as SName

Any help is greatly aprpeciated before I go nuts. Thanks!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-12-10 : 14:56:07
SELECT i.*
FROM invite i
INNER JOIN guests g ON i.InviteID = g.InviteID
WHERE i.Type = 'A' OR i.Type = 'S'

Is that what you are looking for?

Tara
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-12-10 : 14:59:15
SELECT *
FROM Invite i
LEFT JOIN Guests g1
ON i.InviteID = g1.InviteID
LEFT JOIN Guests g2
ON i.InviteID = g2.InviteID
WHERE g1.Type = 'A' AND g2.Type = 'S'

Is that what you're looking for or...

SELECT *
FROM Invite i
LEFT JOIN Guests g1
ON i.InviteID = g1.InviteID
WHERE g1.Type = 'A'
UNION ALL
SELECT *
FROM Invite i
LEFT JOIN Guests g2
ON i.InviteID = g2.InviteID
WHERE g1.Type = 'S'


Pick a flavor...



Brett

8-)
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-12-10 : 15:05:14
quote:

SELECT *
FROM Invite i
LEFT JOIN Guests g1
ON i.InviteID = g1.InviteID
LEFT JOIN Guests g2
ON i.InviteID = g2.InviteID
WHERE g1.Type = 'A' AND g2.Type = 'S'



uh oh Brett -- you just broke a rule !! never left join to a table and then apply a condition to the outer tables -- it then becomes an INNER JOIN.

- Jeff
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-12-10 : 15:06:12
can you give us some sample data and what you would like to return ?

It sounds like you might want a cross-tab, but I'm not sure ...

- Jeff
Go to Top of Page

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2003-12-10 : 15:06:57
This is my read:
select i.InviteID, i.InviteName, g1.Name AName, g1.GuestID AGuestID, g2.Name SName, g2.GuestID SGuestID 
from invite i
left join guests g1 on g1.inviteid = i.inviteid and g1.type = 'A'
left join guests g2 on g2.inviteid = i.inviteid and g2.type = 'S'
Go to Top of Page

interclubs
Yak Posting Veteran

63 Posts

Posted - 2003-12-10 : 15:13:23
Thanks guys, they all worked to some degree or another. Ehorn, that was exactly what I was looking for. Thanks!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-12-10 : 15:46:39
Anyone tell me what the HELL I'm doing wrong here....


USE Northwind
GO

CREATE TABLE xInvite (InviteId int, InvitationDate datetime)
CREATE TABLE xGuests (GuestId int, InviteId int, GuestName varchar(25), GuestType char(1))
GO

INSERT INTO xInvite (InviteId, InvitationDate)
SELECT 1, GetDate()

INSERT INTO xGuests (InviteId, GuestId, GuestName,GuestType)
SELECT 1,1,'Brett','S' UNION ALL
SELECT 1,1,'Brett','A' UNION ALL
SELECT 1,2,'Tara','S' UNION ALL
SELECT 1,3,'Jeff','S' UNION ALL
SELECT 1,4,'Bill','A' UNION ALL
SELECT 1,5,'Damain','A' UNION ALL
SELECT 1,6,'Rob','A' UNION ALL
SELECT 1,7,'Arnold','A' UNION ALL
SELECT 1,8,'Nigel','A'
GO

-- WRONG...I'm having a bad day
SELECT *
FROM xInvite i
LEFT JOIN xGuests g1
ON i.InviteID = g1.InviteID
LEFT JOIN xGuests g2
ON i.InviteID = g2.InviteID
WHERE g1.GuestType = 'A' AND g2.GuestType = 'S'


-- But huh...what am I doing wrong?
SELECT *
FROM xInvite i
LEFT JOIN xGuests g1
ON i.InviteID = g1.InviteID
AND g1.GuestType = 'A'
LEFT JOIN xGuests g2
ON i.InviteID = g2.InviteID
AND g2.GuestType = 'S'
GO

DROP TABLE xInvite
DROP TABLE xGuests
GO



Is it 5:00 yet?



Brett

8-)

EDIT: This is embarassing....

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-12-10 : 15:51:17
Shouldn't the left or right side of the resultant rows be null, except for my row?

I get three rows...I'm missing something...(pobably very fundamental)



Brett

8-)

EDIT:

These work fine


SELECT *
FROM xInvite i
LEFT JOIN xGuests g1
ON i.InviteID = g1.InviteID
AND g1.GuestType = 'A'

SELECT *
FROM xInvite i
LEFT JOIN xGuests g2
ON i.InviteID = g2.InviteID
AND g2.GuestType = 'S'
GO
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-12-10 : 15:55:09
Never mind...

WHAT A SCRUB

Just understand that if you can be on 2 lists for the same initation, you'll have this problem...



Brett

8-)
Go to Top of Page
   

- Advertisement -