Selecting first and last rows of a specific grouped value

Answered Selecting first and last rows of a specific grouped value

  • 2011년 12월 13일 화요일 오후 12:07
     
     

    Hi

    I have data like this in my table

    A     2007-01-11
    A     2007-02-11
    A     2007-11-12
    A     2007-11-13
    A     2007-11-14
    B     2007-11-15
    B     2007-11-16
    B     2007-11-17
    B     2007-11-18
    C     2007-11-19 
    C     2007-12-11 
    C     2007-12-13 
    C     2007-12-14 
    C     2007-12-16 
    C     2007-12-17 
    A     2007-12-18 
    A     2007-12-19 
    A     2007-12-20  
    A     2007-12-21

     

    Now i would like to get the first row and the last row in each (A,B,C) interval, is this possible with sql?

     

모든 응답

  • 2011년 12월 14일 수요일 오후 1:32
     
     답변됨

    Try the below query I think this will fulfill your requirement

    colm1 --> A,B,C

    colm2 --> date

     

    select colm1, min(colm2) min_date, max(colm2) from table

    group by colm1