March 2, 2012 at 2:56 pm
I have to calculate the percentage of revenue for each of the products indicated in the code below. Unfortunately when I execute the code below I get zero. Any help will be appreciated.
SELECT ((A.TRADE/A.TOTAL)*100) AS Trade,((A.CREDIT/A.TOTAL)*100) AS Credit,((A.OTHER/A.TOTAL)*100) AS Other
from(
Select SUM(Trade) as Trade, SUM(Credit) AS Credit, SUM(Others) AS Other, SUM(Trade+Credit+Others) as Total
From dbo.GL_FINANCE_FINAL_TABLE) A
ORDER BY ((A.TRADE/A.TOTAL)*100)
Results:
Trade Credit Other
0 0 0
March 2, 2012 at 3:01 pm
Replace A.TOTAL with A.TOTAL*1.00 to avoid an integer divsion (usually resulting in a zero value in such a scenario).
Example:
SELECT 1/2*100, 1/(2*1.00)*100
March 4, 2012 at 5:17 pm
Sorry, post withdrawn...
--Jeff Moden
Change is inevitable... Change for the better is not.
March 4, 2012 at 5:22 pm
LutzM (3/2/2012)
Replace A.TOTAL with A.TOTAL*1.00 to avoid an integer divsion (usually resulting in a zero value in such a scenario).Example:
SELECT 1/2*100, 1/(2*1.00)*100
Just an idea, Lutz. Move the *100 and give it the decimal place so you only need to do 1 multiplication instead of 2.
SELECT 1*100.0/2, 100.0*1/2
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply