# 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
(INV_HEADER.inv_date >= DATEADD(MONTH,(MONTH(GETDATE()) - 1) / 6 * 12 - 6, CAST(CAST(YEAR(GETDATE()) AS varchar) AS DATE)))
``````

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.
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

freelancerCommented:
It is sometimes true that the more efficient way to do this exists before your query has been run.

For example you could output the total sales in that year on each row using SUM() OVER()

Try this
``````select
SalesYear, SalesMonth, TotalSales, (TotalSales*100.00 / sum(TotalSales) over() ) as MnthPct
from qry1
``````

gives this result:
``````| SalesYear | SalesMonth | TotalSales |            MnthPct |
|-----------|------------|------------|--------------------|
|      2014 |       July |     496386 |  9.737715742881743 |
|      2014 |     August |     433434 |  8.502772208120707 |
|      2014 |  September |     448197 |   8.79238129764411 |
|      2014 |    October |     466834 |   9.15798751599049 |
|      2014 |   November |     343526 |  6.739026762014226 |
|      2014 |   December |     353906 |  6.942653555298309 |
|      2015 |    January |     262129 |   5.14224351606582 |
|      2015 |   February |     390929 |  7.668942068569655 |
|      2015 |      March |     481230 |  9.440397084017238 |
|      2015 |      April |     451320 |  8.853645890652412 |
|      2015 |        May |     411398 |  8.070487042724942 |
|      2015 |       June |     405647 |  7.957668382977663 |
|      2015 |       July |     152625 | 2.9940789330426845 |
``````

from this data:
``````CREATE TABLE Qry1
([SalesYear] int, [SalesMonth] varchar(9), [TotalSales] numeric)
;

INSERT INTO Qry1
([SalesYear], [SalesMonth], [TotalSales])
VALUES
(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)
;
``````

see: http://sqlfiddle.com/#!3/791da/2
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

freelancerCommented:

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

That is supplied by the provided query.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.