Solved

aggregate only get when >0

Posted on 2014-09-15
3
98 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 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Backup & Restore 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.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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