Hey guys I'm trying to create a query which sums one column and groups the result by another.
Let me show you what I mean by showing my tables!
This is my tickets table
Expect result
As you can see it groups the column app and issue and then SUMS the time per issue.
Currently I have something like this
SELECT app,issue,sum(time)
FROM tickets
GROUP BY issues
ORDER BY name ASC;
However it's not outputting the expected result. Any help would be appreciated.
You need to also group by app in addition to issues
SELECT app,issue,sum(time)
FROM tickets
GROUP BY app,issue
ORDER BY name ASC;
You only grouped by the issues, don't forget adding the app to the group by also.
This should work for you:
SELECT app,
issue,
Sum(time)
FROM tickets
GROUP BY app,
issues
ORDER BY name ASC;
Related
I have been searching for an answer to the following but with no success as yet. I am trying to count the number of times a level is achieved by a user within a particular department.
After several attempts I've managed to get results from the query below, however the results are not between the timestamp values required, rather they show the total count for LEVEL. Timestamp is the name of the column in the table, could that be an issue?
SELECT COUNT(LEVEL) AS number, LEVEL, user, department FROM table
WHERE LEVEL ='3' AND TIMESTAMP>= '1500098552' AND TIMESTAMP<= '1568000152'
GROUP BY user ORDER BY number DESC
I have tried using various methods for the WHERE clause including BETWEEN but to no avail.
Any advice would be appreciated, thanks.
Try this one
SELECT COUNT(LEVEL) AS number, LEVEL, user, department FROM table
WHERE LEVEL ='3' AND TIMESTAMP>= 1500098552 AND TIMESTAMP<= 1568000152
GROUP BY user,department,LEVEL ORDER BY number DESC
I'm trying to list multiple columns but specify a condition for only one.. e.g
SELECT max(dollars_spent), customer as most_visits FROM example
I want the customer to display IF they have the most dollars spent but don't know how to specify I want that condition for that specific column only.. I'm tired and am struggling to process this right now. Hoping someone out there will understand what I mean.
This may work...
select sum(dollars_spent), customer as most_visits from example group by customer order by sum(dollars_spent) desc limit 1
I have the following table that stores messages between users:
I need to display a list of all last message for a users (from all users that have had contact with).
You will see to users being 1000000002 & 1000000172 for example. I need to show the last message between them which could be rows 1 to 4 - but would be 4 as last time.
I have tried the query below but its still isn't right:
SELECT sender_userid,receiver_userid,message,message_read,`datetime` FROM messages
WHERE (receiver_userid='1000000172' OR sender_userid='1000000172') AND friendship_status=1 AND receiver_history=1
GROUP BY receiver_userid
ORDER BY `datetime` ASC;
I find the order by doesn't get the most resent - could be because its after the Group By.
Also find it treats the sender_userid & receiver_userid as different rows in the Group By. I'm unsure how to get the most resent out of both.
thankyou so very much
Your GROUP BY should throw you errors, but MySQL just gives you garbage instead :)
You do not even need a group by, you simply want a list of all messages in which user X is involved and you want to sort them, so you have most of the work domn allready:
SELECT *
FROM messages
WHERE (receiver_userid='1000000172' OR sender_userid='1000000172')
AND friendship_status=1 AND receiver_history=1
ORDER BY `datetime` DESC;
Mind that you want to sort descending if you want the most recent ones first!
use DESC instead of ASC. DESC means large one is first.
I've designed a simple website which enables users to search on a MYSQL database. The search method is also simple: the user types a word in a textfield and it searches in the database.
Now i want to include in this website a table with the most searched words and i didn't find anything until now but this sentence:
select distinct column
, count(1) as total
from dept
group by column
order by total desc
limit 5
but this doesnt retrieve what i want. Do you have any idea of how I get this result?
Thank you in advance!
A simple example for a small site:
After each search, add a row to a table searched. Bonus points for adding a timestamp.
insert into searched (keyword, timestamp) values ('foo', 1234567890);
From there:
select keyword, count(*) as total from searched
group by keyword order by total desc limit 5;
Of course, for simple things like this, I'd use redis.
I am currently building a simple PHP + MySQL forum but I am having problems with getting the information to show in the correct format.
My current SQL code is
SELECT forum_posts.catId, forum_posts.postId, forum_posts.date, forum_posts.message,
forum_posts.userId, users.userId, users.username, forum_thread.threadId, forum_thread.subjectTitle
FROM forum_posts
LEFT JOIN forum_thread ON forum_posts.threadId = forum_thread.threadId
LEFT JOIN users ON users.userId = forum_posts.userId
GROUP BY forum_posts.catId
ORDER BY forum_posts.postId DESC, forum_posts.date DESC, forum_posts.catId ASC
The problem I have is that it brings back everything in the right category but it brings back the first result of the category and not the last one.
I simply want the code to show the last reply in each category.
Any help is much appreciated thank you.
Your query should return a range of rows. Try to limit the result to 1 element. If you sort the results descending, you will get the last item.
ORDER BY ... DESC LIMIT 1
I am not sure whether you find the latest entry by postId or date. If you find it by date, you must start the grouping with the date.
But I don't understand why you are sorting the results so much for getting only one dataset.
ORDER BY forum_posts.date DESC LIMIT 1;
Is this what you want? Additionally this could help you: Select last row in MySQL.