简体   繁体   中英

SQL and Grouping

Lost in SQL on grouping. I have a table that looks like this:

start stop source
1     1    A
2     2    A
3     3    B
4     4    B
5     5    B

And I need to group it into something like this:

start stop source
1     2    A
3     5    B

The minimum number in start with the maximum number in stop for each source.

Thanks, Mike

You just need to add a MIN() and MAX() aggregate with a GROUP BY :

Select   Min(Start) As Start,
         Max(Stop)  As Stop,
         Source
From     YourTable
Group By Source

Try this:

select min(start),max(stop),source from test
group by source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM