SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 get percentage
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

peace
Constraint Violating Yak Guru

404 Posts

Posted - 07/14/2013 :  08:46:33  Show Profile  Reply with Quote
Month Title Total
June A 1069
June B 1175

I would like the Title A to calculate as below:

Title A / Title B * 100 to get in percentage. Result should be as below:

Month Title Total
June A 91%
June B 1175

how can i do that?

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 07/14/2013 :  13:39:05  Show Profile  Reply with Quote

SELECT [Month],Title,
Total*100.0/SUM(Total) OVER (PARTITION BY [Month]) AS [Total%]
FROM Table


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3593 Posts

Posted - 07/14/2013 :  21:35:46  Show Profile  Reply with Quote
SELECT
	Month,
	Title,
	CASE 
		WHEN Title = 'A' THEN 
			100.0*Total/SUM(CASE WHEN Title=B THEN Total ELSE 0 END) OVER(PARTITION BY MONTH)
		ELSE Total
	END
FROM
	Tbl;

Edited by - James K on 07/14/2013 22:26:52
Go to Top of Page

peace
Constraint Violating Yak Guru

404 Posts

Posted - 07/14/2013 :  21:40:59  Show Profile  Reply with Quote
I tried James method but the result I get 0.

visakh16 method the total are incorrect.
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3593 Posts

Posted - 07/14/2013 :  22:26:21  Show Profile  Reply with Quote
I edited my previous post. See change in red
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 07/15/2013 :  00:37:21  Show Profile  Reply with Quote
quote:
Originally posted by peace

I tried James method but the result I get 0.

visakh16 method the total are incorrect.


so you just want percentage only if its Title A?

then you can do this small modification


SELECT [Month],Title,
CASE WHEN Title='A'  THEN Total*100.0/SUM(CASE WHEN Title='B' THEN Total ELSE 0 END) OVER (PARTITION BY [Month]) ELSE Total END AS [Total]
FROM Table



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000