I am using user Keevas' example, but asking a different question....
select Master_Code, SUM(Jan), SUM(Feb), SUM(Mar)
from dbo.foobar
WHERE Participating_City = 'foofoo'
GROUP BY Master_Code ORDER BY Master_Code ASC
something like this:
Master_Code sum(Jan) Sum(Feb) sum(Mar) Total
1 4 5 4 13
2 5 5 5 15
How do I get Total value column?
SELECT username,SUM(value) AS SumValue FROM
table GROUP BY username ORDER BY SumValue DESC
Related
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;
I have a table "student_points".
id user_id points subject_id
1 10 45 22
2 11 75 23
3 12 78 24
4 10 13 23
5 12 65 23
and so on ...
This table contain about 1000 records of users and points.
I want 10 records based on points (Max points first)
So I can use mysql query as
Select * from student_points order by points limit 0,10
Now the requirement is that we need to group these 10 records based on user_id
For example in first 10 records three are 3 students records so they should display in group.
End result should be like
id user_id points subject_id
3 12 78 24
5 12 65 23
1 10 45 22
4 10 13 23
2 11 75 23
You can see that first record is based on most point and it student id is 12, now they are group according to user_id.
I tried two order by .
I also tried to array_multisort after getting result but both are not working properly.
Please suggest any way, Either mysql query or group after getting result.
Thanks
To get your required result I have written the query :
SELECT * FROM (
Select * from student_points order by points limit 0,10
) As st
GROUP BY user_id,subject_id
ORDER BY points DESC
Please try this. Let me know if this is not work for you.
This should work just add a limit to whatever number you want to limit by
select sp.id, sp.user_id, sp.points
from student_points sp
join (select user_id, max(points) as sort_by from student_points group by user_id) sort_table on sp.user_id = sort_table.user_id
order by sort_table.sort_by desc, sp.user_id, sp.points desc;
I guess you need to wrap up your query in a subquery and later sort the results based on user_id and points.
SELECT * FROM
(
Select * from student_points order by points limit 0,10
) AS t
ORDER BY
t.user_id DESC,
t.points DESC
I have one table named:
thread_comment
Now the table is getting filled as followed:
thread_comment_id thread_id user_id thread_comment thread_comment_time
1 2 1 This is a comment 2016-09-14 15:30:28
2 4 1 This is a comment 2016-09-14 15:32:28
3 2 1 This is a comment 2016-09-14 15:33:28
4 5 1 This is a comment 2016-09-14 15:34:28
5 7 1 This is a comment 2016-09-14 15:35:28
6 2 1 This is a comment 2016-09-14 15:37:28
7 2 1 This is a comment 2016-09-14 15:40:28
I want to show the newest threads to my page. for example:
as number one i want thread_comment_id 7
as number two i want thread_comment_id 5
I skipped 6 because i don't want any duplicates in the list.
In order to do so i did the folowing:
$sql = "SELECT DISTINCT thread_id FROM thread_comment ORDER BY thread_comment_time DESC"
This is kind of working (Not showing any duplicates). However the order does not make any sense...
For example:
It goes like 5 - 6 -4 - 7 etc...
The column used in the ORDER BY isn't specified in the DISTINCT. You need to use an aggregate function and GROUP BY to make the DISTINCT work.
SELECT DISTINCT thread_id, max(thread_comment_id) FROM thread_comment GROUP BY thread_id ORDER BY max(thread_comment_id) DESC, thread_id
EDIT: added aggregate func max()
Also thread_id is not mandatory in the ORDER BY
It I understand correctly, you want one row per thread. Here is a way to do this:
select tc.*
from thread_comment tc
where tc.thread_date = (select max(tc2.thread_date)
from thread_comment tc2
where tc2.thread_id = tc.thread_id
);
What I want to do is to select a value of the database,
Lets say:
id ---- giftid ---- userid
1 1 481
2 1 422
3 7 123
4 9 542
5 1 122
6 1 455
For example, there are 4 users that want to have the same giftid:
1, 2, 5, 6
It means that each one will have 25% to be chosen.
How can I make the "percent selection"?
Assuming every userid can only claim a giftid once, you can use the ORDER BY RAND() in MySQL. This will firstly select all the rows from table table where the giftid is 1 and then the results are ordered randomly. The LIMIT 1 ensures that only the first record is returned
SELECT * FROM table
WHERE giftid = `1`
ORDER BY RAND()
LIMIT 1
Are you looking this?
SELECT giftid, 1.0 / COUNT(*) percentSelection
FROM tableName
GROUP BY giftid
I have the following table:
step_id hospital_id step_number step_name
17 8 4 First Step
18 8 1 Second Step
19 8 2 Third Step
20 8 3 Finance Approval
What I am trying to do is get the step_id that corresponds to the smallest step number. So in the example above I am looking for step_id 18.
After looking over many posts I have been trying variations of the following to no avail:
SELECT `step_id`
FROM `progress_steps`
WHERE `hospital_id` = 8
GROUP BY `step_id`
HAVING MIN(`step_number`)
It seems to have worked for other but the above returns all rows from the example and other variations I have tried give me step_id 17 only.
You can do that by using ORDER BY and LIMIT
SELECT *
FROM tableName
WHERE hospital_id = 8
ORDER BY step_number ASC
LIMIT 1
SQLFiddle Demo
or if you want to get multiple rows having the same lowest step_number, use subquery:
SELECT *
FROM tableName
WHERE step_Number =
(
SELECT step_Number
FROM tableName
WHERE hospital_id = 8
ORDER BY step_number ASC
LIMIT 1
)
SQLFiddle Demo
SQLFiddle Demo (with duplicate record)
Try this one
SELECT `step_id`
FROM `progress_steps`
WHERE `hospital_id` = 8
Order BY `step_id`
LIMIT 1