SQL QUERY
-
I have a table Id , b1 Id B1 espectedresult 1 5 5 2 19 24 3 14 38 4 41 79 5 14 93 6 41 134 I want the sum b1 at every Id can some one help?
select sum(b1),id from tablename group by id
-
I have a table Id , b1 Id B1 espectedresult 1 5 5 2 19 24 3 14 38 4 41 79 5 14 93 6 41 134 I want the sum b1 at every Id can some one help?
It looks like what you want is a running total. I would have to do a search and do not have the time right now.
-
I have a table Id , b1 Id B1 espectedresult 1 5 5 2 19 24 3 14 38 4 41 79 5 14 93 6 41 134 I want the sum b1 at every Id can some one help?
Assuming MSSQL < 2012, something like this should work:
SELECT
ID,
B1,
(SELECT Sum(B1) FROM TheTable As T2 WHERE T2.ID <= T1.ID) As Actual
FROM
TheTable As T1
ORDER BY
IDhttp://www.sqlfiddle.com/#!3/59751/2[^] For MSSQL 2012:
SELECT
ID,
B1,
Sum(B1) OVER (ORDER BY ID ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) As Actual
FROM
TheTable
ORDER BY
IDhttp://www.sqlfiddle.com/#!6/59751/1[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer