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 |
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2013-11-27 : 03:05:56
|
I have a following query giving result as belowSelect *From (Select TemplateSectionId , FkTemplateId , SectionOrder , Count(1) Over ( PARTITION By FkTemplateId) As Cnt From dbo.TemplateSection) tsWhere ts.FkTemplateId = 22TemplateSectionId FkTemplateId SectionOrder Cnt49 22 90 252 22 2 2in above result section order are 90 ,21 and 2What i want's is sectionRank like 3rd for 90,2nd for 21 and 1st for 2likeTemplateSectionId FkTemplateId SectionOrder Cnt SectionRank49 22 90 2 353 22 21 2 252 22 2 2 1Kamran ShahidPrinciple Engineer Development(MCSD.Net,MCPD.net) |
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2013-11-27 : 05:16:41
|
Solve it via ROW_NUMBER()Over(partition by FkTemplateId Order by SectionOrder ascKamran ShahidPrinciple Engineer Development(MCSD.Net,MCPD.net) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-27 : 05:18:25
|
[code]Select *From (Select TemplateSectionId, FkTemplateId, SectionOrder, Count(1)Over (PARTITION By FkTemplateId) As Cnt, DENSE_RANK() Over (PARTITION By FkTemplateId ORDER BY SectionOrder ASC) As SectionRankFrom dbo.TemplateSection) tsWhere ts.FkTemplateId = 22[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-27 : 05:20:01
|
quote: Originally posted by kamii47 Solve it via ROW_NUMBER()Over(partition by FkTemplateId Order by SectionOrder ascKamran ShahidPrinciple Engineer Development(MCSD.Net,MCPD.net)
will work correctly so long as SectionOrder doesnt repeat within a FkTemplateIdMore safer option is to use DENSE_RANK as i showed------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|