• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 65
  • Last Modified:

simple mysql question

Dear experts,
I'd like to select the max month data from my database.
The max mount in my database is 11 however,  but below code gives me error
what do you suggest I should do?

SELECT month, sum(tot) as total FROM mytable where month=max(month) group by month desc

Open in new window

0
BR
Asked:
BR
  • 2
1 Solution
 
Ryan ChongCommented:
try:

SELECT month , sum(tot) as total FROM mytable 
group by month 
order by 1 desc limit 1

Open in new window

0
 
Pawan KumarDatabase ExpertCommented:
Please try this

/*Use this if you need all the data from the table*/
SELECT * FROM
mytable t INNER JOIN ( SELECT MAX(month) m1 FROM mytable ) p
ON p.m1 = t.month
 

/*Use this if you need month and sum*/
SELECT t.month,SUM(tot) total FROM
mytable t INNER JOIN ( SELECT MAX(month) m1 FROM mytable ) p
ON p.m1 = t.month
GROUP BY t.month
0
 
Pawan KumarDatabase ExpertCommented:
Please refresh the page, edited my last comment.
0
 
BRMarketingAuthor Commented:
thank you Pawan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now