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 Quote Link to comment 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; Quote Link to comment 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 Quote Link to comment 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.