Calculation using 2 tables in mysql - mysql

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

Related

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

Mysql query returning latest result for each element

Having a hard time wrapping my mind around what seems should be a simply query.
So let's say we have a table that keeps track of amount of widgets/balloons in each store by date. How would you get a list of stores and their latest widget/balloons count?
i.e.
mysql> SELECT * FROM inventory;
+----+------------+-------+---------+---------+
| id | invDate | store | widgets | balloons|
+----+------------+-------+---------+---------+
| 1 | 2011-01-01 | 3 | 50 | 35 |
| 2 | 2011-01-04 | 2 | 50 | 35 |
| 3 | 2013-07-04 | 3 | 12 | 78 |
| 4 | 2020-07-04 | 2 | 47 | 18 |
| 5 | 2020-08-06 | 2 | 16 | NULL |
+----+------------+-------+---------+---------+
5 rows in set (0.00 sec)
Would like the result table to list all stores and their latest inventory of widgets/baloons
store, latest widgets, latest balloons
+-------+-----------+---------+
| store | widgets | baloons |
+-------+-----------+---------+
| 2 | 16 | NULL |
| 3 | 12 | 78 |
+-------+-----------+---------+
or grab latest non NULL value for balloons.
This works for all versions of MySQL
select i.*
from inventory i
join
(
select store, max(invDate) as maxDate
from inventory
group by store
) tmp on tmp.store = i.store
and tmp.maxDate = i.invDate
With MySQL 8+ you can do window functions:
with cte as
(
select store, widgets, balloons,
ROW_NUMBER() OVER(PARTITION BY store ORDER BY invDate desc) AS rn
from inventory
)
select * from cte where rn = 1
You can use a correlated sub query to get latest record for each store
select i.*
from inventory i
where i.invDate = (
select max(invDate)
from inventory
where i.store = store
)
order by i.store
DEMO

How to find to "opposite" records on MySQL?

I have 1 table with the following cols:
giver_id | receiver_id
10 | 12
9 | 10
10 | 20
12 | 10
I am looking for a mysql query that will return 10-12 / 12-10 as a match.
Thanks
To identify the records for which an "opposite" record exist, you could do:
SELECT *
FROM mytable t
WHERE EXISTS (
SELECT 1
FROM mytable t1
WHERE t1.giver_id = t.receiver_id AND t.giver_id = t1.receiver_id
)
This demo on DB Fiddle with your sample data returns:
| giver_id | receiver_id |
| -------- | ----------- |
| 10 | 12 |
| 12 | 10 |

I want create query using sum and substarct function in single query using multiple conditions using mysql

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)

MYSQL query with values of the same table

I'm developing a PHP application for querying a MySQL DB.
My first query asks user to choice a value, that has a correspondence with the entry Sbj_ID in my table called 'Rec_SW2_Rel'. The value is correctly returned by the PHP function.
Now I have to query the table once again and perform following selection: imagine that the already chosen Sbj_ID is '9', I must return all the values of all those relations for which Rec_ID is equal and Position is = '2'.
Table 'Rec_SW2_Rel' looks like:
+ ---------------------------- +
* Rec_ID | Sbj_ID | Position | *
+ ---------------------------- +
* 10 | 9 | 1 | *
* 10 | 165 | 2 | *
* 10 | 23 | 3 | *
* 11 | 9 | 1 | *
* 11 | 15 | 2 | *
* 12 | 64 | 1 | *
* 12 | 8 | 2 | *
+ ---------------------------- +
Expected output should be:
10 | 165 | 2
11 | 15 | 2
select
*
from
your_table
where Position = 2
and Rec_ID in (select Rec_ID from your_table where Sbj_ID = 9)