SQL查询,需要不同的行

时间:2010-06-25 17:57:42

标签: sql ms-access distinct

嘿伙计们,我的结构如下:

row structure

所以基本上你可以看到,该部门每隔几年就会更名。以16号为例。我想要一个只在日期最大时获取名称的选择查询。我该怎么做?

4 个答案:

答案 0 :(得分:5)

select ID, Name from departments o 
where o.thedate=
  (select max(i.thedate) from departments i where o.id=i.id)

答案 1 :(得分:1)

SELECT ID, 
First(Name) AS FirstOfName, First(DateChange) AS FirstOfDateChange
FROM departments
GROUP BY ID
ORDER BY First(DateChange) DESC;

答案 2 :(得分:0)

此表的主键是什么?这使子查询与名称比较的表相同。

SELECT
    id,
    name,
    date
FROM table
WHERE name = (SELECT TOP 1 name
              FROM table AS subtable
              WHERE subtable.name = table.name
              ORDER BY date DESC)

答案 3 :(得分:0)

SELECT d。* 来自部门d INNER JOIN(选择pk             来自部门             GROUP BY ID             HADING theDate = MAX(theDate))m ON m.pk = d.pk 在哪里[名称] =“部门”