Here is the table data
Name Stage ID Event ID Attempt No Score
Ramesh 1 1 1 10
Ramesh 1 1 2 20
Ramesh 2 1 1 30
Suresh 1 1 1 05
Suresh 2 1 1 15
Suresh 2 2 1 25
Suresh 2 2 2 35
Suresh 2 2 3 30
We have a Table with Name, Stage ID, Event ID, Attempt No, Score.
We want to group the data for Name, Stage ID, Event ID, Attempt No, Max(Score)
There can be records for every attempt under stage and Event and name and we need to take the max score and the same needs to be displayed.
The output should be :
Name Stage ID Event ID Attempt No Score
Ramesh 1 1 2 20
Ramesh 2 1 1 30
Suresh 1 1 1 05
Suresh 2 1 1 15
Suresh 2 2 2 35
This would have one record for the combination of name + stage + Event
You can try below using correlated subquery
select * from tablename t1
where score in
(select max(score) from tablename t2 where t1.name=t2.name and t1.stage=t2.stage
and t1.eventid=t2.eventid)
You could use a inner join on the subquery for the max value you need
select yt.*
from your_table yt
INNER JOIN (
select Name, Stage_ID, Event_ID, Attempt_No, max(score) max_score
from your_table
group by Name, Stage_ID, Event_ID, Attempt_No
) t on yt.name = t.name
and yt.Stage_ID = t.Stage_ID
and yt.Event_ID = t.Event_ID
and yt.Attempt_No = t.Attempt_No
and yt.score = t.max_score join
Related
I have a table to store id, sid with a date time.
id is used as primary key and no meaning in data.
sid is used to identify entity.
eg.
id sid date
--------------------
1 1 2020-01-12
2 2 2020-01-01
3 1 2019-12-31
4 2 2019-12-31
5 1 2019-12-31
6 1 2019-11-01
7 3 2019-11-01
8 3 2018-12-21
9 2 2018-12-21
Then I would like to query for each record, count occurrences in the same table with the previous date of current date, and with the same sid, like:
id sid date previous_count
----------------------------------
1 1 2020-01-12 2
2 2 2020-01-01 1
3 1 2019-12-31 1
4 2 2019-12-31 1
5 1 2019-12-31 1
6 1 2019-11-01 0
7 3 2019-11-01 1
8 3 2018-12-21 0
9 2 2018-12-21 0
Explanation:
for row 1, since sid 1 has two records in 2019-12-31, which is the previous date of 2020-01-12 for sid 1 in the table, it has 2 in previous_count;
while in row 2, since sid 2 has only 1 record in 2019-12-31, which is the previous date of 2020-01-01 for sid 2, it has 1 in previous_count.
Thanks
Your are looking for dense_rank() - 1:
select t.*,
(dense_rank() over (partition by sid order by date) - 1) as previous_count
from t
order by id;
In older versions of MySQL, you could use variables or a correlated subquery:
select t.*,
(select count(distinct t2.date)
from t t2
where t2.sid = t.sid and t2.date < t.date
) as previous_count
from t
order by id;
EDIT:
Ahh, I think I may have misunderstood the problem. I think this does what you want:
select t.*, lag(cnt, 1, 0) over (partition by sid order by date)
from (select t.*,
count(*) over (partition by sid, date) as cnt
from t
) t
order by id;
Here is a db<>fiddle.
I am performing union on two select statements but the result I am getting contains the random rows like some from table one and some from table two.But I want to get first all the rows from table one and then from table two.I am using MySql database.
Table 1
==========================
s_no Name Marks
1 nikhil 25
Table 2
====================
s_no Name Marks
1 Akhil 10
2 Mark 20
1 Kim 40
Here is the query that I am using:
select * from (
select t2.s_no,t2.name,t2.marks from table1 t2
union all
select t1.roll_no,t1.name,t1.marks from table1 t1
) a order by s_no desc
Here are the results:
Actual result
=========================
s_no Name Marks
1 Akhil 10
1 nikhil 25 <<<
1 kim 40
2 mark 20
required result
====================
s_no Name marks
1 Akhil 10
1 Kim 40
1 nikhil 25 <<<
2 mark 20
Try this. You need to add order by s_no,name in the last select query
(select * from table1)
union
(select * from table2) order by s_no,name
I have the table like this
id rate_user_id content_id points category_id
1 100 1 5 1
2 101 1 3 1
3 100 2 8 1
4 103 2 11 1
So I want to looking highest point of content in this category 1
Content_id 2 = 19 points .
U can do this by
select content_id,sum(points) from user group by content_id order by sum(points) desc limit 1;
Thanks
have a look at the command MAX.
http://technet.microsoft.com/en-us/library/ms187751.aspx
Something like
SELECT MAX(Pts) FROM
(SELECT SUM(points) Pts GROUP BY content_id) A
This is probably a easy one, but for the life of me I can't seem to figure it out.
This is my table:
uid | userID | trackID | carID | highscoreDate | highscore
-----------------------------------------------------------------
1 1 1 1 [date] 123
2 1 1 1 [date] 44
3 2 2 1 [date] 222
4 2 1 1 [date] 28
5 1 2 1 [date] 17
I would like to get the SUM of the highest highscores for each user and track. In the data above that would give:
user 1: 140
user 2: 250
How about using a subselect first.
Something like
SELECT userID, SUM(highscore)
FROM (
SELECT userID, trackID, MAX(highscore) highscore
FROM MyTable
GROUP BY userID, trackID
) s
GROUP BY userID
SQL Fiddle DEMO
I can't figure out the mysql query to extract the data I want from this table "timeEntry":
hours creationDate userId clientId projectId taskId
20 2012-02-18 1 1 1 1
40 2012-02-18 1 1 1 1
30 2012-02-21 2 1 1 1
20 2012-02-22 2 1 1 2
30 2012-02-22 2 1 1 2
80 2012-02-23 1 2 2 2
10 2012-02-23 3 2 2 2
15 2012-02-23 1 2 2 3
40 2012-02-23 1 2 4 1
And I would like to have this kind of result as another table, or csv/excel file or php array (where totalHours is the sum of the hours for a userId) for a given period of time, let say (between 2012-02-01 and 2012-02-25):
clientId projectId taskId userId totalHours
1 1 1 1 60
2 30
2 2 50
2 2 2 1 80
3 10
3 1 15
4 1 1 40
I guess I have to use multiple group by, I tried something like:
SELECT clientId, projectId, taskId, userId, sum(hours)
FROM `timeEntry`
WHERE date_creation >= "2012-02-01"
AND date_creation <= "2012-02-25"
GROUP BY clientId, projectId, taskId, userId;
But didn't work...
Thanks in advance.
Where needs to go before Group by.
If you want to filter by date before grouping, use the where clause you have but moved before the group by.
If you'd instead like to filter entire groups in or out, use a having:
...
Group by ...
Having max(date) <= someValue and min(date) >= someValue
SELECT clientId, projectId, taskId, userId sum(Hours) total_hours
FROM timeEntry
GROUP BY clientID, ProjectID, TaskID, userID;
You can use [with Rollup][1] to generate sub-aggregrates. if you want
Here is a simple solution. You need to group by all necessary fields
SELECT
t.clientId,
t.projectId,
t.taskId,
t.userId,
SUM(t.hours) AS Total
FROM test AS t
GROUP BY t.creationDate , t.userId , t.clientId , t.projectId , t.taskId
Fiddle Demo
OUTPUT:
clientId projectId taskId userId Total
____________________________________________________________
1 1 1 1 60
1 1 1 2 30
1 1 2 2 50
2 2 2 1 80
2 2 3 1 15
2 4 1 1 40
2 2 2 3 10