How to select the highest sum of value in mysql - mysql

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

Related

MySQL Relative ranking of values in one column for all occurrences of a given key

I'm looking for some basic direction on how where to start looking to try and rank rows of a common key in a query.
Imagine I have a table like this:
user_id | account_id | score
1 A 10
1 B 20
2 C 10
2 D 20
2 E 30
What I'm hoping to do is add a rank column for relative to each user_id where the highest score gets the top rank:
user_id | account_id | score | rank
1 A 10 2
1 B 20 1
2 C 10 3
2 D 20 2
2 E 30 1
Just looking for some basic direction in terms of which way to head :/
You can use subquery
select
*,
(select count(1)+1 from your_table b where a.user_id=b.user_id and a.score<b.score) as rank
from your_table a
Output
1 A 10 2
1 B 20 1
2 C 10 3
2 D 20 2
2 E 30 1

sql SELECT count all points from different entries limit by 10 per ID

i have a table that looks like this:
ID GameID DateID Points Place
-------------------------------------
10 1 1 100 1
11 1 1 90 2
12 1 1 80 3
13 1 1 70 4
14 1 1 60 5
10 1 1 100 1
10 1 1 50 1
10 1 1 100 1
10 1 1 100 1
10 1 1 100 1
10 1 1 100 1
10 1 1 100 1
10 1 1 100 1
10 1 1 100 1
10 1 1 50 5
10 1 1 50 5
12 1 1 100 1
-------------------------------------
I want a table with two columns, one for the total points (summated scores/points) of one player and one for the id of the player. But for one player only ten scores may be counted, so for example if one player played thirteen times, only the ten highest scores are counted.
For the example above I want a table that looks like this:
ID totalPoints
-------------------
10 950
11 90
12 180
13 70
14 60
------------------
At the moment I tried this:
SELECT ID,
sum(Points) AS totalPoints
FROM (SELECT Points, ID
FROM Gamer
ORDER BY Points DESC LIMIT 10) AS totalPoints
ORDER BY Points DESC
but it limits the entries at all to ten and not to ten per player.
I hope anybody can help me :)
In all existing versions:
DELIMITER $
CREATE FUNCTION `totalPoints`(gamer_id INT) RETURNS int(11)
BEGIN
DECLARE s INT DEFAULT 0;
SELECT SUM(Points) INTO s FROM ( SELECT Points FROM Gamer WHERE ID=gamer_id ORDER BY Points DESC LIMIT 10) sq;
RETURN s;
END$
DELIMITER ;
SELECT DISTINCT ID, totalPoints(ID) FROM Gamer;
Alternative in MariaDB 10.2 (currently Beta), which has window functions:
SELECT ID, SUM(Points) FROM (
SELECT ID, Points, ROW_NUMBER()
OVER (PARTITION BY ID ORDER BY Points DESC) AS nm
FROM Gamer
) sq WHERE nm <= 10 GROUP BY ID;
I'm pretty sure there are other ways to do the same, these two are first that came to mind.

mySQL how to count this item id

I have a table
Table item kit
item_kit_id item_id
=======================
1 1
1 1
1 2
2 2
2 237
how to display result
item_kit_id item_id count
===============================
1 1 2
1 2 1
2 2 1
2 237 1
Thanks
You should use group by with both column its return your expected result
SELECT item_kit_id,item_id,count(*)
FROM kit
GROUP BY item_kit_id,item_id;
For more know about GROUP BY
you can use below query-
select item_kit_id, item_id, count(item_id) as count
from item_kit
group by item_kit_id, item_id;

My sql query sum of highest points for each category

upload table
id category_id
1 1
2 2
3 3
4 1
In Ratings Table
id upload_id points
1 1 5
2 2 3
3 3 2
4 4 2
5 1 5
I want to display all the records from ratings table except id 4 because id 4 is the same category id 1
I excepted result
Uploaded_id 1 has sum of 10 points
Uploaded_id 2 has sum of 3 points
Uploaded_id 3 has sum of 2 points
Please help me.
Thanks In advance
Sasikumar
select upload_id, sum(points)
from rating r,
(select min(id) id from upload group by category_id) a
where a.id = r.upload_id group by upload_id

mysql select most recent, limit by source

I have a table with the following fields:
id
source_id
title
date
I want to select the 25 most recent items, so SELECT * FROM table ORDER BY date DESC LIMIT 50
The extra requirement is to select only the 3 most recent from every source_id.
So if the records look something like that,
id | source_id | title | date
----+-----------+-------+---------
1 2 aaa 2012-1-1
2 2 aaa 2012-1-2
3 2 aaa 2012-1-3
4 2 aaa 2012-1-4
5 3 aaa 2012-1-5
6 4 aaa 2012-1-6
I want my query to return items 4,3,2,5,6
So just the 3 most recent of every source with an over all limit of 25.
I'm not sure it's clear enough so please ask if you need more details.
Here you go:
SELECT *
FROM your_table t1
WHERE
(
SELECT COUNT(*)
FROM your_table t2
WHERE
t1.source_id = t2.source_id
AND t1.date < t2.date
) < 3
ORDER BY source_id, date DESC
Result:
4 2 aaa 2012-01-04
3 2 aaa 2012-01-03
2 2 aaa 2012-01-02
5 3 aaa 2012-01-05
6 4 aaa 2012-01-06
In plain English: take only rows that have less than 3 newer rows with the same source_id.
NOTE: This could select more than 3 rows per source_id if the third newest date (for the same source_id) happens to be shared by more than one row. Let me know what "3 newest" means in this context if that's a problem...
select * from table where source_id in
(select distinct source_id from table order by date limit 3)
LIMIT 25