I have a table with time (column) and power (column). I want to calculate different result sets on following table data:
My Table:
[timestamp] [power]
2016-01-06 13:32:27 1
2016-01-06 13:33:27 2
2016-01-06 13:34:27 3
2016-01-06 13:35:27 4
2016-01-06 13:36:27 5
2016-01-06 13:37:27 6
2016-01-06 13:38:27 7
2016-01-06 13:39:27 8
2016-01-06 13:40:27 9
2016-01-06 13:41:27 10
Total as:
[timestamp] [power] Total
2016-01-06 13:32:27 1 1
2016-01-06 13:33:27 2 3
2016-01-06 13:34:27 3 6
2016-01-06 13:35:27 4 10
2016-01-06 13:36:27 5 15
2016-01-06 13:37:27 6 21
2016-01-06 13:38:27 7 28
2016-01-06 13:39:27 8 36
2016-01-06 13:40:27 9 45
2016-01-06 13:41:27 10 55
Total for every 5 minute as:
[timestamp] [power] Total_every_5minutes
--2016-01-06 13:32:27 1 1
--2016-01-06 13:33:27 2 3
--2016-01-06 13:34:27 3 6
--2016-01-06 13:35:27 4 10
2016-01-06 13:36:27 5 15
--2016-01-06 13:37:27 6 21
--2016-01-06 13:38:27 7 28
--2016-01-06 13:39:27 8 36
2016-01-06 13:40:27 9 45
Similarly I want to calculate for average, minimum, maximum, standard deviation, & count. As of now I have written a query which will add a column with,
Please note:
That I should not write stored procedure. As I have a windows application where user selects database and it will list the tables, from the listed tables user selects the columns and on a particular column he will set to calculate total or etc.. Upon selection the query is building. and the query will execute and bind the results to datagridview
.
IF OBJECT_ID('tempdb..#mytemptable') IS NOT NULL DROP TABLE #mytemptable;
SELECT SUM( CAST ([power] AS bigint) ) AS power_TOTAL INTO #MYTEMPTABLE
FROM ( SELECT t1.[timestamp], t1.[power] FROM [REPORT] t1 ) TEMPTABLE
SELECT t1.[timestamp], t1.[power], RM_temp.* FROM [REPORT] t1 , #MYTEMPTABLE RM_temp;