Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Tsql function

Author  Topic 

aakcse
Aged Yak Warrior

570 Posts

Posted - 2009-12-04 : 08:17:09
I want to write a qurey(a veiw or function) to give all the below

Lowest value of a column -- can be taken from min(column_name)
Median value -- is there any function for this?
Highest value--can be taken from max(column_name)

Average value --(Total Sum/Total Item Count)--is there any direct function for this

Item count of values less than 0
Item count of values greater than 0
Item count of all line items
Item count of values equal to 0 or "blank"

I want to write a function(or a view can also do) which gives me all the above details, can any one help me to start with.

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-12-04 : 08:32:58
Go to a great website. www.sqlteam.com, go to forums and click on "search". Type in "median value" and then open up the post from yesterday.


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

rajdaksha
Aged Yak Warrior

595 Posts

Posted - 2009-12-04 : 08:33:45
HI

Median try this..


CREATE TABLE [calculate] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[year] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[q8] [float] NOT NULL ,
[q9] [float] NOT NULL ,
CONSTRAINT [PK_calculate] PRIMARY KEY CLUSTERED
(
[ID]
) ON [PRIMARY]
) ON [PRIMARY]
GO


INSERT INTO [dbo].[calculate]([year], [q8], [q9])
VALUES('First', 2, 3);
INSERT INTO [dbo].[calculate]([year], [q8], [q9])
VALUES('First', 4, 2);
INSERT INTO [dbo].[calculate]([year], [q8], [q9])
VALUES('Second', 2, 6);
INSERT INTO [dbo].[calculate]([year], [q8], [q9])
VALUES('Third', 1, 4);
INSERT INTO [dbo].[calculate]([year], [q8], [q9])
VALUES('Third', 2, 3);
INSERT INTO [dbo].[calculate]([year], [q8], [q9])
VALUES('Third',9, 1);


SELECT AVG([q8]) FROM
(
SELECT [q8] FROM (
SELECT TOP 1 [q8] = [q8] * 1.0 FROM
(
SELECT TOP 50 PERCENT [q8]
FROM [CALCULATE] ORDER BY [q8]
) tab_a
ORDER BY 1 DESC
) tab_1
UNION ALL
SELECT [q8] FROM (
SELECT TOP 1 [q8] = [q8] * 1.0 FROM
(
SELECT TOP 50 PERCENT [q8]
FROM [CALCULATE] ORDER BY [q8] DESC
) tab_b
ORDER BY 1
) tab_2
) median



-------------------------
R...
Go to Top of Page
   

- Advertisement -