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
Related
I have 2 tables Games & Transaction
I use this formula in Games table, sum(EntryFee * Rake/(100 + Rake)*TotalEntry) to get a value
I use this query in Transaction table count(distinct UserID) to get a value
Now i want to divide the value of [sum(EntryFee * Rake/(100 + Rake)*TotalEntry)] and value of [count(distinct UserID)]
for eg sum(EntryFee * Rake/(100 + Rake)*TotalEntry) = 90 and count(distinct UserID) = 3
then 90/3 =30
How can i do this in MYSQL
CREATE TABLE Games (EntryFee INT, Rake INT, TotalEntry INT);
CREATE TABLE Transaction1 (UserID VARCHAR(25));
INSERT INTO Games VALUES
(30,16,150),(45,20,100),(15,5,50),(25,20,300),(10,8,270);
INSERT INTO Transaction1 VALUES ('Daniel'),('David'),('John'),('Martha');
SELECT Games.EntryFee, Games.Rake, Games.TotalEntry, COUNT(distinct Transaction1.UserID) AS CountUser,
(Games.EntryFee * Games.Rake / (100 + Games.Rake) * Games.TotalEntry / COUNT(distinct Transaction1.UserID))
AS Calculate
FROM Games JOIN Transaction1 GROUP BY Games.EntryFee, Games.Rake, Games.TotalEntry;
Result :
+==========+======+============+===========+==============+
| EntryFee | Rake | TotalEntry | CountUser | Calculate |
+==========+======+============+===========+==============+
| 10 | 8 | 270 | 4 | 50.00000000 |
+----------+------+------------+-----------+--------------+
| 15 | 5 | 50 | 4 | 8.92857500 |
+----------+------+------------+-----------+--------------+
| 25 | 20 | 300 | 4 | 312.50000000 |
+----------+------+------------+-----------+--------------+
| 30 | 16 | 150 | 4 | 155.17242500 |
+----------+------+------------+-----------+--------------+
| 45 | 20 | 100 | 4 | 187.50000000 |
+----------+------+------------+-----------+--------------+
sample query
SELECT (
SELECT sum(EntryFee * Rake/(100 + Rake)*TotalEntry) FROM Games
)/(
SELECT count(distinct UserID) FROM Transaction
) MyResult
In the above table I want to sum where ledgertype='Earning' and substract where
ledgertype='Deduction' and display both values..... how to write query?
Thanks in advance...
You can achieve using this. As you wanted to print earnings and deductions as well so I used sub query.
select sum_earnings , sum_deduction , sum_earnings - sum_deduction
from ( select sum(case when ledgertype = 'Earning' then ledgervalue end) sum_earrnings, sum(case when ledgertype = 'Deductions' then ledgervalue end) as sum(sum_deduction)
from ratecard ) a
I am unable to understand "Both Values" but you can get Aggregate of both types by:
Select SUM(ledgerValue), ledgerType FROM ratecard group by ledgerType
SELECT (SUM_VAL - SUBSTRACT_VAL) as balance FROM
(
select sum(ledgerValue) AS SUM_VAL FROM ratecard WHERE ledgerType ='Earning',
select sum(ledgerValue) AS SUBSTRACT_VAL FROM ratecard WHERE ledgerType = 'substract'
) t1
If you want a running total you could use a variable to calculate.
DROP TABLE IF EXISTS T;
CREATE TABLE T (ID INT,AMOUNT INT, TYP VARCHAR(10));
INSERT INTO T VALUES
(1,12500,'Earnings'),(2,3200,'Earnings'),(3,1200,'Earnings'),
(4,1200,'Deductions'),(5,200,'Deductions'),(6,600,'Deductions'),(7,500,'Deductions'),
(8,12000,'Earnings'),(9,3200,'Deductions');
select t.*,
if(t.`typ` = 'Earnings' ,#rt:=#rt+amount,#rt:=#rt-amount) RunningTotal
from t
,(select #rt:=0) rt;
order by t.id
+------+--------+------------+--------------+
| ID | AMOUNT | TYP | RunningTotal |
+------+--------+------------+--------------+
| 1 | 12500 | Earnings | 12500 |
| 2 | 3200 | Earnings | 15700 |
| 3 | 1200 | Earnings | 16900 |
| 4 | 1200 | Deductions | 15700 |
| 5 | 200 | Deductions | 15500 |
| 6 | 600 | Deductions | 14900 |
| 7 | 500 | Deductions | 14400 |
| 8 | 12000 | Earnings | 26400 |
| 9 | 3200 | Deductions | 23200 |
+------+--------+------------+--------------+
9 rows in set (0.00 sec)
I have tableA and want add count colnum to it , count is total rows of tableA and is same at all rows in result table ,tableA like below :
note: table A is result of other query
tableA
id | name | rank |
-------------------
1 | John | 12 |
2 | Maria | 18 |
3 | Steph | 44 |
4 | Jay | 17 |
and result should be :
id | name | rank | total | rank/total
---------------------------------------
1 | John | 12 | 4 | 3
2 | Maria | 18 | 4 | 4.5
3 | Steph | 44 | 4 | 11
4 | Jay | 17 | 4 | 4.25
how can do that with MYSQL
To get the total rows in your table, do this:
SELEcT count(*) FROM TableA
You can then combine this query in the SELEcT of another query and make a computation from it, which gives this :
SELEcT *, (SELEcT count(*) FROM TableA) AS total, rank / (SELEcT count(*) FROM TableA) AS 'rank/total'
FROM tableA
if tableA is the result of another query, do
SELEcT *, (SELEcT count(*) FROM TableA) AS total, rank / (SELEcT count(*) FROM TableA) AS 'rank/total'
FROM ( insert_the_other_query_here ) tableA
Check if using a variable helps you helps you:
SET #total = SELECT COUNT(1) FROM tableA;
SELECT name, rank, #total AS total, rank/#total AS 'rank/total' FROM tableA;
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
i have two tables
td_sell
|----------|----------------|------------------|
| id | user_id | price |
|----------------------------------------------|
| 1 | 2 | 10 |
|----------------------------------------------|
| 2 | 1 | 5 |
|----------------------------------------------|
| 3 | 2 | 3 |
|----------------------------------------------|
and td_commsion
|----------|----------------|------------------|
| id | user_id | price |
|----------------------------------------------|
| 1 | 1 | 3 |
|----------------------------------------------|
| 2 | 1 | 5 |
|----------------------------------------------|
| 3 | 2 | 3 |
|----------------------------------------------|
now i want a sql query like this
SELECT (SUM(td_sell.price) + SUM(td_comission.price)) AS his_earning
FROM td_sell, td_comission
WHERE td_sell.user_id='1'
AND td_comission.user_id='1'
but its showing abnormal result
the result should be 13, but its showing 29
This will work:
SELECT (SELECT SUM(s.price) FROM td_sell s WHERE s.user_id = 1)
+
(SELECT SUM(c.price) FROM td_comission c WHERE c.user_id = 1)
DEMO: SqlFiddle
You are getting the sum of the Cartesian join of the two tables.
http://en.wikipedia.org/wiki/Cartesian_product
SELECT sum(price)
FROM (
SELECT * FROM td_sell
UNION ALL
SELECT * FROM td_commission
) a
where a.user_id=1
Here's a SQL Fiddle:
Fiddle
You need to do the sum separately on each table, before combining the results. Here is one way:
select (sell + commission) as his_earning
from (select SUM(td_sell.price) as sell
from td_sell
where td_sell.user_id='1'
) s cross join
(select SUM(td_comission.price) as commission
from td_comission
where td_comission.user_id='1'
) c