How can I get max sum in table with mysql - php

I have an SQL database with a table like this:
ObjID Rating
123 1
333 0
123 0
123 1
567 1
An Ojbect ID can show up in the table multiple times with different ratings, I need a way to select a "distinct" ObjID check all of its ratings and compare that to all other "distinct" ObjIDs and compare those ratings. In the end I should have the 1st - 3rd ObjIDs with the over all highest ratings, in this example 123, 567 and 333 would be printed out.

So the rating of each object is the total sum of all ratings for that object in the table?
SELECT ObjID, SUM(Rating) AS TotalRating
FROM ObjectTable
GROUP BY ObjID
ORDER BY SUM(Rating) DESC
LIMIT 3

Related

how to retrieve the type of value which is there more number of times in a column in a table and count of it?

s.no Name type of vehicle allotted
1 ram bus
2 krish car
3 raj bus
4 albert car
5 johnson bike
6 raghu car
consider this is a table now i want to retrieve the type of vehicle which is there more number of times in the table and count of it. is there any particular method(query).
You have to use COUNT and GROUP BY.
Count will give you the number of rows grouped by type field.
SELECT COUNT(id) as count, type FROM table_name GROUP BY type
The output should be like this:
count | type
2 | bus
3 | car
1 | Bike
If you need just the highest one, append to the query:
ORDER BY count DESC LIMIT 1
I think you have to count and select the top 1 row.
Query
SELECT t.* FROM(
SELECT `type`, COUNT(`type`) `count`
FROM `your_table_name`
GROUP BY `type`
)t
ORDER BY `count` DESC LIMIT 1;

Order by a columns count where another column has a static value

I have a table which stores user items, the two key columns which I would like to use in this query are user_id and item_id. The id field in the example is not needed but just added to show these aren't the only two columns in the table.
----------------------
id user_id item_id
----------------------
1 1 324
2 1 324
3 3 324
4 2 230
5 4 324
The query which I would like to construct should return the top 10 users who have the most items with a specific item id.
So for example if I wanted to run the query against the item ID 324 I should get the following result.
-------------------
user_id item_count
-------------------
1 2
3 1
4 1
2 0
try this
select user_id , count(*) as item_count from table
where item_id = 324 group by user_id order by item_count desc limit 10
limit 10 will show you the top 10 users and order by desc sort from high to low.
However, the above query will not give you the 0 count as per your question. If you really want the zero count you can try this: (assuming your table name is userlist)
SELECT distinct user_id,
(select
count(*) from `userlist`
where user_id=u.user_id and item_id=324
) as item_count FROM `userlist` u
order by item_count desc
I couldn't create the database in my local, but I think this will do the trick
SELECT user_id, COUNT(item_id) as item_count
FROM TABLE_NAME
WHERE item_id = 324
GROUP BY item_id
ORDER BY item_count;

How to get SUM of a column with number of affected rows

How to get SUM of a column with number of affected rows
I am trying to get the sum of a column and I will also like to return the number of affected columns.
Example:
orders table
id customer_id name amount
----------------------------------------
1 2 burger 5.00
2 2 pizza 6.00
3 2 grape 1.00
4 1 sandwich 4.00
Now I want to SUM the amount column for a particular customer(customer_id) and also return number of items(count rows affected)
I am doing this but it only gets the sum of the amount, I would also like to get number number affected rows (count) from this single query:
SELECT SUM(amount) AS amount
FROM orders
WHERE customer_id = 2
Just do a COUNT as well:
SELECT SUM(amount) AS amount, COUNT(*) AS cnt
FROM orders
WHERE customer_id = 2
If amount is a nullable field and you want to only count NOT NULL rows, then do a COUNT(amount) instead of COUNT(*).
SELECT SUM(amount) AS amount, COUNT(1) AS cnt
FROM orders
WHERE customer_id = 2

PHP&SQL select highest score

I'm going to select the highest result in my "quiz" table there is 5 type of data:
quizid, userid, quizdate, result, topicid
1 JAKE 1/7/2015 60 1
2 JAKE 1/7/2015 80 1
3 JAKE 1/7/2015 100 2
i wanna show each topic and user highest score only once.
But it only show the first highest score from the user, after user redo the quiz again it wont show the highest score.
Example: Userid JAKE has done first time quiz have 60 mark at quiz 1, when he redo quiz 1 and get 80 marks it still show 60mark at the Table.
Select userid,topicid, MAX(result) as result
FROM quiz GROUP BY userid, topicid
ORDER BY result desc
The final result should be show 80 for JAKE at topic 1, but my result was
1 JAKE 1/7/2015 60 1
3 JAKE 1/7/2015 100 2
Hey you should used it like. if you want complete record with max result
select * from (Select userid,topicid, result as result
FROM quiz ORDER BY result desc) as t GROUP BY t.userid, t.topicid
Try following,
Select userid, topicid, result from
(
Select
row_number() over (order by userid, topicid, result desc) row_id,
userid,
topicid,
result
FROM quiz
) t
where row_id = 1

MySQL: select last (max) value from column and count of all rows

How can I select last (=max) value from column and count of all rows in single query?
ID ITEM_ID VALUE
1 1 100
2 1 101
3 2 201
4 3 333
5 2 222
6 1 111
I want to select last / max value for particular ITEM_ID and count of all rows with this ID.
For ITEM_ID = 1 thus:
VALUE COUNT
111 3
My query is like this:
SELECT (SELECT COUNT(*) FROM table) AS count, (SELECT value FROM table ORDER BY id DESC LIMIT 1) AS value FROM table WHERE item_id = 1 LIMIT 1
It works but looks ... weird. Is there any better (simpler / faster) solution? Thanks
You need to do a GROUP BY on column ITEM_ID while getting the MAX() and COUNT() like
select max(value) as `VALUE`,
count(*) as `COUNT`
from your_table
group by ITEM_ID;

Categories