Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Question regarding where statement in count fn?

Author  Topic 

larryg003
Starting Member

18 Posts

Posted - 2010-06-15 : 18:41:21
Suppose I have a function with two columns: A and B.
A contains tags (some of which repeat) and B contains
topics like "ABC", "KDH", "FSK" (some of which repeat)

What is the difference between the query:
select
count(COLUMN_A)
where [COLUMN_B] like "ABC"

and

select
count(COLUMN_B)
where(COLUMN_B) like "ABC"?

if neither column is ever empty, than the two values will always be the same, right?

Lastly, I wanted to run a query to count the data when the combination of topics and tags is distinct? Because both are repetitive, i get cases when the same tag and topic can be counted twice. How do I separate this?

Thanks a lot!!!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-15 : 18:44:48
If you want to distinct then you need to add GROUP BY:

select count(*)
from yourtable
group by column_a
where [COLUMN_B] like "ABC"


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

naveengopinathasari
Yak Posting Veteran

60 Posts

Posted - 2010-06-16 : 02:05:41
Use Sub Query to get the result for your requirement.

Select Count(1) From
(Select COLUMN_A , COLUMN_B FROM TableName
GROUP BY COLUMN_A , COLUMN_B ) AS DD
This will return only the count which are not duplicate

Lets unLearn
Go to Top of Page
   

- Advertisement -