#title 누적을 구하는 집계함수 [[TableOfContents]] 에이.. 잘 안된다.. 다시 연구해 봐야 할 듯한데... sql server 2012에서 쉽게 분석 함수로 구할 수 있으니.. 그냥 패스.. ==== 소스 ==== {{{ Imports System Imports System.Data Imports Microsoft.SqlServer.Server Imports System.Data.SqlTypes Imports System.IO Imports System.Text _ Public Class Accumulator Implements IBinarySerialize ''' ''' The variable that holds the intermediate result of the concatenation ''' Private intermediateResult As SqlDecimal ''' ''' Initialize the internal data structures ''' Public Sub Init() Me.intermediateResult = 0 End Sub ''' ''' Accumulate the next value, not if the value is null ''' ''' Public Sub Accumulate(ByVal value As SqlDecimal) If value.IsNull Then Return End If Me.intermediateResult += value End Sub ''' ''' Merge the partially computed aggregate with this aggregate. ''' ''' Public Sub Merge(ByVal other As Accumulator) Me.intermediateResult += other.intermediateResult End Sub ''' ''' Called at the end of aggregation, to return the results of the aggregation. ''' ''' Public Function Terminate() As SqlDecimal Return Me.intermediateResult End Function Public Sub Read(ByVal r As BinaryReader) Implements IBinarySerialize.Read intermediateResult = new SqlDecimal(r.ReadDecimal()) End Sub Public Sub Write(ByVal w As BinaryWriter) Implements IBinarySerialize.Write w.Write(Me.intermediateResult.Value) End Sub End Class }}} ==== 컴파일 ==== {{{ vbc.exe /t:library "c:\clr\accumulator.vb" }}} ==== DB에 설치 ==== {{{ --drop assembly accumulator create assembly accumulator from 'c:\clr\accumulator.dll'; go create aggregate acc(@input decimal(38,3)) returns decimal(38,3) external name accumulator.Accumulator; --대소문자 구문함 go }}} ==== sample ==== {{{ --drop table #temp3 create table #temp3 ( gubun int , val int ) insert #temp3 values(1,1); insert #temp3 values(1,2); insert #temp3 values(1,3); insert #temp3 values(2,4); insert #temp3 values(2,5); insert #temp3 values(3,6); insert #temp3 values(4,7); go select gubun , ods.dbo.acc(val) from #temp3 group by gubun /* gubun ----------- --------------------------------------- 1 6.000 2 9.000 3 6.000 4 7.000 */ }}}