Monday, March 12, 2012

find out if an ID is tied to multiple ids in another table

Table 1

stock

stock_id

cat_id

stock_name

is_fund

Table 2

Fund_contents

fund_id

stock_id

I need to find out all the stock_id's from the stock table that are associated with 2 different fund_id's in the second table and the stock_id's in the first table have to have is_fund = 0 from the stock table

any ideas?

How about,

SELECT S.Stock_Id
FROM StockAs S
JOIN Fund_Contents F
ON S.Stock_Id = F.Stock_Id
WHERE S.IS_Fund = 0
GROUP BY S.Stock_Id
HAVINGCOUNT(F.Fund_Id) >= 2

Hope this helps,

|||

SELECT S.Stock_Id
FROM StockAs S
JOIN Fund_Contents F
ON S.Stock_Id = F.Stock_Id

WHERE S.IS_Fund = 0
GROUP BY S.Stock_Id
HAVINGCOUNT(F.Fund_Id) >= 2

|||

perfect thanks both of you

No comments:

Post a Comment