Sql 需要基于类别的值总和
表中有如下数据Sql 需要基于类别的值总和,sql,sql-server-2005,sum,Sql,Sql Server 2005,Sum,表中有如下数据 <---------------------------> no category value <---------------------------> 1 1 2000 2 1 1000 3 2 500 4 3 3000 5 1
<--------------------------->
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
这意味着每行上每个类别的总和
如果性能更好您可以使用以下sql函数它向您展示了如何使用它以及如何应用它您需要使用行号自联接两个表:
;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 |
请参见您需要的是计算滚动总计。 我所知道的SQL Server 2005/2008/2008 R2中最快的方法如下所述:。要使用此方法,您必须在每个类别中具有唯一的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)
您也可以使用更短的查询,但性能更差。我认为对于递归CTE,它是在^2上,而不是在ON上:
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
请展示您的努力。并定义更好的性能我需要代码@Dukeling。这应该是一个注释。另外,OP希望它用于SQL Server而不是MySQL。它需要00:00:02执行时间……它真的很慢@himso56@user2633770-我认为没有比这更快的方法了。看,这只需要1到3毫秒。我有8000条记录。这是在sql server 2005中工作的吗@Romanpekarys它应该在SQL Server 2005中工作类型在递归查询CTE_RunningTotal的列total_值中的锚点和递归部分之间不匹配这是我得到的错误您可能有值列的十进制类型?为什么上面的代码不能使用十进制值?
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)
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