Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2003
    Posts
    5

    How to retrieve the max sequence number

    Hi,
    I have the following values in my table

    agnum sitenum month year sequence values
    100 010 05 2003 1 50
    100 020 05 2003 1 50
    100 030 05 2003 1 50
    100 040 05 2003 1 50
    100 050 05 2003 1 50
    100 010 05 2003 2 60
    100 020 05 2003 2 40
    100 030 05 2003 2 40

    The out put that i would like is :
    agnum month year sum(values)
    100 05 2003 240 ( 60+40+40+50+50)


    ie: I want the total of all the values for the sites which have the max( sequence) eg: site 010,020,030 have sequence 2 . So i want the total to include only the values of 010,020,030 with sequence 2 and it should include the values of site 040,050 with sequence 1 since site 040,050 donot have a resubmission for the month 05/2003

    Any help is much appreciated

    Jayanthi

  2. #2
    Join Date
    Jul 2003
    Location
    US
    Posts
    314

    Required Query

    Jayanti
    Kindly use the following query to get the answers.

    select a.agnum, sum(a.values1) from table1 a, (select max(sequnce) maxseq, sitenum from table1 group by sitenum) tmptable where a.sequnce=tmptable.maxseq and a.sitenum=tmptable.sitenum group by a.agnum;

    In order to get the max sequence number for all the sitenum, you would use the following query
    select max(sequnce) maxseq, sitenum from table1 group by sitenum;

    Thanx and Regards
    Aruneesh

  3. #3
    Join Date
    Jul 2003
    Posts
    5

    Re: Required Query

    Thank you arun,
    That worked for me

    Jayanthi


    Originally posted by aruneeshsalhotr
    Jayanti
    Kindly use the following query to get the answers.

    select a.agnum, sum(a.values1) from table1 a, (select max(sequnce) maxseq, sitenum from table1 group by sitenum) tmptable where a.sequnce=tmptable.maxseq and a.sitenum=tmptable.sitenum group by a.agnum;

    In order to get the max sequence number for all the sitenum, you would use the following query
    select max(sequnce) maxseq, sitenum from table1 group by sitenum;

    Thanx and Regards
    Aruneesh

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •