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.
Author |
Topic |
Muj9
Yak Posting Veteran
75 Posts |
Posted - 2014-09-15 : 05:27:05
|
drop table Test;with cte as (select 'Capcom' as Comapny, 320 as Total, 2012 as Yearunion allselect 'Capcom' as Comapny, 520 as Total, 2013 as Yearunion allselect 'Capcom' as Comapny, 750 as Total, 2014 as Yearunion allselect 'Konami' as Comapny, 220 as Total, 2012 as Yearunion allselect 'Konami' as Comapny, 420 as Total, 2013 as Yearunion allselect 'Konami' as Comapny, 820 as Total, 2014 as Yearunion allselect 'Rare' as Comapny,20 as Total, 2012 as Yearunion allselect 'Rare' as Comapny, 150 as Total, 2013 as Yearunion allselect 'Rare' as Comapny, 50 as Total, 2014 as Yearunion allselect 'Namco' as Comapny,220 as Total, 2012 as Yearunion allselect 'Namco' as Comapny, 350 as Total, 2013 as Yearunion allselect 'Namco' as Comapny, 345 as Total, 2014 as Yearunion allselect 'UbiSoft' as Comapny,80 as Total, 2012 as Yearunion allselect 'UbiSoft' as Comapny, 120 as Total, 2013 as Yearunion allselect 'UbiSoft' as Comapny, 110 as Total, 2014 as Year)select * into Testfrom cteselect * from Testhow to identify the top 3 companines who have had the largest percentage growth over the last two years?Thank you |
|
Arun Babu N
Starting Member
26 Posts |
Posted - 2014-09-15 : 06:07:03
|
Select top 3 Comapny,RunningTotal 'Growth over the last two years'from (Select distinct Comapny,Sum(total) over(partition by Comapny order by year desc)'RunningTotal' ,dense_rank() over(partition by Comapny order by year desc)'Ranks' from dbo.Test1 )as testwhere ranks =2order by RunningTotal descArun Babu N |
|
|
Muj9
Yak Posting Veteran
75 Posts |
Posted - 2014-09-15 : 06:25:36
|
i keep getting error :-Msg 102, Level 15, State 1, Line 4Incorrect syntax near 'order'.Can you post the output of your query please.And thank you |
|
|
Arun Babu N
Starting Member
26 Posts |
Posted - 2014-09-15 : 07:53:28
|
which version u r using?Comapny Growth over the last two yearsCapcom 1270Konami 1240Namco 695in 2008 r2 try the below code,Select top 3 Comapny,RunningTotal 'Growth over the last two years'from (Select distinct Comapny, (Select Sum(total) from dbo.Test1 t1 where t1.Comapny = t2.Comapny and t1.Year >= t2.Year ) 'RunningTotal' ,dense_rank() over(partition by Comapny order by year desc)'Ranks' from dbo.Test1 t2)as testwhere ranks =2order by RunningTotal desc |
|
|
|
|
|
|
|