Solved

aggregate only get when >0

Posted on 2014-09-15
3
89 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
Comment Utility
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 14

Accepted Solution

by:
Vikas Garg earned 250 total points
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

10 Experts available now in Live!

Get 1:1 Help Now