Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have the below table


List
VersionID  	ListID	CreatedDate	ListTableName
------------------------------------------------------------
1	          1	2013-09-16      ESD_1
2	          1	2013-09-17      ESD_2
3	          2	2013-09-16      Boolean_1
4	          2	2013-09-17      Boolean_2
5	          3	2013-09-16      List_Resources_1


In this table I want the values of Table name column for max of CreatedDate grouped by ListID.

For this I have written a query which is throwing me eror. Please help me in this.

SQL
select ListTableName,MAX(VersionedDate) from LISTS group by ListID


Msg 8120, Level 16, State 1, Line 1
Column 'LISTS.ListTableName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.



Thanks & Regards,
Mathi

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 20-Sep-13 21:14pm
v2

Please try this one

SQL
SELECT T1.LISTID, T1.CREATEDDATE, T1.LISTTABLENAMEQ
FROM TABLE_1 T1
INNER JOIN  (SELECT LISTID, MAX(CREATEDDATE)CREATEDDATE
             FROM TABLE_1
             GROUP BY LISTID) T2
ON DATEDIFF(DAY, T1.CREATEDDATE, T2.CREATEDDATE)=0 AND T1.LISTID = T2.LISTID
 
Share this answer
 
Comments
Mathi2code 23-Sep-13 5:05am    
Thanks Gauri
Try the following :
SQL
select ListTableName,MAX(VersionedDate) as VersionedDate  from LISTS group by ListTableName
 
Share this answer
 
v2
Comments
Mathi2code 23-Sep-13 5:05am    
Thanks Mehdi
SQL
select ListTableName,Max(VersionedDate) from Tem
group by ListID,ListTableName
 
Share this answer
 
v2
Comments
Mathi2code 23-Sep-13 5:05am    
Thanks Veera

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900