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
 distinct

Author  Topic 

BendJoe
Posting Yak Master

128 Posts

Posted - 2008-11-03 : 12:15:03
SELECT distinct
Max(CONVERT(VARCHAR,A.ActiveDate,101)) sortdate,
CONVERT(VARCHAR,datename(month,A.ActiveDate)) + ' ' + CONVERT(VARCHAR,YEAR(A.ActiveDate)) ActiveDate
FROM
Articles A,
ArticleGroupLink GL
WHERE
A.ArticleID = GL.ArticleID and
GL.GroupID = 44
GROUP BY ActiveDate
order by
sortdate desc, --CONVERT(VARCHAR,A.ActiveDate,101) desc ,
CONVERT(VARCHAR,datename(month,A.ActiveDate)) + ' ' + CONVERT(VARCHAR,YEAR(A.ActiveDate)) desc


I need to get distinct ActiveDate column. Tried to get the max of the sortdate and group by ActiveDate that is not working for me. How can I get this done?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-03 : 12:18:18
looks like you need this
SELECT ActiveDate,MAX(sortdate) AS sortdate
FROM
(
SELECT
CONVERT(VARCHAR,A.ActiveDate,101) sortdate,
CONVERT(VARCHAR,datename(month,A.ActiveDate)) + ' ' + CONVERT(VARCHAR,YEAR(A.ActiveDate)) ActiveDate
FROM
Articles A,
ArticleGroupLink GL
WHERE
A.ArticleID = GL.ArticleID and
GL.GroupID = 44
)t
GROUP BY ActiveDate
order by
sortdate desc
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-11-04 : 01:17:04
Always better to specify the length of VARCHAR during convertion
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/12/04/column-length-and-data-length.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -