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.
aritaskyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

PortletPaulEE Topic AdvisorCommented:
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

Open in new window


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 |

Open in new window


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)
;

Open in new window


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

Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trial
PortletPaulEE Topic AdvisorCommented:
aritasky,

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

That is supplied by the provided query.
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.