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
 count rows with duplicate ids

Author  Topic 

girishhande
Starting Member

26 Posts

Posted - 2009-07-23 : 01:20:39
hi ihave table like below e.g.
id name
1 abc
1 xyz
1 pqr
2 tyy
2 yyy
3 bbb
3 iii
i want result from query like id number and row count with duplicate id
like
id rowcnt
1 3
2 2
3 2
plz give query for this?


gkh

Mangal Pardeshi
Posting Yak Master

110 Posts

Posted - 2009-07-23 : 01:24:39
Its simple case of GROUP BY and COUNT.
Try writting one GROUP BY query, if get stuck post back we will help you.

Mangal Pardeshi
http://mangalpardeshi.blogspot.com
Go to Top of Page

girishhande
Starting Member

26 Posts

Posted - 2009-07-23 : 01:26:20
hey i have tried but nt geting..plz give query

gkh
Go to Top of Page

Mangal Pardeshi
Posting Yak Master

110 Posts

Posted - 2009-07-23 : 01:26:59
post the query you tried.

Mangal Pardeshi
http://mangalpardeshi.blogspot.com
Go to Top of Page

girishhande
Starting Member

26 Posts

Posted - 2009-07-23 : 01:30:44
count * from _table
goupby id

gkh
Go to Top of Page

Mangal Pardeshi
Posting Yak Master

110 Posts

Posted - 2009-07-23 : 01:32:34
SELECT id, COUNT(*)
FROM tablename
GROUP BY id

Mangal Pardeshi
http://mangalpardeshi.blogspot.com
Go to Top of Page

Mangal Pardeshi
Posting Yak Master

110 Posts

Posted - 2009-07-23 : 01:33:21
help yourself - http://technet.microsoft.com/en-us/library/ms175997.aspx
http://technet.microsoft.com/en-us/library/ms177673.aspx

Mangal Pardeshi
http://mangalpardeshi.blogspot.com
Go to Top of Page

girishhande
Starting Member

26 Posts

Posted - 2009-07-23 : 01:35:31
thnks

gkh
Go to Top of Page

ashishashish
Constraint Violating Yak Guru

408 Posts

Posted - 2009-07-23 : 02:49:11
select id,count(*) from urtable group by id


if u want only which have records or count greater then 1 then

select id,count(*) from urtable group by id having count(*) >1

i think like this..

iF theRe iS a wAy iN tHen theRe iS a wAy oUt..
Go to Top of Page
   

- Advertisement -