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 |
|
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_Name1-------------John2-------------Peter3-------------Michael4-------------Khaled5-------------Sarah6-------------Amy7-------------Laura8-------------Samantha9-------------Aaron10-------------SharonAt 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" |
 |
|
|
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 |
 |
|
|
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 readThe 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 !!! |
 |
|
|
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 StockCheers!MIK |
 |
|
|
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 @Stockselect 8, 2, 5 unionselect 2, 1, 6 unionselect 4, 4, 9 unionselect 7, 5, 3declare @Affiliates table (ID int, Affiliate_name varchar(20))insert @Affiliatesselect 1, 'John' unionselect 2, 'Peter' unionselect 3, 'Michael' unionselect 4, 'Khaled' unionselect 5, 'Sarah' unionselect 6, 'Amy' unionselect 7, 'Laura' unionselect 8, 'Samantha' unionselect 9, 'Aaron' unionselect 10, 'Sharon'select z.Artist, a2.Affiliate_name, z.Buyerfrom ( select a.Affiliate_name Artist, s.Owner, s.Buyer from @Stock s join @Affiliates a on s.Artist = a.ID) zjoin @Affiliates a2 on z.Owner = a2.ID Some days you're the dog, and some days you're the fire hydrant. |
 |
|
|
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 @Stockselect 8, 2, 5 unionselect 2, 1, 6 unionselect 4, 4, 9 unionselect 7, 5, 3declare @Affiliates table (ID int, Affiliate_name varchar(20))insert @Affiliatesselect 1, 'John' unionselect 2, 'Peter' unionselect 3, 'Michael' unionselect 4, 'Khaled' unionselect 5, 'Sarah' unionselect 6, 'Amy' unionselect 7, 'Laura' unionselect 8, 'Samantha' unionselect 9, 'Aaron' unionselect 10, 'Sharon'select z.Artist, a2.Affiliate_name, z.Buyerfrom ( select a.Affiliate_name Artist, s.Owner, s.Buyer from @Stock s join @Affiliates a on s.Artist = a.ID) zjoin @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! |
 |
|
|
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] |
 |
|
|
|
|
|
|
|