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.
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(*) cntFROM @t_chrgGROUP BY custidORDER BY cnt DESC--Chandu |
|
|
pnpsql
Posting Yak Master
246 Posts |
Posted - 2013-03-14 : 02:29:50
|
any other cluechallenge everything |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-03-14 : 02:34:13
|
quote: Originally posted by pnpsql any other cluechallenge everything
whats the problem with above query....?On which version you are working?--Chandu |
|
|
pnpsql
Posting Yak Master
246 Posts |
Posted - 2013-03-14 : 02:40:52
|
using 2008 but not want to use ties. please suggestchallenge everything |
|
|
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 custidFROM CTEWHERE cnt = (SELECT MAX(cnt) FROM CTE)--Chandu |
|
|
|
|
|
|
|