Can you reuse already determined calculations in select statements? e.g

If I have this valid select part of a statement to work out a distribution of events :

            SUM(CASE WHEN CreatedDate BETWEEN Dateadd(month, - 1, getdate()) AND getdate() THEN 1 ELSE 0 END) AS Period,             COUNT(LastClientCareCall.ContactId) AS NumberOfClients

I need to add a percentage calculation and my instinct is to append to the end something like ",100/NumberOfClients*Period as PercentageOfPeriod" but this is of course not possible.  I could obviously reuse all my formulas and make a very long winded "100/COUNT(LastClientCareCall.ContactId)*SUM(CASE WHEN CreatedDate BETWEEN Dateadd(month, - 1, getdate()) AND getdate() THEN 1 ELSE 0 END)" but wondered if there was a mechanism for using the output of the previously used alias's to achieve something more tidy?

Thanks in advance
Who is Participating?

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

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.

Jim HornMicrosoft SQL Server Data DudeCommented:
afaik nope, as the SELECT clause processes as a whole and not left-to-right where the left columns are retained in memory before the right ones are processed.  The main workaround would be to have your needed calculations in a subquery, and the ones that use them in a main query.   Something like..

SELECT a, b, the_count / the_sum as the_percent
   SELECT a, b, COUNT(b) as the_count, SUM(b) as the_sum
   FROM some_table) one

Open in new window

I'm not quite understanding your calculations, so if you can give a data mockup maybe I'll be able to provide custom SQL to match.

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
dgloverukAuthor Commented:
Hi Jim,
That will be fine, I will nest the 1st query inside and then work with the alias names as you demonstrate.
Thank you!
Jim HornMicrosoft SQL Server Data DudeCommented:
Thanks for the grade.  Good luck with your stuff.  -Jim
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

From novice to tech pro — start learning today.