Link to home
Create AccountLog in
Avatar of aritasky
aritasky

asked on

Calculate Percentage In MSSQL

Hi Experts,

I have the following query which returns sales figures by month for the current financial year (July-June).  In addition to that, I'd like to calculate percentage for each month based on total sales.  How would I do this in MSSQL2008?

SELECT  YEAR(INV_HEADER.inv_date) as SalesYear, DATENAME(MONTH,INV_HEADER.inv_date) as SalesMonth, SUM(INV_HEADER.total_ar_dollars) As TotalMonthSales
FROM INV_HEADER   
WHERE (INV_HEADER.locked = 'Y') and (INV_HEADER.inv_flag in ('I','C')) and (INV_HEADER.updated_to_ar = 'Y') and 
(INV_HEADER.inv_date >= DATEADD(MONTH,(MONTH(GETDATE()) - 1) / 6 * 12 - 6, CAST(CAST(YEAR(GETDATE()) AS varchar) AS DATE)))
and (INV_HEADER.inv_date <= GETDATE()) and (INV_HEADER.ar_inv_type not in ('O','A'))
GROUP BY YEAR(INV_HEADER.inv_date), DATENAME(MONTH,INV_HEADER.inv_date), DATEPART(MONTH,INV_HEADER.inv_date)
ORDER BY YEAR(INV_HEADER.inv_date), DATEPART(MONTH,INV_HEADER.inv_date)

Open in new window


Example Results

SalesYear      SalesMonth      TotalSales
2014              July                      496385.97
2014             August              433433.92
2014             September      448197.32
2014            October              466833.78
2014            November      343525.91
2014            December      353906.41
2015            January              262128.61
2015           February              390929.18
2015           March              481230.28
2015           April                      451320.43
2015           May                      411398.36
2015           June                      405647.44
2015           July                      152624.64

Any help appreciated.
ASKER CERTIFIED SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
aritasky,

Hi. Do you still need help with this?
>>" I'd like to calculate percentage for each month based on total sales"

That is supplied by the provided query.