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.
| 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 ofONLY 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. |
 |
|
|
yukiever
Starting Member
13 Posts |
Posted - 2011-01-24 : 15:14:23
|
The labor hours.So for example if hours are 15,20,16,13Then 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.
|
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-01-24 : 15:40:51
|
| select avg(col1)from(select top 50 percent col1from yourTableorder by col1 asc) aJimEveryday I learn something that somebody else already knew |
 |
|
|
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 withmy 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 abcperformanceWHERE (sitename like '%gessner%' AND datename(weekday,transdate)='Friday' AND actualweather='2 Mostly Sunny' )GROUP BY datename(weekday,transdate),sitename,actualweatherORDER 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 col1from yourTableorder by col1 asc) aJimEveryday I learn something that somebody else already knew
|
 |
|
|
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 aGROUP BY Days,sitename,weatherORDER BY Average,Weather[/code]- LumbagoMy blog-> www.thefirstsql.com |
 |
|
|
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" |
 |
|
|
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 PublishingAnalytics and OLAP in SQLData and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL |
 |
|
|
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 PublishingAnalytics and OLAP in SQLData and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL
|
 |
|
|
yukiever
Starting Member
13 Posts |
Posted - 2011-01-26 : 13:05:34
|
Hi jimf,Thanks! I solved it.This is works great becauseI can change 50 to whatever percentage I want!-Wookquote: Originally posted by jimf select avg(col1)from(select top 50 percent col1from yourTableorder by col1 asc) aJimEveryday I learn something that somebody else already knew
|
 |
|
|
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.-Wookquote: 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 aGROUP BY Days,sitename,weatherORDER BY Average,Weather - LumbagoMy blog-> www.thefirstsql.com
|
 |
|
|
|
|
|
|
|