Unix Technical Forum

how to write an aggregate function which can used like MAX(filedname)

This is a discussion on how to write an aggregate function which can used like MAX(filedname) within the SQL Server forums, part of the Microsoft SQL Server category; --> Recently, I will write a database application to get the median value according to the grouped condition. maybe, just ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 06:14 PM
William Jiang
 
Posts: n/a
Default how to write an aggregate function which can used like MAX(filedname)

Recently, I will write a database application to get the median value
according to the grouped condition.

maybe, just like the following.

SELECT Max(a1) MaxValue, Median(a1) MedianValue FROM test_table

Any suggestion?


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 06:14 PM
John Bell
 
Posts: n/a
Default Re: how to write an aggregate function which can used like MAX(filedname)

Hi

Check out this previous post:

http://tinyurl.com/q59j

John

"William Jiang" <williamj@qd.lucent.com> wrote in message
news:bm0gon$i2h@netnews.proxy.lucent.com...
> Recently, I will write a database application to get the median value
> according to the grouped condition.
>
> maybe, just like the following.
>
> SELECT Max(a1) MaxValue, Median(a1) MedianValue FROM test_table
>
> Any suggestion?
>
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 06:14 PM
David Portas
 
Posts: n/a
Default Re: how to write an aggregate function which can used like MAX(filedname)

A Google search of this group will give you several alternative median
functions depending on the exact form of median you want: Here's one
example:

CREATE TABLE SomeValues (keyx CHAR(1) PRIMARY KEY, valuex INTEGER NOT NULL)

INSERT INTO SomeValues VALUES ('A',1)
INSERT INTO SomeValues VALUES ('B',2)
INSERT INTO SomeValues VALUES ('C',3)
INSERT INTO SomeValues VALUES ('D',4)
INSERT INTO SomeValues VALUES ('E',5)

SELECT S1.valuex AS median
FROM SomeValues AS S1, SomeValues AS S2
GROUP BY S1.valuex
HAVING SUM(CASE WHEN S2.valuex <= S1.valuex
THEN 1 ELSE 0 END)
>= ((COUNT(*) + 1) / 2)

AND SUM(CASE WHEN S2.valuex >= S1.valuex
THEN 1 ELSE 0 END)
>= (COUNT(*)/2 + 1)


SQL For Smarties (Celko) has a whole chapter on how to calculate Medians in
SQL. Other statistics are also covered.
http://tinyurl.com/hntc


--
David Portas
------------
Please reply only to the newsgroup
--

"William Jiang" <williamj@qd.lucent.com> wrote in message
news:bm0gon$i2h@netnews.proxy.lucent.com...
> Recently, I will write a database application to get the median value
> according to the grouped condition.
>
> maybe, just like the following.
>
> SELECT Max(a1) MaxValue, Median(a1) MedianValue FROM test_table
>
> Any suggestion?
>
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:15 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com