我有一个由查询组获取我的一些记录。如果我希望查找代表这些记录的其他列详情,该怎么办?选择查询以从内部查询的这些记录中选择列。内部查询和外部查询有不同的列
假设我有一个查询如下。 Select id,max(date) from records group by id;
以获取表格中最近的条目。 我希望获取另一列代表这些记录。
我想要做这样的事情(例如,这个不正确的查询): Select type from (Select id,max(date) from records group by id)
但这里的类型不存在于内部查询中。
我无法以更简单的方式定义问题,我为此道歉。
任何帮助表示赞赏。
编辑:
Column | Type | Modifiers
--------+-----------------------+-----------
id | integer |
rdate | date |
type | character varying(20) |
样本数据:
id | rdate | type
----+------------+------
1 | 2013-11-03 | E1
1 | 2013-12-12 | E1
2 | 2013-12-12 | A3
3 | 2014-01-11 | B2
1 | 2014-01-15 | A1
4 | 2013-12-23 | C1
5 | 2014-01-05 | C
7 | 2013-12-20 | D
8 | 2013-12-20 | D
9 | 2013-12-23 | A1
当我试图像这样(我在SQL没有好):select type from records as r1 inner join (Select id,max(rdate) from records group by id) r2 on r1.rdate = r2.rdate ;
或
select type from records as r1 ,(Select id,max(rdate) from records group by id) r2 inner join r1 on r1.rdate = r2.rdate ;
你可以发布你的表格定义和一些样品数据吗? – mlinth