Jump to content

Query Problem


californiasteve

Recommended Posts

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 by californiasteve
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.