| Author |
Topic |
|
ConradK
Posting Yak Master
140 Posts |
Posted - 2010-08-12 : 10:31:02
|
| I've got four columns, a, b, c, and d. They look look like thisItem, a, b, c, dApple, 1, 0, 29, 3Banana, 4, 0 , 4, 100Carrot, 39,3,2,0Derringer, 3, 3, 5, 2I want to find an average value between them where the highest value is dropped out. The results would look like thisApple, 1.33Banana, 2.66Carrot, 1.66Derringer, 2.66Thoughts? |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-08-12 : 10:42:59
|
One way...--Sample Datadeclare @t table (Item varchar(20),a int,b int, c int, d int)insert @tselect 'Apple', 1, 0, 29, 3union all select 'Banana', 4, 0 , 4, 100union all select 'Carrot', 39,3,2,0union all select 'Derringer', 3, 3, 5, 2--Queryselect Item, ((t.a + t.b + t.c + t.d - t.[Max_of_a_to_d]) * 1.0) / 3from(select x.Item, x.a, x.b, x.c, x.d, [Max_of_a_to_d] = case when x.a is not null and (x.a >= x.b or x.b is null) and (x.a >= x.c or x.c is null) and (x.a >= x.d or x.d is null) then x.a when x.b is not null and (x.b >= x.a or x.a is null) and (x.b >= x.c or x.c is null) and (x.b >= x.d or x.d is null) then x.b when x.c is not null and (x.c >= x.a or x.a is null) and (x.c >= x.b or x.b is null) and (x.c >= x.d or x.d is null) then x.c when x.d is not null and (x.d >= x.a or x.a is null) and (x.d >= x.b or x.b is null) and (x.d >= x.c or x.c is null) then x.d else null endfrom @t x) t--ResultItem Avg-------------------- ---------------------------------------Apple 1.333333Banana 2.666666Carrot 1.666666Derringer 2.666666 Fine the maximum value among columns - refer this thread from MVJhttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=86906 |
 |
|
|
ConradK
Posting Yak Master
140 Posts |
Posted - 2010-08-12 : 10:46:47
|
| woa. Yeah, I was afraid it would need a complicated if/then stucture. |
 |
|
|
ConradK
Posting Yak Master
140 Posts |
Posted - 2010-08-12 : 10:46:58
|
| Thank you much! |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-08-12 : 10:47:50
|
quote: Originally posted by ConradK Thank you much!
Np. Actual credit goes to MVJ. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2010-08-12 : 10:58:59
|
...maybe less effecient, but simpler..DECLARE @t table(Item varchar(20), a int, b int, c int, d int)INSERT INTO @t (Item, a, b, c, d)SELECT 'Apple' , 1, 0, 29, 3 UNION ALLSELECT 'Banana' , 4, 0, 4, 100 UNION ALLSELECT 'Carrot' , 39,3,2,0 UNION ALLSELECT 'Derringer' , 3, 3, 5, 2SELECT Item, (SUM(Amt) - MAX(Amt))/(COUNT(*) * 1.00 - 1)FROM ( SELECT Item, a AS Amt FROM @t UNION ALL SELECT Item, b AS Amt FROM @t UNION ALL SELECT Item, c AS Amt FROM @t UNION ALL SELECT Item, d AS Amt FROM @t) AS XXXGROUP BY Item Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2010-08-12 : 11:05:06
|
...now if you had actual Normalized data (which should be everyones goal)...this is a snapDECLARE @t table(Item varchar(20), Amt int)INSERT INTO @t (Item, Amt)SELECT 'Apple' , 1 UNION ALLSELECT 'Apple' , 0 UNION ALLSELECT 'Apple' , 29 UNION ALLSELECT 'Apple' , 3 UNION ALLSELECT 'Banana' , 4 UNION ALLSELECT 'Banana' , 0 UNION ALLSELECT 'Banana' , 4 UNION ALLSELECT 'Banana' , 100 UNION ALLSELECT 'Carrot' , 39 UNION ALLSELECT 'Carrot' , 3 UNION ALLSELECT 'Carrot' , 2 UNION ALLSELECT 'Carrot' , 0 UNION ALLSELECT 'Derringer' , 3 UNION ALLSELECT 'Derringer' , 3 UNION ALLSELECT 'Derringer' , 5 UNION ALLSELECT 'Derringer' , 2SELECT Item, (SUM(Amt) - MAX(Amt))/(COUNT(*) * 1.00 - 1)FROM @tGROUP BY Item Go forth and Normalize your dataBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
ConradK
Posting Yak Master
140 Posts |
Posted - 2010-08-12 : 11:14:59
|
| i see what you did there! I actually have the data elsewhere normalized. But alas I am a fledgling novice and do not think in normalized terms. I think I can work it from here!COol!!!! |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
ConradK
Posting Yak Master
140 Posts |
Posted - 2010-08-12 : 11:43:39
|
| Hey man. I'm learning as I go here. Your looking at a man with NO formal training, just trying to do what my bosses ask me to with the tools and knowledge I have. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
ConradK
Posting Yak Master
140 Posts |
Posted - 2010-08-12 : 11:55:04
|
| Thanks for the continued support! Much appreciated!!!! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-08-12 : 13:07:56
|
Brett, I think you missed one column. The "type" column where values {a, b, c, d} are stored, to know which value is attached to which type. N 56°04'39.26"E 12°55'05.63" |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2010-08-12 : 14:23:28
|
quote: Originally posted by Peso Brett, I think you missed one column. The "type" column where values {a, b, c, d} are stored, to know which value is attached to which type. N 56°04'39.26"E 12°55'05.63"
Is there any Aquivit left?What are you taking about?Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-08-12 : 15:56:12
|
For Apple, how can you tell which value of {0, 1, 3, 29} belongs to "b"?In OP original design, it's very easy to see that column b has value 0.If you are going to normalize the original design, it should look more like this.DECLARE @t TABLE ( Item VARCHAR(20) NOT NULL, [Type] CHAR(1) NOT NULL, Amt INT NOT NULL )INSERT @t ( Item, [Type], Amt )SELECT 'Apple', 'a', 1 UNION ALLSELECT 'Apple', 'b', 0 UNION ALLSELECT 'Apple', 'c', 29 UNION ALLSELECT 'Apple', 'd', 3 UNION ALLSELECT 'Banana', 'a', 4 UNION ALLSELECT 'Banana', 'b', 0 UNION ALLSELECT 'Banana', 'c', 4 UNION ALLSELECT 'Banana', 'd', 100 UNION ALLSELECT 'Carrot', 'a', 39 UNION ALLSELECT 'Carrot', 'b', 3 UNION ALLSELECT 'Carrot', 'c', 2 UNION ALLSELECT 'Carrot', 'd', 0 UNION ALLSELECT 'Derringer', 'a', 3 UNION ALLSELECT 'Derringer', 'b', 3 UNION ALLSELECT 'Derringer', 'c', 5 UNION ALLSELECT 'Derringer', 'd', 2 because records don't have an order in a table, unless you put a logical order onto them.It doesn't change your query in any way, but other queries may look for a certain type. N 56°04'39.26"E 12°55'05.63" |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-08-12 : 17:53:21
|
quote: Originally posted by ConradK I've got four columns, a, b, c, and d. They look look like thisItem, a, b, c, dApple, 1, 0, 29, 3Banana, 4, 0 , 4, 100Carrot, 39,3,2,0Derringer, 3, 3, 5, 2I want to find an average value between them where the highest value is dropped out. The results would look like thisApple, 1.33Banana, 2.66Carrot, 1.66Derringer, 2.66Thoughts?
SQL Server 2008 approach:SELECT Item, (a + b + c + d - max_value) / 3.0 AS average FROM table_name CROSS APPLY (SELECT MAX(value) FROM (VALUES (a), (b), (c), (d) ) D(value) ) D(max_value); ______________________ |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-08-13 : 10:03:17
|
quote: Originally posted by ms65g
quote: Originally posted by ConradK I've got four columns, a, b, c, and d. They look look like thisItem, a, b, c, dApple, 1, 0, 29, 3Banana, 4, 0 , 4, 100Carrot, 39,3,2,0Derringer, 3, 3, 5, 2I want to find an average value between them where the highest value is dropped out. The results would look like thisApple, 1.33Banana, 2.66Carrot, 1.66Derringer, 2.66Thoughts?
SQL Server 2008 approach:SELECT Item, (a + b + c + d - max_value) / 3.0 AS average FROM table_name CROSS APPLY (SELECT MAX(value) FROM (VALUES (a), (b), (c), (d) ) D(value) ) D(max_value); ______________________
Another methodDECLARE @t table(Item varchar(20), a int, b int, c int, d int)INSERT INTO @t (Item, a, b, c, d)SELECT 'Apple' , 1, 0, 29, 3 UNION ALLSELECT 'Banana' , 4, 0, 4, 100 UNION ALLSELECT 'Carrot' , 39,3,2,0 UNION ALLSELECT 'Derringer' , 3, 3, 5, 2select item, ( select (sum(n)-max(n))/(count(n)*1.0-1) from ( values(a),(b),(c),(d) ) as t(n) ) as maximum_value from @t Also refer thishttp://beyondrelational.com/blogs/madhivanan/archive/2010/08/02/values-clause-in-sql-server-2008.aspxMadhivananFailing to plan is Planning to fail |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-08-13 : 11:22:57
|
quote: Originally posted by X002548 I'll go look this up...but huh???What does that reference?select item, ( select (sum(n)-max(n))/(count(n)*1.0-1) from ( values(a),(b),(c),(d) ) as t(n) ) as maximum_value from @t Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam
What did you mean?MadhivananFailing to plan is Planning to fail |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-08-13 : 11:23:08
|
The values clause is a new way to write "select a as n union all select b ..." N 56°04'39.26"E 12°55'05.63" |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-08-13 : 11:25:29
|
quote: Originally posted by madhivanan
quote: Originally posted by ms65g
quote: Originally posted by ConradK I've got four columns, a, b, c, and d. They look look like thisItem, a, b, c, dApple, 1, 0, 29, 3Banana, 4, 0 , 4, 100Carrot, 39,3,2,0Derringer, 3, 3, 5, 2I want to find an average value between them where the highest value is dropped out. The results would look like thisApple, 1.33Banana, 2.66Carrot, 1.66Derringer, 2.66Thoughts?
SQL Server 2008 approach:SELECT Item, (a + b + c + d - max_value) / 3.0 AS average FROM table_name CROSS APPLY (SELECT MAX(value) FROM (VALUES (a), (b), (c), (d) ) D(value) ) D(max_value); ______________________
Another methodDECLARE @t table(Item varchar(20), a int, b int, c int, d int)INSERT INTO @t (Item, a, b, c, d)SELECT 'Apple' , 1, 0, 29, 3 UNION ALLSELECT 'Banana' , 4, 0, 4, 100 UNION ALLSELECT 'Carrot' , 39,3,2,0 UNION ALLSELECT 'Derringer' , 3, 3, 5, 2select item, ( select (sum(n)-max(n))/(count(n)*1.0-1) from ( values(a),(b),(c),(d) ) as t(n) ) as maximum_value from @t Also refer thishttp://beyondrelational.com/blogs/madhivanan/archive/2010/08/02/values-clause-in-sql-server-2008.aspxMadhivananFailing to plan is Planning to fail
Another methodselect item, (a + b + c + d - case when a >=all (select b union select c union select d) then a when b >=all (select c union select d) then b when c >= d then c else d end) / 3.0 as avaragefrom table_name ______________________ |
 |
|
|
Next Page
|
|
|