Bosna i Hercegovina



#1 20.08.2018-10:44
Gjoreski Offline
Super Moderator
Registrovan/a od: 02.02.2009-22:24
Komentari: 1,541


Subject: Komulativni zbir
Verzija 1
DownloadIzvorni kod (SQL):
  1. SELECT t1.DayCount, t1.Sales, SUM(t2.Sales) AS suma
  2. FROM dbo.Sales t1
  3. INNER JOIN Sales t2 ON t1.DayCount >= t2.DayCount
  4. GROUP BY t1.DayCount, t1.Sales
  5. ORDER BY t1.DayCount

Verzija 2

DownloadIzvorni kod (SQL):
  1. DECLARE @SalesTbl TABLE (DayCount smallint, Sales money, RunningTotal money)
  2.  
  3. DECLARE @DayCount smallint,
  4.         @Sales money,
  5.         @RunningTotal money
  6.  
  7. SET @RunningTotal = 0
  8.  
  9. DECLARE rt_cursor CURSOR
  10. FOR
  11. SELECT DayCount, Sales
  12. FROM tSales
  13. ORDER BY DayCount
  14.  
  15. OPEN rt_cursor
  16.  
  17. FETCH NEXT FROM rt_cursor INTO @DayCount,@Sales
  18.  
  19. WHILE @@FETCH_STATUS = 0
  20.  BEGIN
  21.  SET @RunningTotal = @RunningTotal + @Sales
  22.  INSERT @SalesTbl VALUES (@DayCount,@Sales,@RunningTotal)
  23.  FETCH NEXT FROM rt_cursor INTO @DayCount,@Sales
  24.  END
  25.  
  26. CLOSE rt_cursor
  27. DEALLOCATE rt_cursor
  28.  
  29. SELECT * FROM @SalesTbl
↑  ↓

Stranice (1): 1


All times are GMT +01:00. Current time: 10.12.2018-07:40.