Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.databases.ms-sqlserver > #1397

Re: How do I write this sql statement

From tony@mountifield.org (Tony Mountifield)
Newsgroups comp.databases.ms-sqlserver
Subject Re: How do I write this sql statement
Date 2013-02-20 14:42 +0000
Organization Software Insight Ltd., Winchester, UK
Message-ID <kg2nbv$tph$1@softins.clara.co.uk> (permalink)
References <kg2mau$13a$1@dont-email.me>

Show all headers | View raw


In article <kg2mau$13a$1@dont-email.me>,
Tony <tony.johansson@inport.com> wrote:
> In the database we have a column called grefnr and loopnr.
> Each record could have something like the following se below. I want to
> select the record with the highest loopnr number for every grefnr.
> In this example I would get the following three record  1 78 and 2  95 and 3
> 99
> How can I write this sql
> grefnr    loopnr
> 1               34
> 1                12
> 1                78
> 1                35
> 2               4
> 2                72
> 2                70
> 2                95
> 3               11
> 3                16
> 3                99
> 3                7

Try this:

SELECT grefnr,MAX(loopnr) FROM yourtable GROUP BY grefnr

Cheers
Tony
-- 
Tony Mountifield
Work: tony@softins.co.uk - http://www.softins.co.uk
Play: tony@mountifield.org - http://tony.mountifield.org

Back to comp.databases.ms-sqlserver | Previous | NextPrevious in thread | Find similar


Thread

How do I write this sql statement "Tony" <tony.johansson@inport.com> - 2013-02-20 15:25 +0100
  Re: How do I write this sql statement tony@mountifield.org (Tony Mountifield) - 2013-02-20 14:42 +0000

csiph-web