californiasteve Posted January 22, 2015 Share Posted January 22, 2015 (edited) I have a running total that I am pulling from a database that I need to sort by year. I have tried adding WHERE YEAR(datepicker) = '2015' but keep getting an error. Here is the original query that works fine when I run it. Just don't need all the records. SELECT id, datepicker, startmiles, endmiles, O.totalMiles, car, section, (select sum(totalMiles) FROM vehicle WHERE id <= O.id) 'runningTotal' FROM vehicle O ORDER BY datepicker DESC Here is what I have tried. SELECT id, datepicker, startmiles, endmiles, O.totalMiles, car, section, (select sum(totalMiles) FROM vehicle WHERE id <= O.id) 'runningTotal' FROM vehicle O AND YEAR(datepicker = '2015' ORDER BY datepicker DESC Edited January 22, 2015 by californiasteve Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted January 23, 2015 Share Posted January 23, 2015 It looks like you're missing the close parenthesis for the YEAR() function here: YEAR(datepicker = '2015' If that doesn't fix the issue, what error(s) are you seeing? Quote Link to comment Share on other sites More sharing options...
californiasteve Posted January 23, 2015 Author Share Posted January 23, 2015 I'm getting this error. #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND YEAR(datepicker) = '2015' ORDER BY datepicker DESC using this query SELECT id, datepicker, startmiles, endmiles, O.totalMiles, car, section, (select sum(totalMiles) FROM vehicle WHERE id <= O.id) 'runningTotal' FROM vehicle O AND YEAR(datepicker) = '2015' ORDER BY datepicker DESC Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted January 23, 2015 Share Posted January 23, 2015 Try removing the "AND" before the YEAR() function. AND YEAR(datepicker) It also looks like you're missing the "WHERE" part in the where clause. WHERE YEAR(datepicker) = '2015' Quote Link to comment Share on other sites More sharing options...
Barand Posted January 24, 2015 Share Posted January 24, 2015 You are using a really inefficient dependent subquery which, for every record, queries the table to get the total miles for the vehicle. It would run quicker if you use a JOIN. EG SELECT id , datepicker , startmiles , endmiles , O.totalMiles , car , section , runningTotal.totalMiles FROM vehicle O INNER JOIN ( select id , sum(totalMiles) as totalMiles FROM vehicle GROUP BY id ) runningTotal USING (id) WHERE YEAR(datepicker) = '2015' ORDER BY O.id, datepicker DESC If, as the name suggests, you want a running total then use user variables in the query to accumulate the total EG SELECT id , datepicker , startmiles , endmiles , O.totalMiles , car , section , @rtot := IF(@id = id, @rtot+totalMiles, totalMiles) as runningTotal , @id := id as dummyId FROM vehicle O JOIN (SELECT @id:+0, @rtot:=0) as init WHERE YEAR(datepicker) = '2015' ORDER BY O.id, datepicker DESC 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.