SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 query help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

pnpsql
Posting Yak Master

India
246 Posts

Posted - 03/14/2013 :  02:16:29  Show Profile  Reply with Quote


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
Flowing Fount of Yak Knowledge

India
2223 Posts

Posted - 03/14/2013 :  02:21:29  Show Profile  Reply with Quote

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

India
246 Posts

Posted - 03/14/2013 :  02:29:50  Show Profile  Reply with Quote
any other clue

challenge everything
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2223 Posts

Posted - 03/14/2013 :  02:34:13  Show Profile  Reply with Quote
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

India
246 Posts

Posted - 03/14/2013 :  02:40:52  Show Profile  Reply with Quote
using 2008 but not want to use ties. please suggest

challenge everything
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2223 Posts

Posted - 03/14/2013 :  03:09:07  Show Profile  Reply with Quote
;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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000