Running Totals

Last Post 10 Jan 2008 08:43 AM by SQLUSA. 1 Replies.
AddThis - Bookmarking and Sharing Button
Author Messages
billthecat0702
New Member
New Member

--
10 Jan 2008 08:17 AM
I have a table that has the following structure
Which I create from raw data using a query like this:

SELECT dbo.F_START_OF_DAY(Datetime), Tag, MAX(KWH)
FROM Raw_Data
WHERE Tag = @Tagname AND Datetime BETWEEN @FromDate AND @ToDate
GROUP BY dbo.F_START_OF_DAY(Datetime), Tag

DateTime, Tag, KWH (Kilowatt-hours)
08/01/07 00:00:00.000, SA15, 709700
08/02/07 00:00:00.000, SA15, 718800
08/03/07 00:00:00.000, SA15, 728000
08/04/07 00:00:00.000, SA15, 737000


Now, I need to calculate Total Usage per day such as the following as I create the table from raw data...
DateTime, Tag, KWH, Total_Usage
08/01/07 00:00:00.000, SA15, 709700, NULL
08/02/07 00:00:00.000, SA15, 718800, 9100 <-- Note this is 8/2/07's max KWH minus 8/1/07's max KWH
08/03/07 00:00:00.000, SA15, 728000, 9200
08/04/07 00:00:00.000, SA15, 737000, 9000

Can someone help me write the query to do this?


Thanks,

Don
SQLUSA
New Member
New Member

--
10 Jan 2008 08:43 AM
This is how you do Running Totals:


http://www.sqlusa.com/bestpractices...ingcursor/


Let me know if this is helpful.


Kalman Toth - Database, Data Warehouse & Business Intelligence Architect
SQLUSA: http://www.sqlusa.com/order2005repo...gservices/ The Best SQL Server 2005 Training in the World!


Acceptable Use Policy
---