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 |
legacyvbc
Starting Member
37 Posts |
Posted - 2007-09-20 : 11:51:35
|
I have a table of stock tickers similar to the following:TickerId Ticker SectorId HedgeId1 MSFT 1 12 INTC 1 13 GE 2 14 T 3 15 S5TECH 1 26 S5COND 2 27 S5TELE 3 28 XLK 1 39 XLD 2 310 TTH 3 3I'm trying to create a query that will return all tickerId's that have a hedgeid of 1 joined with all tickers that have a hedgeid 3 and have the same industry id. So in the above data the query would return:TickerId Ticker SectorId HedgeId TickerId2 Ticker1 MSFT 1 1 8 XLK2 INTC 1 1 8 XLK3 GE 2 1 9 XLD4 T 3 1 10 TTHI can't seem to figure out how to do this correctly but I can't imagine it is difficult. Any help is greatly appreciated. |
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2007-09-20 : 12:23:36
|
Here ya go. This is often called a self-join.SELECT a.tickerID,a.Ticker,a.sectorId,a.hedgeid,b.TickerID,b.TickerFROM tbl ainner join tbl bon a.sectorid = b.sectoridwhere a.hedgeid = 1and b.hedgeid = 3Jim |
 |
|
legacyvbc
Starting Member
37 Posts |
Posted - 2007-09-20 : 15:05:16
|
awesome! thank you so much! |
 |
|
|
|
|