Solved

Custom aggregate functions

Posted on 2002-06-05
18
1,572 Views
Last Modified: 2007-12-19
Is it possible to create my own aggregate functions like MAX, COUNT, SUM, etc. in MS-SQL 2000?

How?
0
Comment
Question by:Epsylon
  • 11
  • 7
18 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7056054
yes. look for UDF (user defined function) in books online.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7056061
0
 
LVL 13

Author Comment

by:Epsylon
ID: 7058667
Thanks for your response.

Only I can't find here how to create an aggregate function like SUM which does not just use a single field in a single row, but uses *all* rows.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7058890
This is an example of retreiving an aggregate from a function.

Here is a function:

CREATE FUNCTION dbo.AvgPrice( @StockId int )
RETURNS real
AS
BEGIN
  DECLARE @avg real
  SELECT @avg = avg(Price) FROM StockPrice
    WHERE StockID = @StockId
  RETURN ( @avg )
END
GO

Here is the call:

SELECT dbo.AvgPrice(2), *
FROM [StockPrice]
where StockId = 2

0
 
LVL 13

Author Comment

by:Epsylon
ID: 7058908
Thanks for trying to help, but this is still not what I am looking for.

The function dbo.AvgPrice() works only on the table 'StockPrice'. MAX(), SUM(), COUNT(), AVG(), etc. (with proper parameters) work on every table.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7058937
Create a cursor and do what you want! What are your trying to do?
0
 
LVL 13

Author Comment

by:Epsylon
ID: 7059012
What I want is an aggregate function that results in for example a logical OR-ed or AND-ed result:

select doOR(intfield) from sometable
select doAND(intfield) from sometable

just like

select SUM(intfield) from sometable



So when I have this table:

intfield
--------------
4
6
20


Results:

select doOR(intfield) from sometable ===> 22
select doAND(intfield) from sometable ===> 4
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7059166
do you know how to do "logical OR-ed or AND-ed" in SQL?
0
 
LVL 13

Author Comment

by:Epsylon
ID: 7059188
OR: 4|6|20 = 22
AND: 4&6&20 = 4

SUM: 4+6+20 = 30
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7059282
i was able to do the OR-ed

DECLARE @A int
SET @A = 0

SELECT @A = @A | Field1
FROM  Table2

select @A
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7059283
and here comes the AND-ed:

DECLARE @A int
SET @A = 9999999

SELECT @A = @A & Field1
FROM  Table2

select @A
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7059296
CREATE FUNCTION dbo.ANDED( )
RETURNS int
AS
BEGIN
DECLARE @A int
SET @A = 9999999

SELECT @A = @A & Field1
FROM  Table2
 RETURN ( @A )
END
GO

CREATE FUNCTION dbo.ORED( )
RETURNS int
AS
BEGIN
DECLARE @A int
SET @A = 0

SELECT @A = @A | Field1
FROM  Table2
 RETURN ( @A )
END
GO

--Here is the call:

SELECT dbo.ANDED(), dbo.ORED()
0
 
LVL 13

Author Comment

by:Epsylon
ID: 7061631
Sorry to say, but these function are still table-specific...
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7061661
pass a tablename and a fieldname to the functions, create a query into your function and execute it from the function.
0
 
LVL 13

Author Comment

by:Epsylon
ID: 7066291
Sorry, but this can only work when the result of the query has only one record. When grouping of all records results in more than one records, it will go wrong.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7066511
I have not tried grouping with UDF.
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 100 total points
ID: 7109524
I have seen a SQL expert tonight who was talking about UDF and he confirmed me that there is no way to create your own function like SUM because UDF needs to be deterministic (everything must be know at compile time : table, where clause, ...)
0
 
LVL 13

Author Comment

by:Epsylon
ID: 7115086
Ok, thanks.

Consider this question closed   :)
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question