Solved

Tsql to Select Rows Based on a Condition

Posted on 2013-11-08
8
387 Views
Last Modified: 2013-11-08
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,
0
Comment
Question by:Dovberman
8 Comments
 
LVL 20

Assisted Solution

by:Daniel Van Der Werken
Daniel Van Der Werken earned 100 total points
ID: 39633618
Something like this:

SELECT EX.SymbolID
FROM Exceptions EX
INNER JOIN StockHist SH
   ON SH.SymbolID = EX.SymbolID
GROUP BY EX.SymbolID
HAVING COUNT(EX.SymbolID) <= 5

Open in new window

0
 
LVL 34

Assisted Solution

by:Brian Crowe
Brian Crowe earned 100 total points
ID: 39633685
You can alter the procedure below to use a correlated subquery instead of a common table expression but I prefer the latter for readability.

WITH cteStockHist (SymbolID, [Count])
AS
(
   SELECT SymbolID, COUNT(1) AS [Count]
   FROM StockHist
   GROUP BY SymbolID
   HAVING COUNT(1) >= 5
)
SELECT E.*
FROM Exceptions AS E
INNER JOIN cteStockHist AS cte
   ON E.SymbolID = cte.SymbolID
0
 

Author Comment

by:Dovberman
ID: 39633732
I will try both and give you feedback.

Thanks,
0
 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 100 total points
ID: 39633738
SELECT e.*
FROM Exceptions e
INNER JOIN (
    SELECT SymbolID, COUNT(*) AS Symbol_Count
    FROM StockHist
    --WHERE ...
    GROUP BY SymbolID
    HAVING COUNT(*) <= 5
) AS S ON
    s.SymbolID = e.SymbolID
--WHERE ...
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 26

Assisted Solution

by:Zberteoc
Zberteoc earned 200 total points
ID: 39633829
The simplest query is:

SELECT 
	SymbolID 
FROM 
	Exceptions E
WHERE 
	(SELECT Count(*) FROM StockHist WHERE SymbolID=E.SymbolID)<=5

Open in new window

0
 

Author Comment

by:Dovberman
ID: 39633848
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
0
 
LVL 26

Accepted Solution

by:
Zberteoc earned 200 total points
ID: 39633893
Use this:

DELETE E FROM Exceptions E
WHERE 
	(SELECT Count(*) FROM StockHist WHERE SymbolID=E.SymbolID)<=5

Open in new window

0
 

Author Closing Comment

by:Dovberman
ID: 39633939
I wish to thank all of you.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now