sum

SQL SUM and COUNT returning wrong values

一笑奈何 提交于 2021-02-08 11:47:19
问题 I found a bunch of similar questions but nothing worked for me, or I am too stupid to get how to do it right. The visit count works fine if I use COUNT(DISTINCT visits.id) but then the vote count goes totally wrong - it displays a value 3 to 4 times larger than it should be. So this is the query SELECT SUM(votes.rating), COUNT(visits.id) FROM topics LEFT JOIN visits ON ( visits.content_id = topics.id ) LEFT JOIN votes ON ( votes.content_id = topics.id ) WHERE topics.id='1' GROUP BY topics.id

pandas add new row based on sum/difference of other rows

我与影子孤独终老i 提交于 2021-02-08 09:24:45
问题 df have id measure t1 t2 t3 1 savings 1 2 5 1 income 10 15 14 1 misc 5 5 5 2 savings 3 6 12 2 income 4 20 80 2 misc 1 1 1 df want- add a new row to the measure for each id, called spend, calculated by subtracting measure=income - measure=savings, for each of the periods t1,t2,t3, for each id id measure t1 t2 t3 1 savings 1 2 5 1 income 10 15 14 1 misc 5 5 5 1 spend 9 13 9 2 savings 3 6 12 2 income 4 20 80 2 misc 1 1 1 2 spend 1 14 68 Trying: df.loc[df['Measure'] == 'spend'] = df.loc[df[

pandas add new row based on sum/difference of other rows

泄露秘密 提交于 2021-02-08 09:24:07
问题 df have id measure t1 t2 t3 1 savings 1 2 5 1 income 10 15 14 1 misc 5 5 5 2 savings 3 6 12 2 income 4 20 80 2 misc 1 1 1 df want- add a new row to the measure for each id, called spend, calculated by subtracting measure=income - measure=savings, for each of the periods t1,t2,t3, for each id id measure t1 t2 t3 1 savings 1 2 5 1 income 10 15 14 1 misc 5 5 5 1 spend 9 13 9 2 savings 3 6 12 2 income 4 20 80 2 misc 1 1 1 2 spend 1 14 68 Trying: df.loc[df['Measure'] == 'spend'] = df.loc[df[

Sum a range of cells in a single column in pandas dataframe

廉价感情. 提交于 2021-02-08 08:24:46
问题 I have three columns in a DataFrame. I want to take the number in the Streak_Count column and sum up that number of cells from the returns in the MON TOTAL. The result is displayed in the WANTED RESULT as shown below. The issue I cant figure out is summing the number of cells which can be any number>> in this example between 1 and 4. MON TOTAL STREAK_COUNT WANTED RESULT 1/2/1992 1.123077 1 1.123077 (only 1 so 1.12) 2/3/1992 -1.296718 0 3/2/1992 -6.355612 2 -7.65233 (sum of -1.29 and -6.35) 4

Modulus Function to Avoid Integer Overflow in C++

…衆ロ難τιáo~ 提交于 2021-02-08 07:29:55
问题 If I have 2 int or long long variables, call them a and b , and I want to compute the sum (a + b) mod p , where p is a large prime integer, how can I utilize the modulo operator in C++ to achieve the desired result? I have tried (a + b) % p , but this gives overflow sometimes, since a + b will overflow before the mod is applied. Other similar approaches I have tried seem to avoid overflow, but give an incorrect result. How can I use the modulo operator in this case to correctly compute the

Modulus Function to Avoid Integer Overflow in C++

旧时模样 提交于 2021-02-08 07:29:40
问题 If I have 2 int or long long variables, call them a and b , and I want to compute the sum (a + b) mod p , where p is a large prime integer, how can I utilize the modulo operator in C++ to achieve the desired result? I have tried (a + b) % p , but this gives overflow sometimes, since a + b will overflow before the mod is applied. Other similar approaches I have tried seem to avoid overflow, but give an incorrect result. How can I use the modulo operator in this case to correctly compute the

Sum Top 10 Values

北慕城南 提交于 2021-02-08 04:05:26
问题 I’ve searched and I know this has been asked before but I am struggling to get my head around what I can / can’t do. My cycling club records race results each time a rider has entered a race. Each result is awarded points - 50 for 1st, 49 for 2nd etc. So the table looks like resultid(pk) | riderid(fk) | leaguepts 1 1 50 2 2 49 3 3 48 4 1 50 5 2 42 6 3 50 7 4 30 ...etc I am trying to extract the sum of top 10 points awarded for each riderid from the results table. (the actual database is a bit

SQL SELECT Sum values without including duplicates

橙三吉。 提交于 2021-02-08 02:20:01
问题 I have a problem in Oracle SQL that I'm trying to get my head around. I'll illustrate with an example. I have three tables that I am querying: Employees __________________________________________ | EmployeeID | Name | | 1 | John Smith | | 2 | Douglas Hoppalot | | 3 | Harry Holiday | ... InternalCosts ________________________________ | IntID | Amount | EmployeeID | | 1 | 10 | 1 | | 2 | 20 | 2 | | 3 | 30 | 1 | ... ExternalCosts ________________________________ | ExtID | Amount | EmployeeID | |

SQL SELECT Sum values without including duplicates

送分小仙女□ 提交于 2021-02-08 02:13:41
问题 I have a problem in Oracle SQL that I'm trying to get my head around. I'll illustrate with an example. I have three tables that I am querying: Employees __________________________________________ | EmployeeID | Name | | 1 | John Smith | | 2 | Douglas Hoppalot | | 3 | Harry Holiday | ... InternalCosts ________________________________ | IntID | Amount | EmployeeID | | 1 | 10 | 1 | | 2 | 20 | 2 | | 3 | 30 | 1 | ... ExternalCosts ________________________________ | ExtID | Amount | EmployeeID | |

SQL SELECT Sum values without including duplicates

北城以北 提交于 2021-02-08 02:13:17
问题 I have a problem in Oracle SQL that I'm trying to get my head around. I'll illustrate with an example. I have three tables that I am querying: Employees __________________________________________ | EmployeeID | Name | | 1 | John Smith | | 2 | Douglas Hoppalot | | 3 | Harry Holiday | ... InternalCosts ________________________________ | IntID | Amount | EmployeeID | | 1 | 10 | 1 | | 2 | 20 | 2 | | 3 | 30 | 1 | ... ExternalCosts ________________________________ | ExtID | Amount | EmployeeID | |