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 |
|
mary H
Starting Member
32 Posts |
Posted - 2008-10-10 : 01:29:15
|
| I have 2 tables, horse and entry.Horse table looks like this:Horse_id-Name-Sire101-rotty-201102-rose-201201-stallion-401301-flower-401401-roxy-9999501-bell-9999502-sam-99999999-unknown-9999Entry table loooks like this:Event_id - Horse_id101-101101-102101-201101-301102-201103-201201-101301-301401-102I need to list the horse_id and name of those horses that were entered in the same event as their sire using a subquery.Any clues or suggestions? |
|
|
wormz666
Posting Yak Master
110 Posts |
Posted - 2008-10-10 : 01:45:55
|
| Select Horse_id,Name from Horse inner join Entry on Horse.Horse_id=Entry.Horse_id |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-10 : 01:56:27
|
| [code]SELECT h.*FROM Horse hINNER JOIN Event eON e.Horse_id=h.Horse_idINNER JOIN Event e1ON e1.Event_id=e.Event_idAND e1.Horse_id=h.Sire[/code] |
 |
|
|
mary H
Starting Member
32 Posts |
Posted - 2008-10-10 : 04:43:21
|
GREAT IT WORKED PERFECTLY!!THANKS |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-10 : 04:51:06
|
quote: Originally posted by mary H GREAT IT WORKED PERFECTLY!!THANKS
which suggestion? first or mine? |
 |
|
|
mary H
Starting Member
32 Posts |
Posted - 2008-10-10 : 05:20:41
|
quote: Originally posted by visakh16
quote: Originally posted by mary H GREAT IT WORKED PERFECTLY!!THANKS
which suggestion? first or mine?
The first suggestion only returned the horse_id and name of every horse in the entry table. Thanks for ur suggestion wormz666 Your suggestion returned those horses that entered in the same event as their sire! Thanks visakh16 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-10 : 05:21:36
|
| Ok..Cheers |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-10-10 : 06:21:44
|
In the future, PLEASE post proper sample data to make things easier for us to help you.Post sample code like this.DECLARE @Horses TABLE ( horseID INT, Name VARCHAR(10), sireID INT )INSERT @HorsesSELECT 101, 'Flash', 201 UNION ALLSELECT 102, 'star', 201 UNION ALLSELECT 201, 'boxer', 401 UNION ALLSELECT 301, 'daisy', 401 UNION ALLSELECT 401, 'snowyv', 9999 UNION ALLSELECT 501, 'bluebell', 9999 UNION ALLSELECT 502, 'sally', 9999 UNION ALLSELECT 9999, 'unknown', 9999DECLARE @Entries TABLE ( eventID INT, horseID INT )INSERT @EntriesSELECT 101, 101 UNION ALLSELECT 101, 102 UNION ALLSELECT 101, 201 UNION ALLSELECT 101, 301 UNION ALLSELECT 102, 201 UNION ALLSELECT 103, 201 UNION ALLSELECT 201, 101 UNION ALLSELECT 301, 301 UNION ALLSELECT 401, 102SELECT h.horseID, h.NameFROM @Horses AS hINNER JOIN @Entries AS e ON e.horseID IN (h.horseID, h.sireID)GROUP BY h.horseID, h.Name E 12°55'05.63"N 56°04'39.26" |
 |
|
|
mary H
Starting Member
32 Posts |
Posted - 2008-10-10 : 06:30:52
|
quote: Originally posted by Peso In the future, PLEASE post proper sample data to make things easier for us to help you.Post sample code like this.DECLARE @Horses TABLE ( horseID INT, Name VARCHAR(10), sireID INT )INSERT @HorsesSELECT 101, 'Flash', 201 UNION ALLSELECT 102, 'star', 201 UNION ALLSELECT 201, 'boxer', 401 UNION ALLSELECT 301, 'daisy', 401 UNION ALLSELECT 401, 'snowyv', 9999 UNION ALLSELECT 501, 'bluebell', 9999 UNION ALLSELECT 502, 'sally', 9999 UNION ALLSELECT 9999, 'unknown', 9999DECLARE @Entries TABLE ( eventID INT, horseID INT )INSERT @EntriesSELECT 101, 101 UNION ALLSELECT 101, 102 UNION ALLSELECT 101, 201 UNION ALLSELECT 101, 301 UNION ALLSELECT 102, 201 UNION ALLSELECT 103, 201 UNION ALLSELECT 201, 101 UNION ALLSELECT 301, 301 UNION ALLSELECT 401, 102SELECT h.horseID, h.NameFROM @Horses AS hINNER JOIN @Entries AS e ON e.horseID IN (h.horseID, h.sireID)GROUP BY h.horseID, h.Name E 12°55'05.63"N 56°04'39.26"
Thank you |
 |
|
|
|
|
|
|
|