Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unique and Duplicate Records Count 3

Status
Not open for further replies.

emasik

Programmer
Mar 2, 2007
3
US
I need to return to values (@unique and @dupes).

I start with:

declare @unique int,
@dupes int

Select name, count (1) from tblTest
Group by name
Having count (1) > 1

However, I need total unique and total dupes from field 'name'.

Any advice is welcome.
 
Denis, that gives you one row per name.

Code:
select
   uniquecount = sum(case when cnt = 1 then 1 else 0 end),
   nonuniquecount = sum(case when cnt > 1 then 1 else 0 end)
from (
   select name, cnt = count(*) from tblTest group by name
) x

[COLOR=black #e0e0e0]For SQL and technical ideas, visit my blog, Squared Thoughts.

[sub]The best part about anything that has cheese is the cheese.[/sub][/color]
 
Question for ESquared:

What x is for by the end of yur statment?


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top