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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

 SQL Server Forums Profile | Active Topics | Members | Search | Forum FAQ Register Now and get your question answered!
 All Forums  SQL Server 2008 Forums  Transact-SQL (2008)  Query Sum Reply to Topic  Printer Friendly
Author  Topic

mrivero1961
Starting Member

7 Posts

 Posted - 05/25/2013 :  13:31:25 Hi all, hope in your help.This is my table:``` +----+--------+--------+ | 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 | +----+--------+--------+ ```I need this output:``` +--------+--------------------+ | field1 | field2 | +--------+--------------------+ | A2 | 19.230769230769200 | +--------+--------------------+ | B2 | 23,076923076923100 | +--------+--------------------+ | C2 | 26,923076923076900 | +--------+--------------------+ | D2 | 30,769230769230800 | +--------+--------------------+ ```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) :``` A = 5/26 * 100 = 19 B = 6/26 * 100 = 23 C = 7/26 * 100 = 26 D = 8/26 * 100 = 30 SELECT field1, field2/Sum(field2)*100 as field2 FROM `tbl_t` WHERE 1 AND field1 IN ('A2', 'B2', 'C2', 'D2'); +--------+--------------------+ | field1 | field2 | +--------+--------------------+ | A2 | 19.230769230769234 | +--------+--------------------+ ```But the ouput is not what I want, can you help me?Thank youAny help would be greatly appreciated.

MuMu88
Aged Yak Warrior

549 Posts

 Posted - 05/25/2013 :  20:15:26 ``` DECLARE @Tbl_t TABLE(ID INT, field1 NVARCHAR(2), field2 INT) INSERT INTO @Tbl_t SELECT 1, 'A1', 7 union all SELECT 2, 'B1', 9 union all SELECT 3, 'C1', 0 union all SELECT 4, 'D1', 3 union all SELECT 5, 'A2', 5 union all SELECT 6, 'B2', 6 union all SELECT 7, 'C2', 7 union all SELECT 8, 'D2', 8; WITH CTE AS (SELECT CASE WHEN (field1 IN ('A2', 'B2', 'C2', 'D2')) THEN 1 ELSE 2 END as Grp, * from @tbl_t) SELECT field1, CAST(CAST(field2*100 as DECIMAL(20,15))/(SELECT SUM(field2) from CTE where Grp = 1 Group by Grp) as DECIMAL(20,15)) as Average FROM CTE WHERE Grp = 1; ```

mrivero1961
Starting Member

7 Posts

 Posted - 05/26/2013 :  17:20:28 quote:Originally posted by MuMu88``` DECLARE @Tbl_t TABLE(ID INT, field1 NVARCHAR(2), field2 INT) INSERT INTO @Tbl_t SELECT 1, 'A1', 7 union all SELECT 2, 'B1', 9 union all SELECT 3, 'C1', 0 union all SELECT 4, 'D1', 3 union all SELECT 5, 'A2', 5 union all SELECT 6, 'B2', 6 union all SELECT 7, 'C2', 7 union all SELECT 8, 'D2', 8; WITH CTE AS (SELECT CASE WHEN (field1 IN ('A2', 'B2', 'C2', 'D2')) THEN 1 ELSE 2 END as Grp, * from @tbl_t) SELECT field1, CAST(CAST(field2*100 as DECIMAL(20,15))/(SELECT SUM(field2) from CTE where Grp = 1 Group by Grp) as DECIMAL(20,15)) as Average FROM CTE WHERE Grp = 1; ```thanks so much

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

 Posted - 05/27/2013 :  01:14:03 this is the explanationhttp://beyondrelational.com/modules/2/blogs/70/posts/10825/beware-of-implicit-conversions.aspx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
Topic
 Reply to Topic  Printer Friendly Jump To: Select Forum General SQL Server Forums       New to SQL Server Programming       New to SQL Server Administration       Script Library       Data Corruption Issues       Database Design and Application Architecture SQL Server 2012 Forums       Transact-SQL (2012)       SQL Server Administration (2012)       SSIS and Import/Export (2012)       Analysis Server and Reporting Services (2012)       Replication (2012)       Availability Groups and DR (2012)       Other SQL Server 2012 Topics SQL Server 2008 Forums       Transact-SQL (2008)       SQL Server Administration (2008)       SSIS and Import/Export (2008)       High Availability (2008)       Replication (2008)       Analysis Server and Reporting Services (2008)       Other SQL Server 2008 Topics SQL Server 2005 Forums       Transact-SQL (2005)       SQL Server Administration (2005)       .NET Inside SQL Server (2005)       SSIS and Import/Export (2005)       Service Broker (2005)       Replication (2005)       High Availability (2005)       Analysis Server and Reporting Services (2005)       Express Edition and Compact Edition (2005)       Other SQL Server Topics (2005) SQL Server 2000 Forums       SQL Server Development (2000)       SQL Server Administration (2000)       Import/Export (DTS) and Replication (2000)       Transact-SQL (2000)       Analysis Services (2000)       MSDE (2000) Development Tools       ASP.NET       Reporting Services Development       Other Development Tools Site Related Forums       Site Related Discussions       Article Discussion       Poll Discussion       The Yak Corral Other Forums       SQL Server 6.5 \ SQL Server 7.0       Other Topics       MS Access       ClearTrace Support Forum Old Forums       CLOSED - General SQL Server       CLOSED - SQL Server 2005/Yukon  -------------------- Home Active Topics Frequently Asked Questions Member Information Search Page
 SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC