Monday, 15 September 2014

visual studio 2010 - Need to print highest year and their highest quarter in SQL Server 2012 -


I need to print the corresponding column for the highest year and the highest quarter relevant.

Input is in a table:

  cityprogram year quarter ====================== ABC 1998 1 ABC 1999 4 ABC 1999 4 ABC 1998 3 xyz 1998 4 xyz 1998 1 xyz 2000 3   

It should print

  ABC 1999 4 xyz 2000 3   

I tried to connect with a lot, the maximum conditions, I get the fourth and 4 for both of them :( Thanks

Use a common ROW_NUMBER in a common-table-expression:

 with  CTE R DESC, [quart] DESC from DOO. TELNAM) [Cityprogram], [Years] [Decision by [City Program], [Year], [Quarter] RN = ROW_NUMBER () ([City Program]] ], [Quarter] RN = 1   

  CITYPROGRAM year tractor ABC 1999 4 xyz 2000 3   

ROW_NUMBER returns only one row per group, even if you have a relation (city program + quarter with the same year) If you want to show the highest, you can use ROW_NUMBER C Ode> DENSE_RANK .

No comments:

Post a Comment