Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [sum]

The tag has no usage guidance.

0
votes
1answer
17 views

Summing Column values based on group by criteria

So I have this query select *, TIMEDIFF(tbltimetracker.tt_endtime, tbltimetracker.tt_begintime) as hours from tbltimetracker LEFT JOIN tblcustomers on tbltimetracker.customer_id = ...
0
votes
1answer
12 views

SQLite return sum of duplicate rows based on array

If I have a simple table fruits that looks like this: +---+---------+------+ | | name | lbs | +---+---------+------+ | 1 | apple | 0.25 | | 2 | melon | 3.00 | +---+---------+------+ In a ...
1
vote
1answer
28 views

How to sum of column by depending other column in my sql?

how to calculate / SUM of column by the latest data of updateid column ? I try this : SELECT * FROM yourTable; SELECT ANY_VALUE(code) AS code, COUNT(*) AS total_rows, SUM(data->'$.chart."21"') ...
1
vote
0answers
25 views

MySQL: sum values with join and group concatenation

I've got the following tables: users: mysql [test]> SHOW CREATE TABLE users\G *************************** 1. row *************************** Table: users Create Table: CREATE TABLE `users` ...
0
votes
0answers
38 views

MySQL how to GROUP SUM based on JOIN TABLE

I have Three Table, One Master Table and Other Two Table have relation with it. Below is the Three Table: 1. ball_by_ball table, this table holds full information by ball by ball: CREATE TABLE `...
2
votes
3answers
51 views

How to group many information une one select

There are my database articles id | art_code | art_name 1 | 5678 | computer 2 | 0987 | display 3 | 8547 | keyboard action id | code | action | qte 1 | 5678 | IN ...
0
votes
1answer
38 views

Complex join 3 tables with sum and group by

I'm trying to generate a report for one of our departments that pulls data from 3 tables. The basic structure is below: CREATE TABLE `evd_us_county_mio_2018q4` ( `state_code` INT(11) NULL DEFAULT ...
0
votes
1answer
30 views

Get percentage from results from two case results

How i can get percentage from two resuts from case staments? SELECT *, COUNT(CASE WHEN estado_rep >=11 and RELATION=1 THEN 1 ELSE NULL END) as pendiente, COUNT(CASE WHEN estado_rep >=...
0
votes
0answers
23 views

Why sum with previous and current column do't working

I want to update my specific row with that row previous value, after searching i found some post about this but those solution not working with me. So what is wrong with my query or how can i ...
0
votes
1answer
157 views

Compare sum of columns between tables

I'm trying to create a query for this problem. Suppose I have these two tables: Acceptor table: ID TYPE QTY -- ---------- ------------------ 1 AB+ 1 2 O- 5 3 O+ ...
0
votes
0answers
30 views

Force query plan to filter early when querying view? [duplicate]

I've got a view defined that lists transactions together with a running total, something like CREATE VIEW historyView AS SELECT a.createdDate, a.value, m.memberId, ...
0
votes
1answer
72 views

Sum(JSON_EXTRACT) MariaDB 10.3.11

I'm looking so long, and cannot find anything right concerning - ho to sum the values from JSON column. My values in the column looks like this: [{"file":"FILE.doc", "POSITION":"3245", "NUMBER":"...
0
votes
2answers
17 views

sum only different id's values with any aggreagte function in postgresql

I need your help. Let's assume that we have a table like below +---------------+-------+--------+----------+ | barcode | shop | id | entrance | +---------------+-------+--------+----------+...
0
votes
1answer
23 views

How to calculate the final value of a row based on another table with costs in PostgreSQL

I'm new to SQL and I have two tables like those in my PostgreSQL database: table 1 - building_specs +-----+---------+---------+--------+ | key | wallsId | floorId | roofId | +-----+---------+--------...
0
votes
1answer
24 views

SQL query on SUM function plus JOINS

Sorry if this has been covered before but I couldnt find an example to my issue Situation: Say I have a table of sales and want to display them Step 1) SELECT payment_id, amount, payment_date FROM ...
-3
votes
1answer
681 views

T-SQL: how to use SUM() with GROUP BY

I have a very basic question: I have this code which works and returns me 23 rows: select Issue_ID ,sum(HoursSpent) as TimeSpent --,DateClosed from SixMonthsReview where StaffAssigned = 'Teddy ...
0
votes
2answers
38 views

Select Sum from two joined tables

There are structures: CREATE TABLE `invoices` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `date` date NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB; INSERT INTO `invoices` VALUES (1,'2018-09-22')...
1
vote
4answers
542 views

How do you SUM a column without having its name?

I'm stuck in a way that how to do sum on a column let's say first column of a table (result from another query) without knowing the column name with something like column position id. it's something ...
0
votes
1answer
1k views

In PHP MySQL how to sum or not sum multiple columns?

I have three tables this is my codes: <table border='1'> <tr> <th>Subject</th> <th>Mark1</th> <th>Mark2</th> &...
2
votes
1answer
27 views

Why does this query not return records where both fields using CASE WHEN are zero?

I'm attempting challenge 13 on this SQLzoo page, but my query returns one fewer row than it's supposed to. SELECT mdate, team1, SUM(CASE WHEN teamid=team1 THEN 1 ELSE 0 END) AS score1, team2, ...
0
votes
2answers
66 views

Calculate ticket sales by using SUM in SELECT clause

In this ticketing setup on dbfiddle, I am trying to calculate the ticket sales for a particular event by multiplying the ticket price and the total number of tickets sold for that event, based on ...
-2
votes
2answers
1k views

How to calculate balance from two tables in MySQL

I have two tables as follows: tbl_credit id cr_amount client_id tbl_debit id dr_amount client_id I want to calculate total sum(cr_amount)-total(dr_amount) where group by client_id.
5
votes
1answer
354 views

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 ...
0
votes
1answer
187 views

Calculate difference between SUM over last 30 days and the SUM for 30 days preceding this period (days 30-60) in Postgres

Every day we store the number of jobs posted for hundres of thousands of companies. We need to query: The SUM of jobs_posted for the last 30 days for each company; The DELTA between jobs posted in ...
0
votes
4answers
56 views

Help for grouping a field and taking total sum

I have a sorted data in a table F1 F2 F3 F4 A M X 1 A N Y 2 A P X 3 B M X 3 B N Y 1 B P X 5 but I want the sub count of values in F1 column for every row. ...
0
votes
1answer
897 views

MySQL - maximum of sum over different months [duplicate]

I have a table called commodities where I have nature_of_goodscolumn in it. nature_of_goods contains all the different types of goods like machinery, household etc. I have another column called as ...
0
votes
1answer
66 views

How to get 0 in the null value fields

select * from ( select type,SUM(ISNULL(amount,0)) AS amount,month(date) as date from tbl_cashmaster where date between '2016/12/31' and '2017/12/31' group by type,month(date) ) src ...
2
votes
1answer
1k views

Summation of distinct rows - SQL Server

I am pretty sure I am making a basic mistake, but I can't figure it out for the life of me. I am trying to get a simple report of total number of unique employees, number of employees who are male, ...
1
vote
1answer
1k views

SQL - sum columns with same name from diferent tables

I have 3 Tables: _tbl1_____ ID C1 C2 -- -- -- AA 2 3 BB 10 12 CC 20 22 _tbl2_____ ID C1 C2 -- -- -- BB 10 12 AA 2 3 CC 20 22 _tbl3_____ ID C1 C2 -- -- -- AA 0 0 ...
2
votes
2answers
4k views

Compare sum of two select results

Using Firebird, I have two tables, Orders and DespatchedParts. Orders table: ID CustomerID Total_number_parts -- ---------- ------------------ 1 1 5 2 1 4 3 2 ...
0
votes
2answers
1k views

SQL Select where value = to another select [closed]

I have two tables. Orders, and OrderParts. One Order can have many OrderParts. I want a list of all the order.customerID where the number of OrderParts ordered = Total_number_parts in the order table....
0
votes
1answer
32 views

query SUM function issue/ grouping issue

I am running into an issue where using the Sum() function is not working as it should be. I am running this query. SELECT tServiceReps.EmployeeNumber, tServiceHours.JobCode, ...
1
vote
2answers
1k views

Update a table using summed values from another table in MySQL/mariaDB

I was trying to use the query below to update the ORDERS table by making the O_TOTALPRICE column up-to-date when the LINEITEM table is modified. And I was using a standard TPC-H database. UPDATE ...
0
votes
2answers
1k views

Sum on distinct and group by

I have this : CREATE TABLE tickets(id serial PRIMARY KEY, source text NOT NULL, user_id int NOT NULL, shop_id int NOT NULL, created_at time DEFAULT current_time); INSERT INTO tickets VALUES (0, 'web-...
6
votes
1answer
644 views

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 ...
0
votes
2answers
1k views

limiting the rows to where the sum a column equals a certain value [duplicate]

i m trying to limit the rows to where the sum a column equals a certain value but i m getting unexpected results. CREATE TABLE buy ( id int not null, volume decimal(9,4) not null, price decimal(...
1
vote
1answer
84 views

How to three way inner join to three way outer join in MySQL?

We are currently joining three MySQL tables based on the summation of a common key within each table: select a.common_key, a.val, b.val, c.val from (select common_key, sum(val) val from table_a ...
2
votes
1answer
1k views

How can I get this “rolling total” query to sum correctly?

I have a problem with the following query: SELECT t1.idcliente, c.nombre,c.apellido, t1.fecha, t1.haber, t1.debe, SUM(t2.haber -t2.debe) as SUM ...
3
votes
1answer
730 views

SUM( A + B ) vs SUM(A) + SUM(B) [closed]

I came across a script written by one of my coworkers and found a line like the following SELECT SUM(A) + SUM(B) FROM TABLE I proposed a different approach SELECT SUM( A + B ) FROM TABLE Which ...
0
votes
1answer
151 views

Count number of rows and sum up a field of all tables in all relevant schemas with a readonly connection

We have a rather complex database setup which involves 500 schemas. Most of these schemas contain a certain table. This table contains a certain number of rows and those rows contain a size field. I ...
3
votes
2answers
10k views

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 ...
1
vote
1answer
4k views

MySQL: SELECT with SUM() and COUNT() as new columns

What is the correct SELECT statement to produce this desired output? +------------+----------+-------------+-----------------+ | QtyOfParts | FamilyID | NumInFamily | TotalQtyOfParts | +------------+-...
4
votes
3answers
3k views

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, ...
0
votes
1answer
309 views

Iterating through results and performing sum on values

I'm using MySQL and not really sure where to begin with this. I've got a query which returns N dates for a given company (N is likely to be 10 but could be more/less). These dates are returned as such ...
1
vote
1answer
29 views

Sum of different table counts

I have 2 different query and i want to summarize their count in other query like this: select a,((select count(id) from foo) + (select count(id) from bar)) as 'alpha' from beta; Is there a ...
1
vote
0answers
236 views

Most efficient way to solve the subset sum problem in PostgreSQL

I'm looking now at the subset sum problem... inspired from this post. It's an interesting problem.. Here we create two tables foo with 1,000 rows (id,rand) between 5..105 bar with 10 rows (id,rand) ...
1
vote
1answer
59 views

How to use PIVOT here?

I have this table +--------+--------------+----------------+--------------+----------------+ | XPK | First_count | First_amount | Second_count | Second_amount | +--------+--------------+------...
2
votes
1answer
259 views

Insert characters into entire row and union with continue of row_number

I have first query IF OBJECT_ID('tempdb..#tmp1') IS NOT NULL DROP TABLE #tmp1 select IDX=ROW_NUMBER() over(order by A_ID) ,A = A_ID ,Number = COUNT(*) ,Value = sum(ammount) into #...
5
votes
2answers
103 views

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 ...
3
votes
1answer
2k views

Rolling SUM with a 30 day range

I am using SQL Server 2012 and have a db with around 2 million rows. I need to figure out how to calculate a rolling sum over a 30 day range and return any results over a set amount (amt_pur). These ...