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
 General SQL Server Forums
 New to SQL Server Programming
 SQL Join with alias

Author  Topic 

SQLInMyHead
Starting Member

4 Posts

Posted - 2011-01-20 : 11:01:55
Hi there...

Ive been trying to wrap my head around this all afternoon. Ive tried several different methods and dont seem to be getting any closer.

Essentially, I have one table 'stock' in which there are three columns which relate to the table 'Affiliates'.

The columns are 'Owner', 'Artist' and 'Buyer', all of which contain an Integer which relates to the ID column of the table 'Affiliates'

So,

----- Stock ------

--Artist ---- Owner ---- Buyer

--8-------------2-----------5
--3-------------1-----------6
--4-------------4-----------9
--7-------------5-----------3

------------------------------------------


----- Affiliates ------

ID ---- Affiliate_Name

1-------------John
2-------------Peter
3-------------Michael
4-------------Khaled
5-------------Sarah
6-------------Amy
7-------------Laura
8-------------Samantha
9-------------Aaron
10-------------Sharon


At the moment, I am only interested in 'Owner' and 'Artist' and I have got it pumping out the same Affiliate_Name for both of these fields, presumably because it is matching the same ID, and I think it was taking said ID from the owner field because it is called later in the SQL statement and the result I was given as 'Affiliate_Name' was definitely an Owner and not an artist.

This is the closest I have got!

I have tried without join statements, with join statements (various types of join statements), all with no success... I am either being a bit thick, or this is a bit complicated, i hope the latter!

If anyone knows how to help me, please do! Here is the current SQL statement...

SQL1 = "SELECT AffiliatesA.*, AffiliatesB.*, Stock.* FROM Affiliates AffiliatesA, Affiliates AffiliatesB, Stock INNER JOIN AffiliatesA ON Stock.Artist=AffiliatesA.ID INNER JOIN AffiliatesB ON Stock.Owner=AffiliatesB.ID WHERE Stock.VAT_Cat IS NULL ORDER BY Stock.Artist ASC"


I will sell my soul to the frst person that answers this...

Many thanks!!

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-01-20 : 11:10:46
Well i am unable to understand what kind of output do you need. Can you provide the details of the required output and i hope every integer in the stock table represents the ID of "Affiliates"


Go to Top of Page

SQLInMyHead
Starting Member

4 Posts

Posted - 2011-01-20 : 11:20:25
quote:
Originally posted by MIK_2008

Well i am unable to understand what kind of output do you need. Can you provide the details of the required output and i hope every integer in the stock table represents the ID of "Affiliates"






Hi There,

Sorry, probably not too clear, I am trying to get a recordset with all the records, and all the columns. The columns 'Owner' and 'Artist' which usually hold integers both refer to Affiliates.ID.

I am trying to get a recordset of all columns (the table 'Stock' has 15 columns all in all, not just the three mentioned originally) and all records with the columns Owner and Artist joined with Affiliate.Name via Affiliate ID, so when I write out the recordset I get 'John' instead of '1', Peter instead of '2' etc...

Hope this is clearer...

Thanks,

Jolly
Go to Top of Page

SQLInMyHead
Starting Member

4 Posts

Posted - 2011-01-20 : 11:22:35
In my original post, the line


The columns are 'Owner', 'Artist' and 'Buyer', all of which contain an Integer which relates to the ID column of the table 'Stock'

should read


The columns are 'Owner', 'Artist' and 'Buyer', all of which contain an Integer which relates to the ID column of the table 'Affiliates'

Have changed it now...


!!! Sorry for the confusion !!!
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-01-20 : 12:04:45
I think the DB structure is wrong ... any how you can use in-line queries to get what you want ... e.g.

Select
(select Affaliate_Name from Affiliates where ID=Stock.Artist)
,(select Affaliate_Name from Affiliates where ID=Stock.owner)
,(select Affaliate_Name from Affiliates where ID=Stock.buyer)
...
,(select Affaliate_Name from Affiliates where ID=Stock.LastColumn)
From Stock


Cheers!
MIK
Go to Top of Page

Skorch
Constraint Violating Yak Guru

300 Posts

Posted - 2011-01-20 : 12:25:05
Is this what you're looking for?

declare @stock table (Artist int, Owner int, Buyer int)

insert @Stock
select 8, 2, 5 union
select 2, 1, 6 union
select 4, 4, 9 union
select 7, 5, 3

declare @Affiliates table (ID int, Affiliate_name varchar(20))
insert @Affiliates
select 1, 'John' union
select 2, 'Peter' union
select 3, 'Michael' union
select 4, 'Khaled' union
select 5, 'Sarah' union
select 6, 'Amy' union
select 7, 'Laura' union
select 8, 'Samantha' union
select 9, 'Aaron' union
select 10, 'Sharon'

select z.Artist, a2.Affiliate_name, z.Buyer
from (
select a.Affiliate_name Artist, s.Owner, s.Buyer
from @Stock s
join @Affiliates a on s.Artist = a.ID) z
join @Affiliates a2 on z.Owner = a2.ID


Some days you're the dog, and some days you're the fire hydrant.
Go to Top of Page

SQLInMyHead
Starting Member

4 Posts

Posted - 2011-01-21 : 09:35:59
quote:
Originally posted by Skorch

Is this what you're looking for?

declare @stock table (Artist int, Owner int, Buyer int)

insert @Stock
select 8, 2, 5 union
select 2, 1, 6 union
select 4, 4, 9 union
select 7, 5, 3

declare @Affiliates table (ID int, Affiliate_name varchar(20))
insert @Affiliates
select 1, 'John' union
select 2, 'Peter' union
select 3, 'Michael' union
select 4, 'Khaled' union
select 5, 'Sarah' union
select 6, 'Amy' union
select 7, 'Laura' union
select 8, 'Samantha' union
select 9, 'Aaron' union
select 10, 'Sharon'

select z.Artist, a2.Affiliate_name, z.Buyer
from (
select a.Affiliate_name Artist, s.Owner, s.Buyer
from @Stock s
join @Affiliates a on s.Artist = a.ID) z
join @Affiliates a2 on z.Owner = a2.ID


Some days you're the dog, and some days you're the fire hydrant.




Well, thank you very much Skorch, you done good m'boy...

I ended up using;


SELECT AffTbl2.Affiliate_Name OwnerName, Z.*
FROM (
SELECT AffTbl.Affiliate_Name ArtistName, StockTbl.*
FROM Stock StockTbl
JOIN Affiliates AffTbl ON StockTbl.Artist=AffTbl.ID) Z
JOIN Affiliates AffTbl2 ON Z.Owner=AffTbl2.ID
WHERE Z.VAT_Cat IS NULL
ORDER BY Z.Artist ASC



Works perfectly...

One more question which I am not sure about, would it be better for me to use INNER JOIN as opposed to JOIN, and if so, why?

Many thanks everyone!

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-01-22 : 08:38:56
"would it be better for me to use INNER JOIN as opposed to JOIN, and if so, why?"
It is the same.

INNER JOIN is the default if the join type is not specified.



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -