|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
Cannot retrieve latest entry for each year.
Hello
![]() I am trying to retrieve the latest (most recent) entry for each year and I am stumped. I write MySQL but unfamiliar with SQL. Here is my failed attempt so far: Select VictoryID,Title,Campaign,body,Image,Year,max(LastE dited) From Victory where Year IN (Select distinct(Year) FROM Victory) I have tried a zillion variations and am willing to completely rewrite this select statement. I want like all the info about the returned result essentially but want one for each year. Where an I going wrong here? |
|
#2
|
|||
|
|||
|
Hi,
If you were using SQL2005, the below query would help you. But I think you are developing on MySQL WITH CTE(Year) AS ( SELECT DISTINCT(DATEPART(YY,InsertedDate)) FROM Sales(NoLock) ) SELECT * FROM ( SELECT MAX(InsertedDate) InsertedDate FROM Sales(NoLock) INNER JOIN CTE ON CTE.Year = DATEPART(YY,InsertedDate) GROUP BY CTE.Year ) CTE_OUT INNER JOIN Sales (NoLock) ON Sales.InsertedDate = CTE_OUT.InsertedDate Eralper http://www.kodyaz.com |
![]() |
| Viewing: Dev Articles Community Forums > Databases > General SQL Development > Cannot retrieve latest entry for each year. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|