sql server 2012 - Calculate daily use from running total in c# -


i have sql-table following columns , trying find way automatically calculate daily usage each entry. 'total usage' running total , there entry once per day per person.

ultimately want able manipulate list in c# sql-query based method useful well. easy in excel hoping way automate it....

date  |  name  |  total usage | daily usage  2016-01-01 | john | 60 | ? 2016-01-02 | john | 63 | ? 2016-01-03 | john | 66 | ? 2016-01-01 | jane | 30 | ? 2016-01-02 | jane | 50 | ? 2016-01-03 | jane | 75 | ? 

pertinent question, can result table using following code snippet written in c#, utilizes system.data , system.data.sqlclient object library:

strsql = "select [name], avg([total usage]) avrusage [yourtablename] group [name]";                // data table 2 columns: name , avgusage                 using (sqlconnection _connsql = new sqlconnection(connstring))                 {                     using (sqlcommand _commandsql = new (sqlcommand())                     {                         _commandsql.commandtype = commandtype.text;                         _commandsql.connection = _connsql;                         _commandsql.commandtext = strsql;                         _connsql.open();                          using (sqldatareader _drsql = _commandsql.executereader())                         {                             datatable _dt = new datatable();                             _dt.load(_drsql);                             return _dt;                         }                     }                 }             }             catch (exception ex) {                 throw;              } 

, connstring - connection string sql database.

alternatively, instead of avg([total usage]) can apply sum([total usage])/365, depends on particular requirement definitions.

hope may help.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -