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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 SQL-Average of lower half

Author  Topic 

yukiever
Starting Member

13 Posts

Posted - 2011-01-24 : 13:52:29
Hi,
This seems to be hard for me.
I have some rows in SQL SERVER 2008 with column name 'labor hours'
in this column, there are values like
{'22','21','19','17','28','25'}

Using SELECT statement I need to get the average of
ONLY THE LOWER HALF.

I use built in function for getting average.
AVG('labor hours')

But don't know how to get the lower half.
Can someone plz help me with this?

Thanks in advance!

-Wook

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2011-01-24 : 14:25:58
What determines "the lower half"?





Poor planning on your part does not constitute an emergency on my part.
Go to Top of Page

yukiever
Starting Member

13 Posts

Posted - 2011-01-24 : 15:14:23
The labor hours.
So for example if hours are 15,20,16,13
Then take away 16 and 20.

15 and 13 is left.
Then the average of this is 14.
I need to do this in SQL SELECT statement somehow...
If I use AVG() it gives me the average of all the records.
Needs to be sorted and then take average of the lower half records.

Thanks!



quote:
Originally posted by dataguru1971

What determines "the lower half"?





Poor planning on your part does not constitute an emergency on my part.


Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-01-24 : 15:40:51
select avg(col1)

from
(
select top 50 percent col1
from yourTable
order by col1 asc
) a

Jim

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

yukiever
Starting Member

13 Posts

Posted - 2011-01-24 : 16:21:24
Hi jimf!
Thanks for the post.
It works great individually.
BUT I am having problem with combining it with
my REAL SQL statment which is:


SELECT sitename as 'Site Name',datename(weekday,transdate) as 'Days',
actualweather as 'Weather',TOP 50 PERCENT ROUND(AVG(statlh),0) as 'Average' FROM abcperformance
WHERE (sitename like '%gessner%' AND datename(weekday,transdate)='Friday' AND actualweather='2 Mostly Sunny' )
GROUP BY datename(weekday,transdate),sitename,actualweather
ORDER BY Average,actualweather


How can I use that with my command?
Thank you in advance!


quote:
Originally posted by jimf

select avg(col1)

from
(
select top 50 percent col1
from yourTable
order by col1 asc
) a

Jim

Everyday I learn something that somebody else already knew

Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2011-01-25 : 03:37:54
[code]
SELECT Sitename, Days, Weather, ROUND(AVG(statlh),0) as 'Average'
FROM (
SELECT TOP 50 PERCENT sitename,datename(weekday,transdate) as 'Days',
actualweather as 'Weather', statlh
FROM abcperformance
WHERE (sitename like '%gessner%' AND datename(weekday,transdate)='Friday' AND actualweather='2 Mostly Sunny' )
ORDER BY statlh
) as a
GROUP BY Days,sitename,weather
ORDER BY Average,Weather[/code]

- Lumbago

My blog-> www.thefirstsql.com
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2011-01-25 : 04:35:34
I would use a CROSS APPLY to calculate the "Average of lower half" for each group.



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2011-01-25 : 15:24:28
Here is a solution with no proprietary code.

CREATE TABLE Foobar (i INTEGER NOT NULL);
INSERT INTO Foobar VALUES (), ...;

SELECT AVG(i) AS lower_i_avg
FROM (SELECT i, ROW_NUMBER () OVER (ORDER BY i DESC),
COUNT(*) OVER()/2
FROM Foobar)
AS F1(i, seq, cut)
WHERE seq >= cut;


Did you know about single quotes, double quotes and square brackets? There is no need to write dialect. Did you notice that your query is full of constants? There is no need to pull them out of the table; just give the constant columns a name.

SELECT site_name, 'Friday' AS weekday_name, 'Mostly Sunny' AS actual_weather_txt,
(..) AS lower_statlh_avg
FROM ABC_Performance
WHERE site_name LIKE '%GESSNER%'
AND DATENAME(WEEKDAY, trans_date)= 'Friday'
AND actual_weather_txt = 'Mostly Sunny'
GROUP BY site_name;


--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page

yukiever
Starting Member

13 Posts

Posted - 2011-01-26 : 13:04:28
Hi jcelko,
Thanks a lot. Learned a lot..!



quote:
Originally posted by jcelko

Here is a solution with no proprietary code.

CREATE TABLE Foobar (i INTEGER NOT NULL);
INSERT INTO Foobar VALUES (), ...;

SELECT AVG(i) AS lower_i_avg
FROM (SELECT i, ROW_NUMBER () OVER (ORDER BY i DESC),
COUNT(*) OVER()/2
FROM Foobar)
AS F1(i, seq, cut)
WHERE seq >= cut;


Did you know about single quotes, double quotes and square brackets? There is no need to write dialect. Did you notice that your query is full of constants? There is no need to pull them out of the table; just give the constant columns a name.

SELECT site_name, 'Friday' AS weekday_name, 'Mostly Sunny' AS actual_weather_txt,
(..) AS lower_statlh_avg
FROM ABC_Performance
WHERE site_name LIKE '%GESSNER%'
AND DATENAME(WEEKDAY, trans_date)= 'Friday'
AND actual_weather_txt = 'Mostly Sunny'
GROUP BY site_name;


--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL


Go to Top of Page

yukiever
Starting Member

13 Posts

Posted - 2011-01-26 : 13:05:34
Hi jimf,
Thanks! I solved it.
This is works great because
I can change 50 to whatever percentage I want!

-Wook

quote:
Originally posted by jimf

select avg(col1)

from
(
select top 50 percent col1
from yourTable
order by col1 asc
) a

Jim

Everyday I learn something that somebody else already knew

Go to Top of Page

yukiever
Starting Member

13 Posts

Posted - 2011-01-26 : 13:06:18
Hi Lumbago,
This works too. I twisted a little bit.
Thanks for the reply.

-Wook


quote:
Originally posted by Lumbago


SELECT Sitename, Days, Weather, ROUND(AVG(statlh),0) as 'Average'
FROM (
SELECT TOP 50 PERCENT sitename,datename(weekday,transdate) as 'Days',
actualweather as 'Weather', statlh
FROM abcperformance
WHERE (sitename like '%gessner%' AND datename(weekday,transdate)='Friday' AND actualweather='2 Mostly Sunny' )
ORDER BY statlh
) as a
GROUP BY Days,sitename,weather
ORDER BY Average,Weather


- Lumbago

My blog-> www.thefirstsql.com

Go to Top of Page
   

- Advertisement -