X-Received: by 10.224.178.205 with SMTP id bn13mr22506247qab.3.1366719555661; Tue, 23 Apr 2013 05:19:15 -0700 (PDT) X-Received: by 10.49.2.170 with SMTP id 10mr2691760qev.40.1366719555637; Tue, 23 Apr 2013 05:19:15 -0700 (PDT) Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!gp5no1824360qab.0!news-out.google.com!ef9ni833qab.0!nntp.google.com!gp5no1824352qab.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.databases.ms-sqlserver Date: Tue, 23 Apr 2013 05:19:15 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=94.101.225.18; posting-account=yUnXAgoAAAAtYWXOhuy56byPUjMGOID6 NNTP-Posting-Host: 94.101.225.18 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <6957ef41-d7dc-492d-9ad2-e5a21b963965@googlegroups.com> Subject: Re: need help with ordering groups of records From: Gints Plivna Injection-Date: Tue, 23 Apr 2013 12:19:15 +0000 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Xref: csiph.com comp.databases.ms-sqlserver:1452 ceturtdiena, 2013. gada 18. apr=C4=ABlis 04:21:02 UTC+3, migurus rakst=C4= =ABja: > I have a list of customers and addresses they have visited, each record h= as a count of visits. I need to re-sort the list in such a way that custome= rs with higher counts will be higher on the list, but all his/her records s= hould stay together >=20 [skipped] >=20 > As you see, Juan Silva has 225 visits, which is highest of them all, so a= ll his records are sorted before everybody else. The order of records withi= n group belonging to the same customer is not important. >=20 >=20 >=20 > Any hint or idea would be appreciated. select * from @t t order by MAX(cnt) OVER (PARTITION BY NAME) desc, cnt desc Gints Plivna http://www.gplivna.eu