Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Tsql to Select Rows Based on a Condition

Posted on 2013-11-08
8
389 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher 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
 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Pivot with row total 5 28
tools to scan a SQL server's problem 14 27
SQL Log size 3 18
RESTORE MASTER DATABASE -- NOW 2 20
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

837 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