subquery or leftjoin with group by which one is faster?

前端 未结 3 2090
忘掉有多难
忘掉有多难 2020-11-28 14:52

\"Queryi have to show running total with the total column in my application ... so i have used the fol

3条回答
  •  醉话见心
    2020-11-28 15:09

    I think you will find the recursive CTE a bit faster.

    ;with C as
    (
      select t.ind,
             t.col1,
             t.col1 as Total
      from @tmp as t
      where t.ind = 1
      union all
      select t.ind,
             t.col1,
             C.Total + t.col1 as Total
      from @tmp as t
        inner join C
          on C.ind + 1 = t.ind
    )
    select C.col1,
           C.Total
    from C
    

    any other method by which is more faster

    Yes there is. If you are looking for outstanding performance you should just pull your data in a simple select and do the running total calculation on the client when you do the presentation.

提交回复
热议问题