Questions tagged [sum]

147 questions
10
votes
2 answers

How to Get the Sum (total) of a column in a query result set

Is there a way that I can get the sum of a column in my query result set by just clicking on the column (something like in Excel) so that I don't have to copy and paste the column into Excel to the see the total sum of all the values in the…
Juan Velez
  • 3,165
  • 13
  • 48
  • 67
10
votes
1 answer

Sum Interval of Dates within same column

How do you best sum the differences of a range of dates in the same column between interleaving rows? I have a Datetime column and want to calculate the difference between Rows. I want the difference in Seconds. This questions is not about how to…
C Smith
  • 101
  • 1
  • 4
9
votes
3 answers

Compute the total of SUM(column)

I have this code that sums up the qty for a certain item (itemid) and by its product date code (proddte). select sum(qty), itemid, proddte from testtable where .... group by itemid, proddte What I want to do is to get the total of all qty…
niq
  • 103
  • 1
  • 1
  • 5
8
votes
1 answer

Left outer join not returning all rows in a grouping query

I'm trying to query all users to find the price of orders in the last month. I'm doing this on PostgreSQL. Here's a toy database to show my problem: I have a people table and an orders table. I'm trying to find the sum of orders for all people in…
user1276560
  • 135
  • 2
  • 6
6
votes
1 answer

Grouping data based on cumulative sum

I have been looking online for an answer but do not really know how to formulate correctly what I would like to achieve and whether it's possible, sorry if the question sounds dumb. I am using Postgresql. I have price data per day. CREATE TEMP…
Nina
  • 63
  • 1
  • 4
6
votes
1 answer

SUM CASE WHEN CLAUSE

I have the below Query and I want the Ageing profile to show as a new column my ageing profile is 0-30, 31-60, 61-90, 91-120, 121-180, 181-365, 365+ I basically want the results to appear lie this with the Net due appearing in the appropriate…
Chris
  • 61
  • 1
6
votes
2 answers

SUM() ignores GROUP BY and sums up 4 rows instead of 2

I'm having difficulty with GROUP BY in MySQL. My database setup: client_visit - id - member_id - status_type_id (type_of_visit table) - visit_starts_at - visit_ends_at member - id schedule_event - id - member_id …
undefinedman
  • 217
  • 1
  • 2
  • 5
5
votes
1 answer

SUM(Amount) different value then sum of lines - SQL Server 2017 (14.0.3045.24)

I am using following Query to get the SUM of field Amount of all lines in my filter: SELECT SUM(Amount) FROM dbo.[CompanyName$Detailed Cust_ Ledg_ Entry] WHERE [Customer No_] = 'XYZ' The result is This result doesn't add up to the sum of…
5
votes
3 answers

Join Multiple Tables for Aggregates

How do I join multiple tables for aggregates with Sum and Count functions? The query I'm trying is as below: Select campaigns.id, campaigns.name, Count(landers.campaign_id) As landers_count, Sum(conversions.revenue) As total_revenue From …
Jani
  • 153
  • 1
  • 4
5
votes
2 answers

How to display queries of separate tables in adjacent columns?

I have two tables - one titled "planning constraints" which contains the 'sot_allowed' time intervals, and one titled "planning" which contains the 'sot_contribution' time interval. Here are the schema of the two tables (edited for readiability): …
Alex Willison
  • 199
  • 10
5
votes
1 answer

SQL Query for cumulative sum

I am experiencing problems with formulating a (relatively) simple SQL Query (SQL Server 2012 is used). We have a database which counts something up for certain users. Therefore we have a really simple database structure consisting of two…
Roland
  • 53
  • 1
  • 3
5
votes
2 answers

MySQL: sum time ranges exluding overlapping ones

I need to sum up the time which results from multiple time ranges. For example - we have enter / exit ranges of some office: Query has to: Exclude overlapping ranges (8:00 - 10:00) Exclude "missing" part (12:00 - 14:00) Expected result in this…
Łukasz
  • 83
  • 1
  • 6
4
votes
1 answer

How to get the sum of a particular item id in a column

Item ItemName PackageID Specifics brandname GRNQty UnitPric GRNValue 102814742801 ALL BRAN FLAKES 1PKT Bran. KELLOGS 7 15.9 111.3 102814742801 ALL BRAN FLAKES 1PKT KELLOGS 50 …
Md Haidar Ali Khan
  • 5,967
  • 9
  • 34
  • 59
4
votes
2 answers

Why does SQlite return incorrect sum?

CREATE TABLE "Test" ("ItemQty" DOUBLE NOT NULL ); Insert Into Test VALUES (1.4); Insert Into Test VALUES (1.4); Insert Into Test VALUES (1.4); Insert Into Test VALUES (1.4); Insert Into Test VALUES (1.4); Insert Into Test VALUES (1.4); Insert Into…
user2125348
  • 143
  • 2
  • 5
4
votes
1 answer

Performance issues summing a count column over a date range

In our application, we have a query which on an API endpoint to return users who have 'activity' to a named client or matter (one or many - specified by ID). This query takes about 15 seconds to return when the activity table has 30 million rows…
Mark Brown
  • 76
  • 6
1
2 3
9 10