Welcome to SPN

Register and Join the most happening forum of Sikh community & intellectuals from around the world.

Sign Up Now!
  1.   Become a Supporter    ::   Make a Contribution   
    Target (Recurring Monthly): $200 :: Achieved: $98

Need help on query - average of most recent 25 results

Discussion in 'Information Technology' started by Philip Lo, Oct 27, 2005.

  1. Philip Lo

    Philip Lo
    Expand Collapse
    Guest

    Hi,

    I have a table with 3 columns, col 1 is date time, col 2 is text, and col 3
    is number. Is it possible to have one single query that calculates the
    average of 3 for col 3 (selection of 3 rows is based on those with most
    recent time stamp in col 1) group by col 2 ?

    Example:
    ------------------
    col 1, col 2, col 3
    ------------------
    1 Oct 2005, A01, 20;
    2 Oct 2005, A01, 25;
    3 Oct 2005, A01, 30;
    4 Oct 2005, A01, 35;
    5 Oct 2005, A02, 40;
    6 Oct 2005, A02, 45;
    7 Oct 2005, A02, 50;
    8 Oct 2005, A02, 55;

    After query:
    -------------------------------------------
    Average of most recent 3 result, col 2
    -------------------------------------------
    (25+30+35)/3=30, A01;
    (45+50+55)/3=50, A02;

    Thanks....

    regards,
    Philip
     
  2. Loading...

    Similar Threads Forum Date
    Helping Education To The Needy Through Daswandh Hard Talk Apr 14, 2017
    Interracial And Interfaith Marriage, Help Needed To Confront/convince My Parents Love & Marriage Aug 16, 2016
    17 yr old Baljit needs your help Get Involved Aug 19, 2015
    Learn Punjabi Need help Language, Arts & Culture May 20, 2014
    United Sikhs From United Sikhs. Phillippines Needs Your Help Sikh Organisations Nov 22, 2013

  3. Jeff Boyce

    Jeff Boyce
    Expand Collapse
    Guest

    Philip

    Define "most recent". Based on the data example you posted, Access will
    have no way to know which are the 3 most recent.

    Regards

    Jeff Boyce
    <Office/Access MVP>

    "Philip Lo" <no.mail@mail.com> wrote in message
    news:OlRsTRm2FHA.1140@tk2msftngp13.phx.gbl...
    > Hi,
    >
    > I have a table with 3 columns, col 1 is date time, col 2 is text, and col
    > 3 is number. Is it possible to have one single query that calculates the
    > average of 3 for col 3 (selection of 3 rows is based on those with most
    > recent time stamp in col 1) group by col 2 ?
    >
    > Example:
    > ------------------
    > col 1, col 2, col 3
    > ------------------
    > 1 Oct 2005, A01, 20;
    > 2 Oct 2005, A01, 25;
    > 3 Oct 2005, A01, 30;
    > 4 Oct 2005, A01, 35;
    > 5 Oct 2005, A02, 40;
    > 6 Oct 2005, A02, 45;
    > 7 Oct 2005, A02, 50;
    > 8 Oct 2005, A02, 55;
    >
    > After query:
    > -------------------------------------------
    > Average of most recent 3 result, col 2
    > -------------------------------------------
    > (25+30+35)/3=30, A01;
    > (45+50+55)/3=50, A02;
    >
    > Thanks....
    >
    > regards,
    > Philip
    >
     
  4. Michael Evanchik

    Michael Evanchik
    Expand Collapse
    Guest

    man, i should charge you for making me think

    =P

    select (sum(col3)-max(col3))/(count(*)-1) from table1 group by col2

    your date column might have to be indexed asc not sure didnt test

    Michael Evanchik
    www.MichaelEvanchik.com

    "Philip Lo" wrote:

    > Hi,
    >
    > I have a table with 3 columns, col 1 is date time, col 2 is text, and col 3
    > is number. Is it possible to have one single query that calculates the
    > average of 3 for col 3 (selection of 3 rows is based on those with most
    > recent time stamp in col 1) group by col 2 ?
    >
    > Example:
    > ------------------
    > col 1, col 2, col 3
    > ------------------
    > 1 Oct 2005, A01, 20;
    > 2 Oct 2005, A01, 25;
    > 3 Oct 2005, A01, 30;
    > 4 Oct 2005, A01, 35;
    > 5 Oct 2005, A02, 40;
    > 6 Oct 2005, A02, 45;
    > 7 Oct 2005, A02, 50;
    > 8 Oct 2005, A02, 55;
    >
    > After query:
    > -------------------------------------------
    > Average of most recent 3 result, col 2
    > -------------------------------------------
    > (25+30+35)/3=30, A01;
    > (45+50+55)/3=50, A02;
    >
    > Thanks....
    >
    > regards,
    > Philip
    >
    >
    >
     
  5. Michael Evanchik

    Michael Evanchik
    Expand Collapse
    Guest

    MVP im ashamed :|

    "Philip Lo" wrote:

    > Hi,
    >
    > I have a table with 3 columns, col 1 is date time, col 2 is text, and col 3
    > is number. Is it possible to have one single query that calculates the
    > average of 3 for col 3 (selection of 3 rows is based on those with most
    > recent time stamp in col 1) group by col 2 ?
    >
    > Example:
    > ------------------
    > col 1, col 2, col 3
    > ------------------
    > 1 Oct 2005, A01, 20;
    > 2 Oct 2005, A01, 25;
    > 3 Oct 2005, A01, 30;
    > 4 Oct 2005, A01, 35;
    > 5 Oct 2005, A02, 40;
    > 6 Oct 2005, A02, 45;
    > 7 Oct 2005, A02, 50;
    > 8 Oct 2005, A02, 55;
    >
    > After query:
    > -------------------------------------------
    > Average of most recent 3 result, col 2
    > -------------------------------------------
    > (25+30+35)/3=30, A01;
    > (45+50+55)/3=50, A02;
    >
    > Thanks....
    >
    > regards,
    > Philip
    >
    >
    >
     
Since you're here... we have a small favor to ask...

More people are visiting & reading SPN than ever but far fewer are paying to sustain it. Advertising revenues across the online media have fallen fast. So you can see why we need to ask for your help. Donating to SPN's is vote for free speech, for diversity of opinions, for the right of the people to stand up to religious bigotry. Without any affiliation to any organization, this constant struggle takes a lot of hard work to sustain as we entirely depend on the contributions of our esteemed writers/readers. We do it because we believe our perspective matters – because it might well be your perspective, too... Fund our efforts and together we can keep the world informed about the real Sikh Sikhi Sikhism. If everyone who writes or reads our content, who likes it, helps us to pay for it, our future would be much more secure. Every Contribution Matters, Contribute Generously!

    Become a Supporter      ::     Make a Contribution     



Share This Page