I have a table with this structure
| id | name | score | time |
----------------------------
| 1 | Mike | 120 | 35 |
| 2 | Rose | 67 | 20 |
| 3 | John | 120 | 20 |
| 4 | Kate | 130 | 50 |
How can I select the person who scored the highest in the least time?
Time is in seconds.
Sort by highest score then by lowest time, select 1st row:
SELECT * FROM table ORDER BY score DESC, `time` ASC LIMIT 0, 1
Related
I have a table named sellingDetails.
Table Data is as follows
+--------+----------------+
| id | sellingPrice |
+--------+----------------+
| 1 | 35000 |
| 2 | 40000 |
| 3 | 30000 |
| 4 | 25000 |
| 5 | 35000 |
| 6 | 33000 |
| 7 | 25000 |
+--------+----------------+
When I query
SELECT id, sellingPrice
FROM sellingDetails
ORDER BY id DESC limit 0, 4
The above query outputs:
+--------+----------------+
| id | sellingPrice |
+--------+----------------+
| 7 | 25000 |
| 6 | 33000 |
| 5 | 35000 |
| 4 | 25000 |
+--------+----------------+
Out of the above result how I can get the MAX result i.e. 35000.
I tried using MAX(sellingprice) but it didn't work.
this question is very near to mine but it has joins whereas mine is a single table
DB Fiddle
If I use the below query
SELECT MAX(sellingprice) FROM sellingdetails
ORDER BY id DESC
LIMIT 0, 4;
I get output as 40000 which is not intended.
You can use your existing query as a Derived Table and then determine the maximum value from it.
SELECT MAX(dt.sellingPrice)
FROM
(
SELECT sellingPrice
FROM sellingDetails
ORDER BY id DESC limit 0, 4
) dt
Result
| MAX(dt.sellingPrice) |
| -------------------- |
| 35000 |
View on DB Fiddle
I am currently trying to bring two tables together. An employee table and a department table. Both share an ID and I Have managed to join them together successfully using the following code.
SELECT department.dept_id, employee.dept_id, employee.salary
From department
INNER Join employee ON department.dept_id=employee.dept_id
ORDER BY employee.dept_id ASC
This produces a table like...
| 10 | 10 | 12000 |
| 20 | 20 | 5000 |
| 20 | 20 | 7500 |
| 20 | 20 | 15000 |
| 20 | 20 | 35000 |
| 30 | 30 | 15000 |
| 30 | 30 | 25000 |
| 40 | 40 | 25000 |
| 40 | 40 | 5000 |
How would i go about producing it so that it could look like...
10 | 1 | 12000
20 | 4 | 62500
30 | 2 | 40000
40 | 2 | 30000
where the second column is a count of the amount of times they appear?
Try this:
SELECT
department.dept_id
,COUNT(department.dept_id)
,SUM(employee.salary)
From department
INNER Join employee ON department.dept_id=employee.dept_id
GROUP BY department.dept_id
ORDER BY department.dept_id ASC
I found this question which is very similar but I'm still having some troubles.
So I start with table named Scores
id | player | time | scoreA | scoreB |
~~~|~~~~~~~~|~~~~~~|~~~~~~~~|~~~~~~~~|
1 | John | 10 | 70 | 80 |
2 | Bob | 22 | 75 | 85 |
3 | John | 52 | 55 | 75 |
4 | Ted | 39 | 60 | 90 |
5 | John | 35 | 90 | 90 |
6 | Bob | 27 | 65 | 85 |
7 | John | 33 | 60 | 80 |
I would like to select the best average score for each player along with the information from that record. To clarify, best average score would be the highest value for (scoreA + scoreB)/2.
The results would look like this
id | player | time | scoreA | scoreB | avg_score |
~~~|~~~~~~~~|~~~~~~|~~~~~~~~|~~~~~~~~|~~~~~~~~~~~|
5 | John | 35 | 90 | 90 | 90 |
2 | Bob | 22 | 75 | 85 | 80 |
4 | Ted | 39 | 60 | 90 | 75 |
Based on the question I linked to above, I tried a query like this,
SELECT
s.*,
avg_score
FROM
Scores AS s
INNER JOIN (
SELECT
MAX((scoreA + scoreB)/2) AS avg_score,
player,
id
FROM
Scores
GROUP BY
player
) AS avg_s ON s.id = avg_s.id
ORDER BY
avg_score DESC,
s.time ASC
What this actually gives me is,
id | player | time | scoreA | scoreB | avg_score |
~~~|~~~~~~~~|~~~~~~|~~~~~~~~|~~~~~~~~|~~~~~~~~~~~|
1 | John | 10 | 70 | 80 | 90 |
2 | Bob | 22 | 75 | 85 | 80 |
4 | Ted | 39 | 60 | 90 | 75 |
As you can see, it has gotten the correct max avg_score, from record 5, but gets the rest of the information from another record, record 1. What am I missing? How do I ensure that the data all comes from the same record? I'm getting the correct avg_score but I want the rest of the data associated with that record, record 5 in this case.
Thanks in advance!
SELECT x.*
, (scoreA+scoreB)/2 avg_score
FROM scores x
JOIN
( SELECT player, MAX((scoreA+scoreB)/2) max_avg_score FROM scores GROUP BY player) y
ON y.player = x.player
AND y.max_avg_score = (scoreA+x.scoreB)/2;
Try
SELECT s.*,
q.avg_score
FROM scores s JOIN
(
SELECT player,
MAX((scoreA + scoreB)/2) AS avg_score
FROM scores
GROUP BY player
) q ON s.player = q.player
AND (s.scoreA + s.scoreB)/2 = q.avg_score
ORDER BY q.avg_score DESC, s.time ASC
Sample output:
| ID | PLAYER | TIME | SCOREA | SCOREB | AVG_SCORE |
----------------------------------------------------
| 5 | John | 35 | 90 | 90 | 90 |
| 2 | Bob | 22 | 75 | 85 | 80 |
| 4 | Ted | 39 | 60 | 90 | 75 |
Here is SQLFiddle demo
I have a table like this
id | invent_id | order
1 | 95948214 | 70
2 | 46018572 | 30
3 | 46018572 | 20
4 | 46018572 | 50
5 | 36025764 | 60
6 | 36025764 | 70
7 | 95948214 | 80
8 | 95948214 | 90
I want get the sum of order qty with same invent id
That is the want the result like this
| invent_id | order
| 95948214 | 240
| 46018572 | 100
| 36025764 | 130
how can we write the mysql query
Make use of Aggregate function SUM and grouped them according to invent_id.
SELECT invent_id, SUM(`order`) `Order`
FROM tableName
GROUP BY invent_ID
GROUP BY clause
SQLFiddle Demo
I have the following table:
+-----+-----------+----------+------------+------+
| key | idStudent | idCourse | hourCourse | mark |
+-----+-----------+----------+------------+------+
| 0 | 1 | 1 | 10 | 78 |
| 1 | 1 | 2 | 20 | 60 |
| 2 | 1 | 4 | 10 | 45 |
| 3 | 3 | 1 | 10 | 90 |
| 4 | 3 | 2 | 20 | 70 |
+-----+-----------+----------+------------+------+
Using a simple query, I can show student with their weighted average according to hourCourse and mark:
SELECT idStudent,
SUM( hourCourse * mark ) / SUM( hourCourse ) AS WeightedAvg
FROM `test`.`test`
GROUP BY idStudent;
+-----------+-------------+
| idStudent | WeightedAvg |
+-----------+-------------+
| 1 | 60.7500 |
| 3 | 76.6667 |
+-----------+-------------+
But now I need to select the registers until the cumulative sum of hourCourse per student reaches a threshold. For example, for a threshold of 30 hourCourse, only the following registers should be taken into account:
+-----+-----------+----------+------------+------+
| key | idStudent | idCourse | hourCourse | mark |
+-----+-----------+----------+------------+------+
| 0 | 1 | 1 | 10 | 78 |
| 1 | 1 | 2 | 20 | 60 |
| 3 | 3 | 1 | 10 | 90 |
| 4 | 3 | 2 | 20 | 70 |
+-----+-----------+----------+------------+------+
key 2 is not taken into account, because idStudent 1 already reached 30 hourCourse with idCourse 1 and 2.
Finally, the query solution should be the following:
+-----------+-------------+
| idStudent | WeightedAvg |
+-----------+-------------+
| 1 | 66.0000 |
| 3 | 76.6667 |
+-----------+-------------+
Is there any way to create an inline query for this? Thanks in advance.
Edit: The criteria while selecting the courses is from highest to the lowest mark.
Edit: Registers are included while the cumulative sum of hourCourse is less than 30. For instance, two registers of 20 hours each would be included (sum 40), and the following not.
You can calculate the cumulative sums per idStudent in a sub-query, then only select the results where the cumulative sum is <= 30:
select idStudent,
SUM( hourCourse * mark ) / SUM( hourCourse ) AS WeightedAvg
from
(
SELECT t.*,
case when #idStudent<>t.idStudent
then #cumSum:=hourCourse
else #cumSum:=#cumSum+hourCourse
end as cumSum,
#idStudent:=t.idStudent
FROM `test` t,
(select #idStudent:=0,#cumSum:=0) r
order by idStudent, `key`
) t
where t.cumSum <= 30
group by idStudent;
Demo: http://www.sqlfiddle.com/#!2/f5d07/23