Link to home
Start Free TrialLog in
Avatar of Dovberman
DovbermanFlag for United States of America

asked on

Tsql to Select Rows Based on a Condition

I have two tables.

I need to select rows in one table based on the number of times a common field occurs in the other table.

Table 1: Exceptions, Common field: SymbolID
Table 2: StockHist, Common field: SymbolID

SELECT SymbolID FROM Exceptions
WHERE Count(Exceptions.SymbolID) in StockHist is less than or equal to 5

Thanks,
SOLUTION
Avatar of Daniel Van Der Werken
Daniel Van Der Werken
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dovberman

ASKER

I will try both and give you feedback.

Thanks,
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
All three work.

Can the following be used to delete Exception table rows where the Exception.SymbolID occurs less than 5 times in the StockHist table? I do not want to delete StockHist rows.

DELETE FROM Exceptions EX
INNER JOIN StockHist SH
   ON SH.SymbolID = EX.SymbolID
GROUP BY EX.SymbolID
HAVING COUNT(EX.SymbolID) <= 5
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I wish to thank all of you.