473,385 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Sum question with excluding condition

CroCrew
564 Expert 512MB
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)...
Expand|Select|Wrap|Line Numbers
  1. KeyID   UserID   Amount
  2. 1        12        1.00
  3. 2        09        1.00
  4. 3        12        5.00
  5. 4        12        6.00
  6. 5        10        2.00
  7.  
I can't figure out the syntax to query the table to sum all the "Amount"s and exclude older records for like UserIDs.
Expand|Select|Wrap|Line Numbers
  1. KeyID   UserID   Amount
  2. 1        12        1.00  <---exclude
  3. 2        09        1.00
  4. 3        12        5.00  <---exclude
  5. 4        12        6.00
  6. 5        10        2.00
  7.  
Here is what I have....

SELECT SUM(Amount) as Total FROM Table

Total = 15 (Should be 9)

Thanks again.
Mar 17 '20 #1
5 5388
Rabbit
12,516 Expert Mod 8TB
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.
Mar 17 '20 #2
NeoPa
32,556 Expert Mod 16PB
CroCrew.
Are you trying to get the sum of [Amount] for the latest records of all distinct users?
Apr 25 '20 #3
Check both query below:

Expand|Select|Wrap|Line Numbers
  1. select sum(t1.Amount) as Amount
  2. from tblTest as t1
  3. where t1.KeyID = (select max(t2.KeyID)
  4.                       from tblTest as t2
  5.                       where t2.UserID = t1.UserID
  6.                      );

Expand|Select|Wrap|Line Numbers
  1. ;with cte as (
  2.     select KeyID, UserID, Amount,
  3.            row_number() over(partition by UserID order by KeyID desc) as RowNum
  4.         from tblTest
  5. )
  6. select SUM(Amount) as Amount
  7.     from cte
  8.     where RowNum = 1
Jul 1 '20 #4
Thank you for your expantiation
Jul 1 '20 #5
Banfa
9,065 Expert Mod 8TB
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?
Jul 2 '20 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: J.P | last post by:
Hi! Anyone knows if it's possible to do an update if, and only if my condition is TRUE. Example: In MYTABLE I have three columns, like this: ID(INT), PUBLISH(ENUM(Y,N)),...
8
by: xixi | last post by:
hi, we are using db2 udb v8.1 on windows, i have a table contains over one million records, it has seperate own tablespace than others, with bufferpool size 250, i have created multiple views on...
9
by: Alpha | last post by:
C# doesn't allow multiple condition in a single if ? Or did I make a mistake with my syntax? It keep giving me syntax error when I put "and" or "or" in the if statemnt for multiple condition...
4
by: RA | last post by:
Is it possible to add an attribute for onClick event with an if condition. For example. If button is enabled then popup a message "Button Enabled" and if it is disabled popup message "Button...
1
by: Michael R | last post by:
Hello all. I'm currently writting a query that would be able to filter a table by a form's control value. If the value is null, the query should select all the rows of the table. The condition...
1
by: damezumari | last post by:
How do I get the total without a condition and the total with the condition at the same time? This is my mysql table: id datetimeconfirmed customerid amount 1 ...
6
by: fran7 | last post by:
Hi, Would anyone know how to populate an asp dropdown with a dependant condition. I need this dropdown to select only artists, ie from the authortwo field where in another field, ie extrathree, ...
1
by: sudeep kumar | last post by:
i want to validate many textboxes with same condition....Do i need to write code for each text boxes...plzz help
0
by: yuentong | last post by:
hi guy, My question is to create a poll system for basically 2 condition which is single vote and mulitiple vote per poll. For the single vote, it can be single vote per day or per duration. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.