kevinfwb Posted August 14, 2007 Share Posted August 14, 2007 I have a MySQL database for a chain of restaurants that tracks store sales, among other things. I have a table called sales that stores storeNum period week year sales I'm trying to find which store has the max sales for a given period/week/year. storeNum, period, week and year are all INTS and sales is a double I have sales set to a double because other fields stores/calculates percentage of increase/decrease etc to the 4th decimal. I can get the code to work using SELECT MAX(sales) FROM Sales WHERE period=8 AND week=2 AND year=2007 However, this doesn't give me the corresponding store number so I tried the following code that returns 0 results. SELECT sales, storeNum FROM Sales WHERE period=8 AND week=2 AND year=2007 AND sales=(SELECT MAX(sales) FROM Sales) The code doesn't pull any rows for any period, week, or year. I think I've tried this code in just about every imaginable syntax. Any help is greatly appreciated. -Kevin Link to comment https://forums.phpfreaks.com/topic/64774-help-with-max-function/ Share on other sites More sharing options...
Illusion Posted August 14, 2007 Share Posted August 14, 2007 why u need a subquery , insted of that u can do SELECT sales, storeNum ,MAX(sales) FROM Sales WHERE period=8 AND week=2 AND year=2007; Link to comment https://forums.phpfreaks.com/topic/64774-help-with-max-function/#findComment-323534 Share on other sites More sharing options...
Barand Posted August 14, 2007 Share Posted August 14, 2007 SELECT sales, storeNum FROM Sales WHERE period=8 AND week=2 AND year=2007 ORDER BY sales DESC LIMIT 1 Link to comment https://forums.phpfreaks.com/topic/64774-help-with-max-function/#findComment-323617 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.