Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need Help with a nested query

Author  Topic 

intern2424
Yak Posting Veteran

53 Posts

Posted - 2009-11-17 : 20:14:23
Hi, I have a question about a this query statement. I have 5 different car models. I have been trying to find all the models in 2003 and show the percentage each one had against the total sales for 2003.

sales = sale price for car
modletype = car model type
solddate = when car was sold

I want to try and display modeltype and percentage sold

Thank you for any help you can give

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-11-17 : 21:25:28
[code]
select modeltype,
sum(sales) as sales,
sum(sales) * 100.0 / (select sum(sales) from sales where solddate between '20030101' and '20031231')
from sales
where solddate between '20030101' and '20031231'
group by modeltype
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

intern2424
Yak Posting Veteran

53 Posts

Posted - 2009-11-17 : 22:38:13
So i do not have to create a variable for each model type? With this query, each model type will have different sums. So it will look like this:

modeltype Sum Percentage_of_total
a 50 .50
b 50 .50


I thought you had to do variables and other things.
Go to Top of Page

intern2424
Yak Posting Veteran

53 Posts

Posted - 2009-11-17 : 23:26:00
Thanks for the help. i understand it now.
Go to Top of Page
   

- Advertisement -