-4

表有类似的数据

<--------------------------->  
no      category     value 
<--------------------------->      
1        1           2000    
2        1           1000     
3        2            500    
4        3           3000    
5        1           2000   
6        2           -500        
7        3           5000    
8        1          -1000 

我想要像这样的输出

<----------------------------------> 
no  category     amount       Sum   
<---------------------------------->   
1        1       2000         2000                
2        1       1000         3000                 
3        2        500          500                 
4        3       3000         3000                 
5        1       2000         5000                  
6        2       -500            0                 
7        3       5000         8000
8        1      -1000         4000 

例如:category=1 这样,

no:1
amount=2000--->total=2000
no:2
amount=1000---->total=3000
no:5
amount=2000---->total=5000
no:8
amount=-1000---->total=4000

这意味着每一行上每个类别的总和并且具有更好的性能

4

3 回答 3

0

您需要使用行号自连接两个表:

;WITH t(No,Category,value,row) AS
(
    SELECT No, category, value
      ,ROW_NUMBER() OVER (Partition by category ORDER BY no) AS row
   FROM Table1
)
SELECT t1.No, t1.category, t1.value, sum(t2.value) as sums
  FROM t t1
  JOIN t t2
    ON t1.row >= t2.row
   AND t1.Category = t2.Category
 GROUP BY t1.No, t1.category, t1.value;

输出:

| NO | CATEGORY | VALUE | SUMS |
--------------------------------
|  1 |        1 |  2000 | 2000 |
|  2 |        1 |  1000 | 3000 |
|  3 |        2 |   500 |  500 |
|  4 |        3 |  3000 | 3000 |
|  5 |        1 |  2000 | 5000 |
|  6 |        2 |  -500 |    0 |
|  7 |        3 |  5000 | 8000 |
|  8 |        1 | -1000 | 4000 |

看到这个 SQLFiddle

于 2013-07-30T11:08:55.837 回答
0

你需要的是计算滚动总数。我知道的 SQL Server 2005 / 2008 / 2008 R2 中最快的方法在这里描述:https ://stackoverflow.com/a/13744550/1744834 。您必须在每个类别中都有唯一的 secuential 列才能使用此方法,如下所示:

create table #t (no int, category int, value int, id int, primary key (category, id))

insert into #t (no, category, value, id)
select no, category, value, row_number() over (partition by category order by no)
from test

;with 
CTE_RunningTotal
as
(
    select T.no, T.category, T.value, cast(T.value as decimal(29, 10)) as total_value, T.id
    from #t as T
    where T.id = 1
    union all
    select T.no, T.category, T.value, cast(T.value + C.total_value as decimal(29, 10)) as total_value, T.id
    from CTE_RunningTotal as C
        inner join #t as T on T.id = C.id + 1 and T.category = C.category
)
select C.no, C.category, C.value, C.value
from CTE_RunningTotal as C
option (maxrecursion 0)

SQL 提琴示例

您也可以使用更短的查询,但性能更差(我认为递归 CTE 是 O(N^2) vs O(N)):

select t1.no, t1.category, t1.value, sum(t2.value)
from test as t1
    inner join test as t2 on t2.category = t1.category and t2.no <= t1.no
group by t1.no, t1.category, t1.value
order by t1.no

SQL 提琴示例

于 2013-07-30T11:27:29.827 回答
-1

您可以使用以下 sql 函数SQL 函数它向您展示如何使用它以及如何应用它

于 2013-07-30T10:57:01.540 回答