dlebowski Posted September 27, 2011 Share Posted September 27, 2011 Hi. I have a table that looks like the following: +------+---------------+ | Price | ItemNumber | +------+---------------+ | 1 | 33 | | 2 | 66 | | 3 | 66 | | 4 | 44 | | 5 | 44 | | 6 | 44 | +------+--------------- What I want to do is takes the highest Price for each ItemNumber, and sum those values. For example: 1 - 33 3 - 66 6 - 44 --------- Total: 10 I am assuming that I am going to use MAX function and SUM, but how do I use both. Here is my query: SELECT MAX(Price), ItemNumber FROM mytable GROUP BY ItemNumber ORDER BY ItemNumber This query gives me this: 1 - 33 3 - 66 6 - 44 --------- So how do I use that same query to give me a SUM of those values? Than you in advance! Quote Link to comment https://forums.phpfreaks.com/topic/247986-using-sum-with-max/ Share on other sites More sharing options...
awjudd Posted September 28, 2011 Share Posted September 28, 2011 Something like this should work: SELECT SUM(MaxPrice) AS TotalPrice FROM ( SELECT MAX(Price) AS MaxPrice FROM mytable GROUP BY ItemNumber ) Basically running your max in the sub query and then based on those values, sum them up ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/247986-using-sum-with-max/#findComment-1273395 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.