Solved

aggregate only get when >0

Posted on 2014-09-15
3
97 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
3 Comments
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 250 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 250 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 48

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

713 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