我需要一個返回序列的第一個元素的FIRST聚合函數,我將使用HAVING子句。CLR用戶定義的聚合函數,以獲得序列的第一個元素
這是一個問題,可以被視爲我的上一個問題的延續:FIRST aggregate function which I can use with HAVING clause。
事實證明,只有一個聚合函數可以解決我的問題,所以我試圖創建一個:
[Serializable]
[SqlUserDefinedAggregate(
Format.UserDefined, //use clr serialization to serialize the intermediate result
IsInvariantToNulls = true, //optimizer property
IsInvariantToDuplicates = false, //optimizer property
IsInvariantToOrder = false, //optimizer property
MaxByteSize = 8000) //maximum size in bytes of persisted value
]
public struct GetFirst : IBinarySerialize {
private string allValues;
public void Init() {
allValues = string.Empty;
}
private void incrementAndAdd(SqlInt32 value) {
allValues += (value.Value.ToString() + ",");
}
public void Accumulate(SqlInt32 Value) {
incrementAndAdd(Value);
}
public void Merge(GetFirst Group) {
}
public SqlInt32 Terminate() {
// Put your code here
return new SqlInt32(int.Parse(allValues.Split(',')[0]));
}
// This is a place-holder member field
private SqlInt32 var1;
public void Read(System.IO.BinaryReader r) {
allValues = r.ReadString();
}
public void Write(System.IO.BinaryWriter w) {
w.Write(this.allValues);
}
}
這就是我如何使用它:
DECLARE @fooTable AS TABLE(
ID INT,
CategoryName NVARCHAR(100),
Name NVARCHAR(100),
MinAllow INT,
Price DECIMAL(18,2)
);
INSERT INTO @fooTable VALUES(1, 'Cat1', 'Product1', 3, 112.2);
INSERT INTO @fooTable VALUES(2, 'Cat2', 'Product2', 4, 12.34);
INSERT INTO @fooTable VALUES(3, 'Cat1', 'Product3', 5, 233.32);
INSERT INTO @fooTable VALUES(4, 'Cat3', 'Product4', 4, 12.43);
INSERT INTO @fooTable VALUES(5, 'Cat3', 'Product5', 1, 13.00);
INSERT INTO @fooTable VALUES(7, 'Cat4', 'Product7', 1, 15.00);
INSERT INTO @fooTable VALUES(6, 'Cat4', 'Product6', 3, 13.00);
DECLARE @minAllowParam AS INT = 3;
SELECT ft.CategoryName, SUM(ft.Price), dbo.GetFirst(ft.MinAllow) FROM @fooTable ft
GROUP BY ft.CategoryName
HAVING dbo.GetFirst(ft.MinAllow) >= @minAllowParam;
它有時會返回正確的結果,有時不是,我不確定我是否實施了正確的。任何想法,如果我根據我的要求得到這個權利?
如果如果(這是不是像@Mikael埃裏克森指出)訂單保證是存在的,您不是*在您的查詢中指定*排序 - 表格不要*有一個訂單,所以要求查詢中沒有出現'ORDER BY'的'FIRST'*某處*是不明確的。 – 2012-04-12 12:06:21