SORU
22 Ocak 2010, Cuma


nasıl kümülatif toplam almak için

declare  @t table
(
    id int,
    SomeNumt int
)




insert into @t
select 1,10
union
select 2,12
union
select 3,3
union
select 4,15
union
select 5,23


select * from @t

yukarıdaki seçmek bana aşağıdaki döndürür.

id  SomeNumt
1   10
2   12
3   3
4   15
5   23

Nasıl aşağıdaki.. alabilirim

id  srome   CumSrome
1   10  10
2   12  22
3   3   25
4   15  40
5   23  63

TİA

CEVAP
22 Ocak 2010, Cuma


select t1.id, t1.SomeNumt, SUM(t2.SomeNumt) as sum
from @t t1
inner join @t t2 on t1.id >= t2.id
group by t1.id, t1.SomeNumt
order by t1.id

SQL Fiddle example

Çıktı

| ID | SOMENUMT | SUM |
-----------------------
|  1 |       10 |  10 |
|  2 |       12 |  22 |
|  3 |        3 |  25 |
|  4 |       15 |  40 |
|  5 |       23 |  63 |

Düzenleme:bu en db platformda çalışacağına dair yaygın bir çözümdür. Daha iyi bir çözüm, özel bir platform (örneğin, gareth) için kullanılabilir olduğunda, bunu kullanın!

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Gimpology.com Video Tutorials

    Gimpology.co

    3 ŞUBAT 2008
  • metal571

    metal571

    30 Mayıs 2006
  • The Amazing Atheist

    The Amazing

    20 Kasım 2006