_대문 | 방명록 | 최근글 | 홈피소개 | 주인놈 |
FrontPage › 누적을구하는집계함수
|
|
에이.. 잘 안된다.. 다시 연구해 봐야 할 듯한데...
sql server 2012에서 쉽게 분석 함수로 구할 수 있으니.. 그냥 패스.. [edit]
1 소스 #Imports System Imports System.Data Imports Microsoft.SqlServer.Server Imports System.Data.SqlTypes Imports System.IO Imports System.Text <Serializable(), SqlUserDefinedAggregate(Format.UserDefined, IsInvariantToNulls:=True, IsInvariantToDuplicates:=False, IsInvariantToOrder:=False, MaxByteSize:=8000)> _ Public Class Accumulator Implements IBinarySerialize ''' <summary> ''' The variable that holds the intermediate result of the concatenation ''' </summary> Private intermediateResult As SqlDecimal ''' <summary> ''' Initialize the internal data structures ''' </summary> Public Sub Init() Me.intermediateResult = 0 End Sub ''' <summary> ''' Accumulate the next value, not if the value is null ''' </summary> ''' <param name="value"></param> Public Sub Accumulate(ByVal value As SqlDecimal) If value.IsNull Then Return End If Me.intermediateResult += value End Sub ''' <summary> ''' Merge the partially computed aggregate with this aggregate. ''' </summary> ''' <param name="other"></param> Public Sub Merge(ByVal other As Accumulator) Me.intermediateResult += other.intermediateResult End Sub ''' <summary> ''' Called at the end of aggregation, to return the results of the aggregation. ''' </summary> ''' <returns></returns> 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 [edit]
3 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 [edit]
4 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 */
|
성공하느냐 실패하느냐는 다른 사람이 아닌 바로 내가 하는 일이다. 내가 바로 힘이다. 나는 내 앞의 장애물을 치울 수도 있고 미로 속에서 길을 잃을 수도 있다. 내 선택 내 책임 이다. 이기거나 지는 것은 오직 나만이 가진 내 운명의 열쇠에 달려있다. (알레인 맥스웰) |