MySQL Query SUM Total Based on Column Value - mysql

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.

Related

How to SUM minus INT Currency

I have a transaction table with positive and negative numbers and a budget table. A negative number will increase if being minus with budget.
bud_tab
id
budget
1
90
2
80
3
50
4
30
trans_tab
id
trans
1
-50
2
80
3
-70
4
60
What query should I use to get an output like this:
budget
trans
total
90
-50
140
80
80
0
50
-70
120
30
60
-30
From the look of output, this seems like a basic math calculations. Am I missing something ?
select budget, trans, budget - trans as total from bud_tab natural join trans_tab ;

How to find the smallest value in a row

i need help to create a sql query that can find the smallest value in 1 row , and display it in the last column, like this table.
id
out
mid
in
Smallest
1
200
100
50
50
2
100
150
50
50
3
200
100
250
100
4
50
100
150
50
5
50
100
100
50
6
20
200
100
20
7
-
-
100
100
8
150
-
100
100
this is my query :
On MySQL you may use the scalar LEAST() function:
SELECT id, `out`, mid, `in`, LEAST(`out`, mid, `in`) AS Smallest
FROM yourTable;
If your database doesn't have a LEAST function, we can use a CASE expression as an alternative:
SELECT id, `out`, mid, `in`,
CASE WHEN `out` < mid AND `out` < `in` THEN `out`
WHEN mid < `in` THEN mid
ELSE `in` END AS Smallest
FROM yourTable;
Side note: Both IN and OUT are reserved MySQL keywords, and you should avoid naming your columns with them.

How to get cumulative total for previous month and upto this month?

ID pcID contractor approver claimed
-------------------------------------------
1 1 one 1000 900
2 1 two 200 100
3 1 three 1000 1000
4 1 six 100 11
5 2 six 100 22
6 3 six 120 1
7 4 three 102 10
From the above table, I need to get cumulative amount for upto this month and previous month of approver and claimed and also current month approver, claimed amount based on the contractor. Like below table.
ID contractor approver claimed uptothisMTApprover uptothisMTClaimed previousMTApprover previousMTClaimed
-----------------------------------------------------------------------------------------------------------------
1 one 1000 900 1000 900 0 0
2 two 200 100 200 100 0 0
3 three 102 10 1102 1010 1000 1000
4 six 120 1 320 34 200 33
Thanks in advance..
You seem to want the latest row per contractor, as defined by pcID, and a cumulative sum of all previous months.
You can use window functions:
select contractor, approver, claimed,
total_approver as uptothisMTApprover,
total_claimed as uptothisMTClaimed,
total_approver - approver as previousMTApprover,
total_claimed - claimed as previousMTClaimed
from (
select t.*,
row_number() over(partition by contractor order by pcID desc) rn,
sum(approver) over(partition by contractor) total_approver,
sum(claimed) over(partition by contractor) total_claimed
from mytable t
) t
where rn = 1

How to perform SUM of rows in 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);

change multi rows to one row-multi column

For some reason i have to do this. i have a query that have result like this :
limit usage tariff total
0 10 10 700 7000
11 20 10 900 9000
21 30 10 1800 18000
31 > 11 2700 29700
the query return 4 rows maximum (like above) or sometime just 3 rows.
I want to change the rows to just one row and multi column like this (the list below just one row):
limit1 usage1 tariff1 total1 limit2 usage2 tariff2 total2
0 10 10 700 7000 11 20 10 900 9000
limit3 usage3 tariff3 total3 limit4 usage4 tariff4 total4
21 30 10 1800 18000 31 > 11 2700 29700
if the query return just 3 rows, the values in column limit4 until total4 will be empty. I dont know how to do like that.
EDITED
I add one ID column so the list will be :
ID limit usage tariff total
1 0 10 10 700 7000
2 11 20 10 900 9000
3 21 30 10 1800 18000
4 31 > 11 2700 29700
I try to make it one row like this :
SELECT e.*,f.id AS id4,f.limit AS limit4,f.usage AS usage4,f.tariff AS tariff4,f.total AS total4
FROM
(SELECT c.*,d.id AS id3,d.limit AS limit3,d.usage AS usage3,d.tariff AS tariff3,d.total AS total3
FROM
(SELECT b.id AS id1,b.limit AS limit1,b.usage AS usage1,b.tariff AS tariff1,b.total AS total1,
a.id AS id2,a.limit AS limit2,a.usage AS usage2,a.tariff AS tariff2,a.total AS total2
FROM testtariff a
INNER JOIN testtariff b ON a.id!=b.id
LIMIT 1) c INNER JOIN testtariff d ON c.id1 != d.id AND c.id2 != d.id
LIMIT 1) e INNER JOIN testtariff f ON e.id1 != f.id AND e.id2 != f.id
AND e.id3 != f.id
LIMIT 1
it work as i expected for 4 rows but not work for 3 rows. should i use cursor ?
This is generally called a "pivot". Here's how you do it:
select
'0 10' as limit1,
sum(limit between 0 and 10 * usage) as usage1,
sum(limit between 0 and 10 * tariff) as tariff1,
sum(limit between 0 and 10 * usage * tariff) as total1,
'11 20' as limit2,
sum(limit between 11 and 20 * usage) as usage2,
sum(limit between 11 and 20 * tariff) as tariff2,
sum(limit between 11 and 20 * usage * tariff) as total2,
-- etc
from mytable
group by 1,5 -- etc
This works because limit between x and x is 1 if true and 0 if false, so using multiplying by this is a simple way to filter the results into different groups.