[SQL Server] Query Sum |
Posted: 25 May 2013 04:32 AM PDT Hi all, hope in your help.This is my table:[code]+----+--------+--------+| id | field1 | field2 |+----+--------+--------+| 1 | A1 | 7 || 2 | B1 | 9 || 3 | C1 | 0 || 4 | D1 | 3 || 5 | A2 | 5 || 6 | B2 | 6 || 7 | C2 | 7 || 8 | D2 | 8 |+----+--------+--------+[/code]I need this output:[code]+--------+--------------------+| field1 | field2 |+--------+--------------------+| A2 | 19.230769230769200 |+--------+--------------------+| B2 | 23,076923076923100 |+--------+--------------------+| C2 | 26,923076923076900 |+--------+--------------------+| D2 | 30,769230769230800 |+--------+--------------------+[/code]and tried this query where calculate the value of single field1 (5,6,7,8) divided by the sum of field2 equal to A1, B2, C2 and D2 (26) :[code]A = 5/26 * 100 = 19B = 6/26 * 100 = 23C = 7/26 * 100 = 26D = 8/26 * 100 = 30SELECT field1, field2/Sum(field2)*100 as field2FROM `tbl_t`WHERE 1AND field1 IN ('A2', 'B2', 'C2', 'D2');+--------+--------------------+| field1 | field2 |+--------+--------------------+| A2 | 19.230769230769234 |+--------+--------------------+[/code]But the ouput is not what I want, can you help me?Thank youAny help would be greatly appreciated. |
You are subscribed to email updates from SQLServerCentral / SQL Server 2008 / SQL Server Newbies To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |
No comments:
Post a Comment