MYSQL query with values of the same table - mysql

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)

Related

Calculation using 2 tables in 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

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

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 |

Selecting top x from the sum of two columns

I have a table which is formed like this
SELECT * FROM test WHERE GREATEST(number, initial)
| id | username | number | initial |
I need to add the both top number and initial to retrieve the top.
Use this as an example
| id | username | number | initial | total
| 1 | a | 665 | 441 | 1106
| 2 | b | 918 | 99 | 1017
| 3 | c | 611 | 336 | 947
| 4 | d | 491 | 968 | 1459
| 5 | e | 414 | 129 | 543
What I need is to retrieve the highest number first and the lowest number last.
I have tried SELECT * FROMtestWHERE GREATEST(number, initial) but that doesn't seem to do the trick.
I'm not so experienced with SQL, but one thing I have tried is this
If you want to get the record with the highest value of number + initial:
select * from test
order by (number + initial) desc
limit 1
or, if you want all records that have the greatest value of number + initial:
select * from test
where test.number + test.initial =
(select max(test.number + test.initial)
from test)
or, if you want to order all records by the value of number + initial in descending order:
select * from test
order by (number + initial) desc

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;