[thelist] MySQL selecting sum of records from time period

Sarah Sweeney mr.sanders at designshift.com
Wed Aug 18 07:47:26 CDT 2004


> I'm working on an query in mysql to select some totals from the previous 
> week, previous month, etc.
> 
> I've got an auto-incrementing ID field, so that saves a bit of date 
> manipulation work...
> 
> I was thinking this would do it
> 
> select sum(cCost) as cCost, sum(zCost) as zCost, sum(zTotal) as zTotal, 
> sum(cTotal) as cTotal
> from dailyTotalsA
> where ID <= max(ID)
> and ID >= (max(ID)-7)

How about this:

SELECT   sum(cCost) AS cCost, sum(zCost) AS zCost,
          sum(zTotal) AS zTotal, sum(cTotal) AS cTotal
FROM     dailyTotalsA
ORDER BY ID DESC
LIMIT    7

HTH

-- 
Sarah Sweeney   Web Developer & Programmer
     Portfolio   http://sarah.designshift.com
          Blog   http://hardedge.ca


More information about the thelist mailing list