I have been working in mySQL and I have been having issues with the COUNT function. I have already tried removing the spaces and the error persists.
SELECT COUNT(payment) FROM paymentType
GROUP BY payment
ORDER BY COUNT (payment) DESC
LIMIT 1;
I am trying to find the most common payment entry in the table paymentType.
If you want the most common payment, then I think you want:
SELECT payment
FROM paymentType
GROUP BY payment
ORDER BY COUNT(*) DESC
LIMIT 1;
this will work:
SELECT Count(payment),payment FROM paymentType
GROUP BY payment
ORDER BY COUNT DESC
LIMIT 1;
Note: the column you will get after the COUNT function will be a new column… And it has to have a name – so SQL automatically names it “count” (check the latest screenshot above). When you refer to this column in your ORDER BY clause, you have to use this new name
Something like this maybe?
select *
from (
select payment, count(*) as c from paymentType group by payment
) x
order by c desc
limit 1
Don't put the space in HAVING clause between COUNT and (
Try this:
ORDER BY COUNT(payment) DESC
Count aggregate function SQL
Related
I have a table in which I fetch results according to winning in descending order. But I need id for Load More function.
I have successfully added serial number but for load more function i want to use serial number id to get the result and not repeating the result. But when I use into where clause I get error unknown column.
Any help would be appreciated.
I have deleted the stuff, as I post my answer.
try this
select * from (
SELECT rank()over(order by pic_wins desc) as serial_number,
pic_id,
pic_caption,
pic_image,
pic_wins
FROM pics
WHERE pic_status = '1'
order by pic_wins desc
) as foo
where serial_number=1;
you can't use alias in where clause but you can use it an sub query
And if you want continuous series in serial_number use dense_rank()
Thanks for everyone, I got my answer to little modify #dgnk answer
select * from ( SELECT #s:=#s+1 serial_number, pic_id, pic_caption, pic_image, pic_wins FROM pics, (SELECT #s:= 0) AS s WHERE pic_status = '1' order by pic_wins desc ) as foo where serial_number > 8
Hi I have a query like this
select avg(TotalSale) as value from Sales group by SalesPerson;
which gives me the output as
value
50.0000
250.0000
62.5000
I want to just get the max out of the values. What should i do?
I tried
select max(avg(TotalSale)) as value from Sales group by SalesPerson; which is wrong.
select avg(TotalSale) as value from Sales group by SalesPerson order by value desc limit 1; which works partially.
What would be better approach to do this?
Wrap your query....
select max(avgvalue)
from
(
select avg(totalsale) as avgvalue
from sales
group by salesperson
)z
Use order by and fetch one row:
select avg(TotalSale)
from Sales
group by SalesPerson
order by avg(TotalSale) desc
limit 1;
You can also get the SalePerson involved with this as well.
I guess it can be done in a single query .Try this :
SELECT
Max(Totalsale) as Value
FROM
Sales
ORDER BY avg(Totalsale)
LIMIT 1;
I have a table with name and order date. I am trying to come up with a query to show the last time each name ordered in reverse order. So each names newest order but sorted by the name that ordered earliest.
So with this data
Tom,10/01/2013
Bob,09/24/2013
Tom,11/03/2013
Tom,10/24/2013
Bill,11/01/2013
Bob,10/22/2013
I want this
Bob,10/22/2013
Bill,11/01/2013
Tom,11/03/2013
I have tried SELECT * from orders group by name order by -odate
that got either random or older than the newest dates
I then tried SELECT * from orders order by -odate group by name
thinking it would sort first but it returned nothing
Thanks
Ed
SELECT name, MAX(odate) odate FROM orders GROUP BY name ORDER BY odate
The key thing here is to GROUP BY the name, but to select the maximum value that you need.
When grouping you can perform range functions on other fields.
Using the ORDER BY DESC puts the most recent first.
SELECT name, MAX(odate) AS odate FROM orders GROUP BY name ORDER BY odate DESC
see SQL Fiddle
Try using MAX function for your date column
SELECT name, MAX(odate) from orders group by name order by odate
Basically I have a review table for product. The attributes are reviewID, reviewCustName, reviewText, productID. So I wonder is there any ways to count the product with most reviews? Here is my SQL statement:
SELECT productID, count(*) AS mostReviews, MAX(mostReviews) FROM sm_review GROUP BY productID;
I wonder is it possible to write such SQL statement? Or i there any better way?
Thanks in advance.
You can use the following to get the result. This gets the total count for each product but when you order the count in a descending order and apply LIMIT 1 it returns only the product with the most reviews:
select count(*) total
from sm_review
group by productId
order by total desc
limit 1
It should just be;
SELECT count(*) AS num_reviews FROM sm_review
GROUP BY productID ORDER BY num_reviews DESC LIMIT 1;
Note the ORDER BY num_reviews and the LIMIT 1 which limits the number of results.
I have a social network I am coding but it's a bit different than others, in this case there is only ever one status show per user on a feed section.
So I need to sort the status by date with the latest ones on top but also group by the userID
unfortunately I can't seem to get this working....
This is my current query:
SELECT *
FROM status
WHERE userID != "83" #This is just so my statuses don't show
GROUP BY userID
ORDER BY addedDate DESC
LIMIT 10
I expect to see the latest status results and only one per user instead I see the first statuses so the group by is working but not the order by.
Thanks in advance.
As mentioned in the comments to Robin's answer, that approach is unreliable because MySQL does not guarantee that it will always return the most recent status from each group. You must instead join your table with a subquery that selects the most recent status (based on addedDate).
SELECT *
FROM status
NATURAL JOIN (
SELECT userID, MAX(addedDate) as addedDate
FROM status
GROUP BY userID
) AS mostRecent
ORDER BY addedDate DESC
LIMIT 10
Note that if a user has multiple status updates with the same addedDate, the server will return all of them (whereas Robin's query would return an indeterminate one); if you need control over such a situation, you will need to define how one determines which such status update should be selected.
SELECT userID, max(addedDate)
FROM status
WHERE userID != "83" #This is just so my statuses don't show
GROUP BY userID
SELECT *
FROM ( SELECT *
FROM status
WHERE userID != "83"
ORDER BY addedDate DESC) AS h
GROUP BY userID
ORDER BY addedDate DESC
LIMIT 10
You must ORDER BY before GROUP BY'ing.
Example 1
Example 2