Solved

Tsql to Select Rows Based on a Condition

Posted on 2013-11-08
8
383 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 19

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL Backup 24 72
Auditing with Temporal Tables 4 19
SQL Split character from numbers 3 18
When to use an Aggregate Function. 18 38
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

747 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

7 Experts available now in Live!

Get 1:1 Help Now