2009-09-09 60 views
0

如何从下面的示例中获得唯一的部门?简单的查询以获得组结果

Dept Id          Created Date 
06013cd7-2224-4220-b048-a54bbd1ff403   2009-09-08 17:36:11.293 
06013cd7-2224-4220-b048-a54bbd1ff403   2009-09-08 17:41:54.857 
5e29bd98-04ba-452d-bfcd-caa63ab9018b   2009-09-08 17:20:45.373 

我试过这样

select top 10 deptid, 
    (Select convert(varchar,createddate,101)) 
from depts 
where [status]='Y' 
group by deptid,convert(varchar,createddate,101) 

但显示的所有结果。我想这一点:

Dept Id          Created Date 
06013cd7-2224-4220-b048-a54bbd1ff403   2009-09-08 
5e29bd98-04ba-452d-bfcd-caa63ab9018b   2009-09-08 

你能不能帮我写这个查询

谢谢

回答

2
SELECT deptid, MAX(createdate) FROM depts WHERE [status] = 'Y' GROUP BY deptid 
+0

谢谢你的工作 – Nagu 2009-09-09 04:24:09

2

你返回所有行,因为你包括分组的日期。试试:

select deptid, Max(convert(varchar,createddate,101)) AS MaxDate 
from depts 
where [status]='Y' 
group by deptid