adding summary row to the end of the table - mysql

Hi I have a table with 4 columns. the table is as below
sampleId totalAmount discount netAmount
1 120 40 80
2 200 50 150
3 400 100 300
Now i want the totals summary row at the bottom of the table. Please look at the image file below. how can i achieve this?

You can use UNION ALL as below
select cast(sampleId as char(10)) as sampleId, totalAmount,discount, netAmount
from tab
union all
select 'Total', sum(totalAmount),sum(discount), sum(netAmount)
from tab
SqlFiddle Demo
1st column is converted to varchar becouse you want to Total word atthe bottom. Columns types in UNION must be the same type.

You may do union all
select * from tablename
union all
select 'Totals' as sampleId,
sum(totalAmount) as totalAmount,
sum(discount) as discount,
sum(netAmount) as netAmount
from tablename
Here is a demo
mysql> select * from test ;
+------+--------+----------+-----------+
| id | amount | discount | net_total |
+------+--------+----------+-----------+
| 1 | 120 | 40 | 80 |
| 2 | 200 | 50 | 150 |
| 3 | 500 | 100 | 300 |
+------+--------+----------+-----------+
3 rows in set (0.00 sec)
mysql> select * from test union all select 'Totals' as id,sum(amount) as amount,sum(discount) as discount,sum(net_total) as net_total from test ;
+--------+--------+----------+-----------+
| id | amount | discount | net_total |
+--------+--------+----------+-----------+
| 1 | 120 | 40 | 80 |
| 2 | 200 | 50 | 150 |
| 3 | 500 | 100 | 300 |
| Totals | 820 | 190 | 530 |
+--------+--------+----------+-----------+

SELECT
IFNULL(sampleId,"Total") as sampleId, SUM(totalAmount), SUM(discount), SUM(netAmount)
FROM tablename
GROUP BY sampleId WITH ROLLUP;

Try to use group by modifiers :
select Coalesce(sampleId, 'Total') `sampleId` , sum(totalAmount),sum(discount),sum(netAmount)
from t
group by sampleId with rollup
SQLFiddle : http://sqlfiddle.com/#!9/f1826/11
Here is a documentation on ROLLUP : https://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html

Related

How to calculate the remaining amount per row?

I have a wallet table like this:
// wallet
+----+----------+--------+
| id | user_id | amount |
+----+----------+--------+
| 1 | 5 | 1000 |
| 2 | 5 | -200 |
| 3 | 5 | -100 |
| 4 | 5 | 500 |
+----+----------+--------+
I want to make a view that calculates the remaining amount per row. Something like this:
+----+----------+--------+------------------+
| id | user_id | amount | remaining_amount |
+----+----------+--------+------------------+
| 1 | 5 | 1000 | 1000 |
| 2 | 5 | -200 | 800 |
| 3 | 5 | -100 | 700 |
| 4 | 5 | 500 | 1200 |
+----+----------+--------+------------------+
Any idea how can I do that?
MySQL 8 has window function for that purpose, like SUM() OVER
for your sample data, this will calculate the running SUM for every user_id
vital for th function to work is the PARTITION BY and the ORDER BY to get the right amount
The PARTITION BY is used to get sums for a user_id, so if you had user 5,6,7,8 it will correctly add (or subtract) the maount theat that user produced.
The ORDER BYis needed to get the right mount at the corect position. Tables are by nature unsortede, so an ORDER BY is needed to give the outout the corect order, if the ids where changed, you would get another mount, as it will be prior added to the running sum
SELECT
`id`, `user_id`, `amount`
, SUM(`amount`) OVER(PARTITION BY `user_id` ORDER BY `id`) run_sum
FROM wallet
id
user_id
amount
run_sum
1
5
1000
1000
2
5
-200
800
3
5
-100
700
4
5
500
1200
fiddle
Do not know if this meets your demands or not
SELECT
t1.id,t1.user_id,t1.amount,
(
SELECT sum(t2.amount) FROM yourtable t2 WHERE t2.id<=t1.id AND t1.user_id=t2.user_id
) as remaning_amount
FROM yourtable t1

How to SUM of rows on Condition in next column using SELECT

I have a dataset/table structure like below
|Dept|Rate|No.Of Employee |
|----|----|---------------|
| A | 8 | 2 |
| A | 5 | 2 |
| B | 10 | 2 |
| B | 5 | 2 |
Expecting the output of the SELECT / SQL to be
|Dept|Rate|No.Of Employee | TotalHoursPerWeek | TotalCostPerWeek |TotalCostPerEmplPerDept |
|----|----|---------------|---------------------|--------------------|------------------------|
| A | 8 | 2 | 80 | 640 | 1040 |
| A | 5 | 2 | 80 | 400 | 1040 |
| B | 10 | 2 | 80 | 800 | 1200 |
| B | 5 | 2 | 80 | 400 | 1200 |
I have tried below SELECT, however not able to SUM 'TotalTotalCostPerWeek' based on 'Dept' & 'Employee'
Please note SUM(TotalCostPerWeek 'per' Dept) in below query is more for representation purpose, as I know/understand it will not work in SQL, hence need help/suggestion on how to get this kind of result using SELECT statement.
SELECT Dept, Rate, NoOfEmployee,
(NoOfEmployee * 40) AS TotalHoursPerWeek,
(NoOfEmployee * 40* Rate) AS TotalCostPerWeek,
SUM(TotalCostPerWeek 'per' Dept) AS TotalCostPerEmplPerDept
FROM TABLE
GROUP BY Dept, Rate;
I think I understand what you need and you can use "case when" to achieve this...
Select Dept,
Rate,
No_of_employee,
TotalHoursPerWeek = (No_of_employee * 40),
TotalCostPerWeek = (No_of_employee * 40 * Rate)
TotalCostPerEmplPerDep = case when Dept ='A' then (select SUM(No_of_employee * 40 * Rate) from table where Dept = 'A')
else (select SUM(No_of_employee * 40 * Rate) from table where Dept <> 'A')
from table
You have only aggregation function for Dept, while the othersc olumn don't need aggregation
in this case you shoudl use a join on a subquery for aggregated result
SELECT Dept, rate, NoOfEmployee,
(NoOfEmployee * 40) AS TotalHoursPerWeek,
(NoOfEmployee * 40 * Rate) AS TotalCostPerWeek
from TABLE
INNER JOIN (
SELECT Dep, SUM(ToOfEmployee * 40 * Rate) AS TotalCostPerEmplPerDept
FROM TABLE
GROUP BY Dept
) t on t.Dept = TABLE.Dept

Two records limit when the same column

I have column:
id | id_contract | price
I'd like to select all with the limit 2 the cheapest offer from one contract.
I use kochana ORM.
Thanks.
For example
1 | 1 | 100 *
2 | 1 | 500
3 | 1 | 300 *
4 | 1 | 900
5 | 2 | 1000
6 | 2 | 100 *
7 | 2 | 200 *
8 | 3 | 10000 *
This is what I want to select.
You can do this in MySQL with the following query:
select t.*
from table t
where (select count(*)
from table t2
where t2.id_contract = t.id_contract and
t2.price <= t.price
) <= 2;

MySQL group by with MAX not working as expected?

I have a table:
ID | User | Amount
1 | 1 | 50
2 | 1 | 80
3 | 2 | 80
4 | 2 | 100
5 | 1 | 90
6 | 1 | 120
7 | 2 | 120
8 | 1 | 150
9 | 2 | 300
I do a query:
SELECT * FROM TABLE ORDER BY amount DESC group by userid
I'm getting this:
ID | User | Amount
1 | 1 | 50
2 | 1 | 80
But I was expecting:
ID | User | Amount
9 | 2 | 300
8 | 1 | 150
What is wrong with my sql?
When grouping you have to use aggregate functions like max() for all columns that are not grouped by
select t.*
from table t
inner join
(
SELECT userid, max(amount) as total
FROM TABLE
group by userid
) x on x.userid = t.userid and x.total = t.amount
ORDER BY t.amount DESC
Another solution.Check SQL Fiddle
Using FIND_IN_SET clause
SELECT
ua.*
FROM user_amount ua
WHERE FIND_IN_SET(ua.amount,(SELECT
MAX(ua1.amount)
FROM user_amount ua1
WHERE ua1.user = ua.user)) > 0
ORDER BY amount desc;
Using IN clause
SELECT
ua.*
FROM user_amount ua
WHERE ua.amount IN (SELECT
MAX(ua1.amount)
FROM user_amount ua1
WHERE ua1.user = ua.user)
ORDER BY amount desc

Mysql Query group by

I am trying to make a query to get some results:
I have a table with some data:
client | price
1 | 100
1 | 150
1 | 200
2 | 90
2 | 130
2 | 200
3 | 95
3 | 120
3 | 250
I would like with one query to select the results and order it by price and client and get them in this form, ordered by the best price of each clint:
2 | 90
2 | 130
2 | 200
3 | 95
3 | 120
3 | 250
1 | 100
1 | 150
1 | 200
SELECT tbl.client, ytbl.price
FROM (SELECT client, min(price) as mpr FROM yourtable group by client) tbl
JOIN yourtable ytbl ON ytbl.client=tbl.client
ORDER BY tbl.mpr ASC, tbl.client ASC, ytbl.price ASC
Something like that...