Pergunta

I would like to accumulate my data as you can see below there is origin table table1:

  1. What is the best query for to do this?
  2. Is possible to do this dynamically - when I add more types of terms??

Table 1

ID  |  term  |  value
-----------------------
1   |   I    |   100
2   |   I    |   200
3   |   II   |   100
4   |   II   |    50 
5   |   II   |    75
6   |   III  |    50
7   |   III  |    65
8   |   IV   |    30
9   |   IV   |    45

And the result should be like below:

YTD  | Acc Value
------------------
I-I  |   300
I-II |   525
I-III|   640
I-IV |   715

Thanks

Foi útil?

Solução

    select 
        (select min(term) from yourtable ) +'-'+term,       
        (select sum(value) from yourtable  t1 where t1.term<=t.term)
    from yourtable t 
    group by term
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top