Sum question with excluding condition

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CroCrew
    Recognized Expert Contributor
    • Jan 2008
    • 564

    Sum question with excluding condition

    Hello Everyone,

    First I would like to thank you to anyone that give an answer to my question.

    I have a table like this one below (KeyID is auto-increment)...
    Code:
    KeyID   UserID   Amount
    1        12        1.00
    2        09        1.00
    3        12        5.00
    4        12        6.00
    5        10        2.00
    I can't figure out the syntax to query the table to sum all the "Amount"s and exclude older records for like UserIDs.
    Code:
    KeyID   UserID   Amount
    1        12        1.00  <---exclude
    2        09        1.00
    3        12        5.00  <---exclude
    4        12        6.00
    5        10        2.00
    Here is what I have....

    SELECT SUM(Amount) as Total FROM Table

    Total = 15 (Should be 9)

    Thanks again.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Use an aggregate query to select the max KeyID grouping by UserID. Join that aggregate query back to the table on that max KeyID and UserID to get the latest record for each user.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32662

      #3
      CroCrew.
      Are you trying to get the sum of [Amount] for the latest records of all distinct users?

      Comment

      • relicx2004
        New Member
        • May 2014
        • 9

        #4
        Check both query below:

        Code:
        select sum(t1.Amount) as Amount
        from tblTest as t1
        where t1.KeyID = (select max(t2.KeyID)
                              from tblTest as t2
                              where t2.UserID = t1.UserID
                             );

        Code:
        ;with cte as (
            select KeyID, UserID, Amount,
                   row_number() over(partition by UserID order by KeyID desc) as RowNum
                from tblTest
        )
        select SUM(Amount) as Amount
            from cte
            where RowNum = 1

        Comment

        • Iweblessing
          New Member
          • Jul 2020
          • 1

          #5
          Thank you for your expantiation

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Something to consider with this solution is that the KeyId field is not infinite; that is at some point, if you ever remove records and the key wraps round you will get the situation where the order of the KeyId does not correspond to the chronological order that the records were inserted.

            OK this might be quite hypothetical but as software engineers it is our job consider all the problems that could arise with our solution and then decide if we need to mitigate against them. In this case possibly not for a simple exercise which will never have more than a few rows but a table that processes and then deletes millions of rows a day might have future trouble with this solution.

            Additionally the mitigation in this case is quite easy as all you need to do is add a datatime column to you table to record the entry time for the record and use that for your ordering.

            In general it is a bad idea to assume a pattern in a small test data set will (or won't) be present in a larger real world data set. Your only real guarantee with a key field is that it is unique (assuming the person creating the table did it properly).

            It comes down to 4 questions
            What is the potential issue?
            What will be the consequences if this issue arises?
            How often will or likely is it that this issue arises?
            How easy is it to mitigate against?

            Comment

            Working...