问题
I have a weird requirement which I need to use inside my Stored Procedure in SQL Server 2008 R2.
I need a FIRST aggregate function which returns the first element of a sequence and I will use that with HAVING clause.
Let me give you an example:
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', 2, 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);
DECLARE @minAllowParam AS INT = 3;
SELECT ft.CategoryName, SUM(ft.Price) FROM @fooTable ft
GROUP BY ft.CategoryName;
As you see, we have a table and some dummy values. Inside the SELECT query, we group the categories together and sum the price of the products up.
This query returns the following result:
CategoryName TotalPrice
---------------- ----------------
Cat1 345.52
Cat2 12.34
Cat3 25.43
What I need here is something like this:
SELECT ft.CategoryName, SUM(ft.Price) FROM @fooTable ft
GROUP BY ft.CategoryName
HAVING GetFIRST(MinAllow) >= @minAllowParam;
In that our case with a query something like this, we should be able to select following results:
INSERT INTO @fooTable VALUES(2, 'Cat2', 'Product2', 4, 12.34);
INSERT INTO @fooTable VALUES(4, 'Cat3', 'Product4', 4, 12.43);
INSERT INTO @fooTable VALUES(5, 'Cat3', 'Product5', 1, 13.00);
As the INSERT INTO @fooTable VALUES(1, 'Cat1', 'Product1', 2, 112.2); record is the first element of a sequence and has the value of 2 for MinAllow column, Cat1 should be out of scope here. On the other hand, INSERT INTO @fooTable VALUES(5, 'Cat3', 'Product5', 1, 13.00); record has the value of 1 for MinAllow column but is the second element of the sequence. So, Cat3 is safe and can be selected.
Note:
MINorMAXis not what I am looking for!
I know that this example logically does not make sense but I have a situation which it totally does and hard to explain here on the other hand.
Any thoughts?
Edit:
I am assuming that I can achieve what I want here by creating a CLR User-Defined Aggregate Function but I do not want to do this if there is any other choice
回答1:
How about
SELECT f1.CategoryName, SUM(f1.Price)
FROM @fooTable AS f1
INNER JOIN (
SELECT MinAllow, CategoryName
FROM (
SELECT MinAllow, CategoryName, ROW_NUMBER() OVER (PARTITION BY CategoryName ORDER BY ID) AS m
FROM @fooTable
) AS f
WHERE m = 1
) AS f2 ON f1.CategoryName = f2.CategoryName
WHERE f2.MinAllow >= @minAllowParam
GROUP BY f1.CategoryName
I know not a very elegant query. Maybe I can tweak it a little if I work on it a little longer!
Edit: Ok the inner most subquery should be unnecessary. This should also work:
SELECT f1.CategoryName, SUM(f1.Price)
FROM @fooTable AS f1
INNER JOIN (
SELECT MinAllow, CategoryName, ROW_NUMBER() OVER (PARTITION BY CategoryName ORDER BY ID) AS m
FROM @fooTable
) AS f2 ON f1.CategoryName = f2.CategoryName
WHERE f2.m = 1 AND f2.MinAllow >= @minAllowParam
GROUP BY f1.CategoryName
回答2:
UPDATE: a readable query:
SELECT ft.CategoryName, SUM(ft.Price)
FROM fooTable ft
cross apply
(
select top 1 MinAllow
from fooTable a
where a.CategoryName = ft.CategoryName
order by ID
) a
where a.MinAllow >= @minAllowParam
GROUP BY ft.CategoryName;
You might filter categories having first (by id?) MinAllow >= @minAllowParam:
...
inner join
(
select
-- Add columns you might need
CategoryName,
Price
from
fooTable
inner join
(
-- First ID in category
select
min(id) id
from
fooTable
group by
CategoryName
) firstID
-- Back to all columns
ON fooTable.ID = firstID.ID
-- but only if category sequence starts properly
AND fooTable.MinAllow >= @minAllowParam
) a
-- Allow MinAllow categories only
ON fooTable.CategoryName = a.CategoryName
回答3:
Have a look at ROW_NUMBER() with partitioning:
http://msdn.microsoft.com/en-us/library/ms186734.aspx
回答4:
There are two ways that I know to achieve FIRST aggregate function:
ROW_NUMBER()
(WHERE ROW_NUMBER_COLUMN = 1)
http://msdn.microsoft.com/en-us/library/ms186734.aspx
AND
SELECT ...., (SELECT TOP 1 FROM ... WHERE (outer table join) ORDER BY SOMETHING) AS [FIRST]
FROM ...
来源:https://stackoverflow.com/questions/10120145/first-aggregate-function-which-i-can-use-with-having-clause