此類問題還可以延伸到類似進銷存的批次計算中
sql語句無法簡單實現mdx語句的類似功能
clr的代碼如下
using System;
using System
using System
using System
using Microsoft
public partial class UserDefinedFunctions
{
// 保存當前組當前值
private static System
// 保存當前組
private static System
/// <summary>
/// 獲取當前組上條記錄數值
/// </summary>
/// <param name="key"> 並發鍵 </param>
/// <param name="currentGroup"> 當前組 </param>
/// <param name="currentValue"> 當前組當前值 </param>
/// <returns></returns>
[Microsoft
public static SqlString GetPrevMemberValue(SqlString key
{
if (key
try
{
SqlString prevMemberValue = _listValue[key
// 組變更
if (_listGroup[key
{
prevMemberValue = SqlString
_listGroup[key
}
// 值變更
_listValue[key
return prevMemberValue;
}
catch
{
return SqlString
}
}
/// <summary>
/// 初始化並發鍵
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
[Microsoft
public static SqlBoolean InitKey(SqlString key)
{
try
{
_listValue
_listGroup
return true ;
}
catch
{
return false ;
}
}
/// <summary>
/// 釋放並發鍵
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
[Microsoft
public static SqlBoolean DisposeKey(SqlString key)
{
try
{
_listValue
_listGroup
return true ;
}
catch
{
return false ;
}
}
};
部署和生成自定義函數
CREATE ASSEMBLY TestForSQLCLR FROM
go
CREATE FUNCTION dbo
(
@key nvarchar (
@initByDim nvarchar (
@currentValue nvarchar (
)
RETURNS nvarchar (
AS EXTERNAL NAME TestForSQLCLR
go
CREATE FUNCTION dbo
(
@key nvarchar (
)
RETURNS bit
AS EXTERNAL NAME TestForSQLCLR
go
CREATE FUNCTION dbo
(
@key nvarchar (
)
RETURNS bit
AS EXTERNAL NAME TestForSQLCLR
這樣我們就可以使用了
declare @t table (
[ 區域 ] [varchar](
[TradeMonth] [varchar](
[TradeMoney] [float] NULL
[TradeArea] [float] NULL
[TradePrice] [float] NULL
)
insert into @t
select
select
select
select
select
select
select
select
select
select
declare @key varchar (
declare @b bit
set @key= newid ()
select @b= dbo
select 區域
cast ( round (( Tradeprice
select *
) t
select @b= dbo
/*
區域 TradeMonth TradePrice LastMonthPrice 環比
闵行
闵行
闵行
浦東
浦東
浦東
浦東
普陀
普陀
普陀
*/
這個函數寫的還是比較粗糙
From:http://tw.wingwit.com/Article/program/MySQL/201311/29559.html