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
Related
I have a high scores table that is slightly more complicated because scores are tracked in rounds (round 1, round 2, round 3, etc.). Sample table:
scoreID
roundID
userID
score
1
1
2
25
2
1
3
12
3
1
4
14
4
1
5
6
5
2
2
39
6
2
3
23
7
2
4
13
8
2
5
26
There can be many more rounds, and many more users.
I would like to pull the top 3 user scores from each round. My select statement at the moment looks like this:
select `scores`.`score`, `users`.`username`, `scores`.`roundID`
FROM `scores`
INNER JOIN `users` on `users`.`user_id` = `scores`.`userID`
ORDER BY `scores`.`score` DESC LIMIT 3;
However, this returns a result like so:
score
username
roundID
39
joey
2
26
bubba
2
25
george
1
when what I want is the top 3 scores per round:
score
username
roundID
25
george
1
14
bubba
1
12
joey
1
39
george
2
26
homey
2
23
joey
2
How do I select the top 3 scores in each round so my result mirrors the table immediately above?
You would do this like:
select score, username, roundID
from (
select
score, userID, roundID,
rank() over (partition by roundID order by score desc) score_rank
from score
) ranked_scores
inner join users on users.user_id = ranked_scores.userID
where score_rank <= 3
order by roundID, score, username
I have Table user_views
My Tablet Sample data
Id | product_id | category | user_id | sitename| price | click_count | created_date
1 10 watch 102 ebay 820 1 2014-08-18 13:56:05
2 10 watch 102 amazon 750 1 2014-08-19 13:56:05
3 10 watch 102 amazon 740 1 2014-08-19 18:00:05
4 10 watch 102 ebay 940 1 2014-08-25 08:00:00
5 10 watch 102 amazon 640 5 2014-08-25 08:10:10
6 10 watch 102 ebay 580 3 2014-09-25 18:10:10
7 10 watch 102 amazon 980 5 2014-10-05 12:20:40
I want the total count of the user visited this product
My Query
"select Id , proudct_id , category , user_id , count(click_count) as cnt from user_view where user_id =102 group by product_id order by rand() limit 0,10"
But the output is showing only one count
OUTPUT
Id | product_id | category | user_id | cnt
1 10 watch 102 1
EXPECTED OUTPUT IS
Id | product_id | category | user_id | cnt
1 10 watch 102 17
You should be using sum aggregate function instead of count which will count number of rows. So your query should be something like:
select Id , product_id , category , user_id , SUM(click_count) as sum ...
Try this:
select Id , proudct_id , category , user_id , SUM(click_count) as cnt
from user_view
where user_id =102
order by rand()
limit 0,10
group by product_id
COUNT() always returns the number of items, not the sum of values. So use SUM().
"select Id , proudct_id , category , user_id , count(click_count) as cnt from user_view where user_id =102 group by user_id, product_id order by rand() limit 0,10"
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
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
This is my table structure:
rec_id product_id quantity quantity_in quantity_out balance stock_date status
1 2 342 NULL 17 325 2009-10-23 1
2 2 325 NULL 124 201 2009-10-23 1
3 1 156 NULL 45 111 2009-10-23 1
4 2 201 NULL 200 1 2009-10-23 1
5 2 1 NULL 1 0 2009-10-23 1
6 1 111 NULL 35 76 2009-10-23 1
All I want is the last transaction done for a given product: product_id, quantity, quantity_out and balance from this table.
Example, there are 2 transaction done for product 2 (ids 1 & 2):
final balance for product_id 2 is 0 -> stored in rec_id 5
final balance for product_id 1 is 76 -> stored in rec_id 6
Final result/output should be like this:
recid productid quantity quantityin quantityout balance stock_date status
5 2 1 NULL 1 0 2009-10-23 1
6 1 111 NULL 35 76 2009-10-23 1
You can find the latest record for each product like:
select max(rec_id) as MaxRec
from YourTable
group by product_id
Using a subquery, you can retrieve the latest rows for their product:
select *
from YourTable
where rec_id in (
select max(rec_id) as MaxRec
from YourTable
group by product_id
)
Here's a single query with no subqueries:
SELECT main.*
FROM YourTable main
LEFT JOIN YourTable newer
ON newer.product_id = main.product_id AND newer.rec_id > main.rec_id
WHERE newer.rec_id IS NULL;
You can tweak the field list however you want--make sure you select fields from main, not newer, which should be all null.