TSQL CTE Needed Percentage of Sum...

Last Post 10 Nov 2011 12:30 AM by Eralper. 2 Replies.
AddThis - Bookmarking and Sharing Button
Author Messages
New Member
New Member

07 Apr 2011 08:12 AM
Hi, thought I am R&Ding this on my own - thought one of you experts would have a quick answer.
Basically, I want to calculate the values from a table - sum(myval), and get a percentage for each.  I think you can use a CTE, but not exactly sure how to construct it.

I want something that gets me:

select sum(myval1), myval2, myval1/(sum(myval1)*100 from table.

New Member
New Member

07 Apr 2011 12:36 PM
I basically figured it out...I'll try to post it back soon..
New Member
New Member

10 Nov 2011 12:30 AM
Hi SQL Jr,
Please try the following CTE script

with cte as (
    select sum(SubTotal) GrandTotal from sales.SalesOrderHeader
    top 10
    Percentage = SubTotal * 100 / GrandTotal
from sales.SalesOrderHeader, cte

You can find more CTE examples at http://www.kodyaz.com/content/SQLSe...icles.aspx

I hope that script will help you

Acceptable Use Policy