Quantcast
Channel: Forum SQL Server Database Engine
Viewing all articles
Browse latest Browse all 15694

How to use GROUP BY in SQL Server 2008R2

$
0
0

Hello,

I am using SQL Server 2008R2.

I would like to get the records which has MAX date GROUP BY "ProjectName". The query that i am using now is,

select [ProjectName],MAX([Month]) from [AdventureWorks].[dbo].[INfopath2]
  GROUP BY [ProjectName]

Result:

ProjectName (No column name)
Proj1	    2012-11-01
Proj2	    2012-11-02
Proj3	    2012-11-03
Proj4	    2012-09-01

Then, I would like to see the "Value" of the "ProjectName", then i added "Value", filed in to my query, the query as in the below,

select [ProjectName],MAX([Month]), [Value] from [AdventureWorks].[dbo].[INfopath2]
  GROUP BY [ProjectName],[Value]

Then, result is as in the below, which i don't want, i want only MAX[Month] ProjectName values, in this example it should only display 4 records.

ProjectName  (No column name)	Value
Proj1	      2012-01-01	           10
Proj2	      2012-02-10	           20
Proj3	      2012-11-03	           30
Proj1	      2012-11-01	           40
Proj4	      2012-09-01	           43
Proj2	      2012-11-02	           50
Proj4	      2012-08-02	           56
Proj2	      2012-07-01	           80
Proj3	      2012-08-01	           90

I hope you understand what i am exactly looking for.

Could please provide me suitable solution.

Thank you.


Viewing all articles
Browse latest Browse all 15694

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>