I have three tables:
TICKERS
id
ticker (varchar 100)
COEF
id
id_ticker
id_spre
coef (varchar 100)
SPRE
id
id_check
name (varchar 100)
I need to retrive all the SPRE’s records that have id_check = 1 AND then for this result I have to get all the records that have SPRE.id = COEF.id_spre (ONE time) and then all the TICKERS related to the COEF (tickers.id = COEF.id_ticker).
The result shoud be:
SPRE.name
COEF.coef
TICKERS.ticker
TICKERS.ticker
TICKERS.ticker
....
....
SPRE.name
COEF.coef
TICKERS.ticker
TICKERS.ticker
TICKERS.ticker
(I have repeated tickers because many tickers should have the same COEF)
I thougth something like:
A query that retrieve all the SPRE with id_check = 1 and then a SUBQUERY that UNION two selects, the FIRST to get the COEF.id_spre = SPRE.id and the second to get all the tickers with TICKERS.id = COEF.id_ticker
…. but how to write it?
wait.. are you wanting to pivot out the results? as in do you want all the tickers related to one spre returned in the same row?
anyway as it stands it looks like something like the following may be of use:
http://sqlfiddle.com/#!3/c984a/15
if you need to limit it for SPREs then would the following work:
using MSSQL syntax: http://sqlfiddle.com/#!3/c984a/25