Q:

SQL Server : get percentage of sales by country

I have a T-SQL query that is calculating the sum of sales by country:
SELECT      
    SUM(F_Sales_Month.Sales_Qty), D_Customer.Country
FROM            
    F_Sales_Month
INNER JOIN 
    D_Customer ON F_Sales_Month.CustomerKey = D_Customer.CustomerKey
GROUP BY 
    D_Customer.Country

Result:

How can I get the percentage instead of the amount?

A:

This is probably the most efficient  
declare @s money = (select sum(Sales_Qty) / 100.0 from F_Sales_Month);
SELECT      sum(F_Sales_Month.Sales_Qty) / @s as pct 
         ,  D_Customer.Country
FROM        F_Sales_Month
INNER JOIN D_Customer ON F_Sales_Month.CustomerKey = D_Customer.CustomerKey
GROUP BY D_Customer.Country

declare @t table (amt int, c char);
insert @t values 
(1, 'a'),
(3, 'a'),
(5, 'b'),
(11, 'c');
declare @s money = (select sum(amt) / 100.0 from @t)
select t.c, sum(t.amt) as sum, sum(t.amt)/@s as pct 
from @t t 
group by t.c
order by t.c;

