How to perform SUM of rows in MySQL - mysql

I have a table called temp_reading. It has the following columns (consumption is an index key):
id consumption total
1 100
1 200
1 300
1 400
2 50
2 100
3 200
4 250
Now I want to display total as
id consumption total
1 100 100
1 200 300
1 300 600
1 300 900
2 50 50
2 100 150
3 200 200
4 250 250
Is it possible to display like the above?
I tried the following query:
SELECT id,consumption,sum(consumption) as total
FROM temp_reading
GROUP BY consumption;
Please help me solve this.

I recommend that you add a Primary Key on your temp_reading table. (Read more about Primary Keys) This key will be unique per row. Then you can try this query:
SELECT TR.id
, TR.consumption
, TR.consumption + IFNULL(SUM(TR2.consumption), 0) AS Total
FROM temp_reading TR
LEFT JOIN temp_reading TR2 ON TR.id = TR2.id AND TR.pk > TR2.pk
GROUP BY TR.pk;
I've tried it in SQL Fiddle.

select id,sum(consumption) as total
from temp_reading
group by id;
I suggest you do not have the same ID (1,1,1.2,2...)

Try this:
SELECT id, consumption, IF(#s=#s:=id, #s2:=#s2+consumption, #s2:=consumption) AS total
FROM temp_reading, (SELECT #s:=0, #s2:=0);

Related

MySQL Select records exceeding the cumulative total

Given I have following table
Id
FileSizeMB
1
100
2
100
3
100
4
100
5
100
6
100
I want to select oldest records exceeding a cumulative value, in this case say 500.
So something like this
Id
Cumulative_FileSizeMB
6
100
5
200
4
300
3
400
2
500
1
600
I want to select only records with id 2 and 1 as they are >= 500.
Goal is to delete them.
Thanks
For anyone with same problem.
I have reached this solution using Mysql window functions,
and also there is no need to declare a variable for cumulative total
SELECT * from (
SELECT
id,
FileSizeMB,
SUM(FileSizeMB) OVER (ORDER BY id DESC) AS TotalFileSizeMB
FROM table
) as t1
WHERE TotalFileSizeMB > 500

Merging two tables using SQL

I have two tables that I need to merge.
Table 1 is :
ID
Product code
Spend
1
101
100
1
102
200
1
103
300
2
201
400
3
301
500
3
302
600
Table 2 has
ID
Product code
Spend
Product tenure
1
101
100
20
1
102
200
30
3
302
600
40
I want to merge these such that only ID's present in table 2 are retained from table 1. Table 2 does not contain all the product codes for each ID, but I want my final table to have it.
Output must have
ID
Product code
Spend
Product tenure
1
101
100
20
1
102
200
30
1
103
300
3
301
500
3
302
600
40
Any help on this would be appreciated. I tried left join on ID, but it produces many duplicates.
SELECT *,
(
SELECT `product_tenure`
FROM `second_table`
WHERE `second_table`.`id` = `first_table`.`id`
AND `first_table`.`product_code` = `second_table`.`product_code`
) product_tenure
FROM `first_table`
WHERE `id` IN (SELECT DISTINCT `id` FROM `second_table`)
Explaination:
Select id from second table, which wanted to keep from first table.
Because the product_tenure only in second_table select them with combined id and product_code
Result:
Test this:
SELECT *
FROM t1
LEFT JOIN t2 AS t21 USING (ID, `Product code`)
WHERE EXISTS ( SELECT NULL
FROM t2 AS t22
WHERE t1.ID = t22.ID )
PS. The query will return 2 Spent columns (they're present in each table, and nothing prevents their values to be not equal...) - so replace an asterisk with definite columns list.

MySQL Query SUM Total Based on Column Value

I have table like this
Order D.O Cost Maintenance Total
ORD-0005 1 100 50 150
ORD-0005 2 50 120 170
ORD-0006 3 200 100 300
ORD-0006 4 150 50 200
Now I want to have total SUM based on Column 'ORDER'
So the result will look like this
Order D.O Cost Maintenance Total
ORD-0005 1 100 50 320
ORD-0005 2 50 120 320
ORD-0006 3 200 100 500
ORD-0006 4 150 50 500
The total value is sum from all cost+maintenance refer to Order Column
Thank you..really appreciated it
You could use a sub-query that calculates the SUM and then join this to the output:
SELECT a.`Order`,
a.`D.O`,
a.`Cost`,
a.`Maintenance`,
b.`Total`
FROM `myTable` a
INNER JOIN (
SELECT `Order`,
SUM(`Total`) AS `Total`
FROM `myTable`
GROUP BY `Order`
) b ON a.`Order` = b.`Order`
ORDER BY a.`Order`
This will calculate the total value for each order in the sub-query, and then include that total value in the output for each Order.
Output:
Order
D.O
Cost
Maintenance
Total
ORD-0005
1
100
50
320
ORD-0005
2
50
120
320
ORD-0006
3
200
100
500
ORD-0006
4
150
50
500
Seen here in this working fiddle.

Mysql query to sum rows and columns based on column criteria

Please help me with writing a query for the following condition. I have a table which I have listed below
ID Wt1 Wt1_Type Wt2 Wt2_Type Wt3 Wt3_Type Wt4 Wt4_Type
--------------------------------------------------------------
1 200 1 220 1 300 2 400 3
2 100 4 150 3 100 5 120 1
3 100 3 110 1 200 5 100 4
I want a query to sum all the the weights (wt1, wt2, wt3, wt4) grouped on the weight type (wt1_type, wt2_type, wt3_type, wt4_type).
The output should look like
Wt_type Total
1 650
2 300
3 650
4 200
5 300
Can someone please help me draft a mysql query to get this result ?
Thanks
You can try below - using union all and subquery
select Wt_Type,sum(Wt) as total from
(
select Wt1_Type as Wt_Type,Wt1 as Wt from tablename
union all
select Wt2_Type ,Wt2 from tablename
union all
select Wt3_Type ,Wt3 from tablename
union all
select Wt4_Type ,Wt4 from tablename
)A group by Wt_Type
Rather than giving the answer by #fa06, which should work for you, I am going to suggest using a better table design. Here is how you should be storing your data:
ID Type Wt
-------------
1 1 200
2 4 100
3 3 100
4 1 220
5 3 150
6 1 110
7 2 300
8 5 100
9 5 200
10 3 400
11 1 120
12 4 100
Note that there is a single column which stores the type and a single column for that type's weight. Now your expected output just requires a very simple query:
SELECT Type, SUM(Wt) AS Total
FROM yourTableUpdated
GROUP BY Type;
Databases are really good at performing operations across rows, much less so across columns.
Use this it should be work
select Wt_Type,sum(Wt) as total from ( select Wt1_Type as Wt_Type,Wt1 as Wt from tablename union all select Wt2_Type ,Wt2 from tablename union all select Wt3_Type ,Wt3 from tablename union all select Wt4_Type ,Wt4 from tablename )A group by Wt_Type

Combining results of two different tables in MYSQL

I have to combine two tables with different sets of columns by a 'salesperson' column.
The problem with the query I've got so far is that some salespeople names are duplicated, and some from the right table are missing.
Transactions table
salesperson, Profit, Units
John 100 1
John 50 1
Carl 200 2
Matt 300 3
Connections table
salesperson, Amount
Carl 100
Lynda 200
Lucy 300
Combined table
salesperson, (Amount+Profit), Units(sum)
Carl 300 2
John 150 2
Matt 300 3
Lynda 200 0
Lucy 300 0
This is what I've got so far
SELECT t.salesperson, SUM(t.profit) + SUM(c.amount), SUM(t.units)
FROM transactions AS t
FULL OUTER JOIN connections as c ON t.salesperson = c.salesperson
GROUP BY t.salesperson
ORDER BY t.salesperson ASC
Any help would be greatly appreciated.
SELECT salesperson, SUM(total), SUM(Units)
FROM
(
SELECT salesperson, Amount as total, Units
FROM Transactions
UNION ALL
SELECT salesperson, Profit as total, 0 as Units
FROM Connections
) T
GROUP BY salesperson