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
 query help

Author  Topic 

pnpsql
Posting Yak Master

246 Posts

Posted - 2013-03-14 : 02:16:29


i have a table
t_chrg
custid chargid
1 10
1 11
1 12
2 11
2 11
2 15
3 11



i want to get custid that have maximum count of charges are applied. in this case this is 1 and 2









challenge everything

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-03-14 : 02:21:29

DECLARE @t_chrg TABLE(custid INT, chargid INT)
INSERT INTO @t_chrg VALUES(1, 10), (1, 11 ), (1, 12 ), (2, 11), (2, 11), (2, 15), (3, 11)
SELECT TOP 1 WITH TIES custid, COUNT(*) cnt
FROM @t_chrg
GROUP BY custid
ORDER BY cnt DESC

--
Chandu
Go to Top of Page

pnpsql
Posting Yak Master

246 Posts

Posted - 2013-03-14 : 02:29:50
any other clue

challenge everything
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-03-14 : 02:34:13
quote:
Originally posted by pnpsql

any other clue
challenge everything

whats the problem with above query....?

On which version you are working?

--
Chandu
Go to Top of Page

pnpsql
Posting Yak Master

246 Posts

Posted - 2013-03-14 : 02:40:52
using 2008 but not want to use ties. please suggest

challenge everything
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-03-14 : 03:09:07
;with CTE AS (SELECT custid, COUNT(*) cnt FROM @t_chrg GROUP BY custid)
SELECT custid
FROM CTE
WHERE cnt = (SELECT MAX(cnt) FROM CTE)


--
Chandu
Go to Top of Page
   

- Advertisement -