Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

aggregate only get when >0

Posted on 2014-09-15
3
Medium Priority
?
100 Views
Last Modified: 2014-09-15
Hello,
How do I only to get records that have Cnt>0 in

SELECT MyTableId, Cnt=(SELECT COUNT(*) FROM OtherTable o where o.MyTableId=m.MyTableId)
 FROM MyTable m
0
Comment
Question by:johnson1
[X]
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
3 Comments
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 1000 total points
ID: 40323083
SELECT m.*
FROM MyTable m
    JOIN (SELECT MyTableID, count(some_column) as the_count FROM OtherTable GROUP BY MyTableID) o ON m.MyTableId = o.MyTableId
GROUP BY MyTableId
ORDER BY MyTableId
HAVING o.the_count > 0    -- <--   Looky here

Open in new window

I have an article called SQL Server GROUP BY Solutions that talks a lot about the HAVING clause.  If you like what you see, please click the 'Yes' button next to 'Was this article helpful?', and provide some feedback.

Thanks in advance.
Jim
0
 
LVL 15

Accepted Solution

by:
Vikas Garg earned 1000 total points
ID: 40323097
Hi,
You can achieve your result this way
;With CTE as
(
SELECT MyTableId, Cnt=(SELECT COUNT(*) FROM OtherTable o where o.MyTableId=m.MyTableId)
 FROM MyTable
)

SELECT * FROM CTE WHERE CNT > 0

Open in new window

0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40323144
I'm not sure if it is deliberate or not but you have omitted GROUP BY in your question; but you refer to getting recordS (plural) so I assume a group by being used.

SELECT o.MyTableId, COUNT(*)  as cnt
FROM OtherTable o
GROUP BY o.MyTableId
HAVING COUNT(*)  > 0

Use a HAVING clause which allows you to filter the aggregated values, here it will remove all counts of zero from the result.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 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