Select 从内部查询的记录中选择列的查询。内部查询和外部查询有不同的列
Select query for selecting columns from those records from the inner query . where inner query and outer query have different columns
我有一个分组查询,它为我获取了一些记录。如果我想找到代表这些记录的其他列详细信息怎么办?
假设我有如下查询。Select id,max(date) from records group by id;
获取 table 中的最新条目。
我希望获取代表这些记录的另一列。
我想做这样的事情(这个错误的查询只是为了举例):
Select type from (Select id,max(date) from records group by id)
但这里的类型在内部查询中不存在。
我无法用更简单的方式定义问题 manner.I 对此深表歉意。
感谢任何帮助。
编辑:
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 ;
如果我对问题的理解是正确的,那么这应该可行(或者至少为您提供一些可以使用的东西):
SELECT
b.id, b.maxdate, a.type
FROM
records a -- this is the records table, where you'll get the type
INNER JOIN -- now join it to the group by query
(select id, max(rdate) as maxdate FROM records GROUP BY id) b
ON -- join on both rdate and id, otherwise you'll get lots of duplicates
b.id = a.id
AND b.maxdate = a.rdate
请注意,如果同一 ID 和 rdate 组合的记录具有不同类型,您将得到重复项。
您可以使用 window function 轻松做到这一点:
SELECT id, rdate, type
FROM (
SELECT id, rdate, type, rank() OVER (PARTITION BY id ORDER BY rdate DESC) rnk
FROM records
WHERE rnk = 1
) foo
ORDER BY id;
window 定义 OVER (PARTITION BY id ORDER BY rdate DESC)
获取具有相同 id
值的所有记录,然后从最近到最不最近的顺序排序 rdate
并为每个记录分配一个等级排。 1 的排名是最近的,所以相当于 max(rdate)
.
我有一个分组查询,它为我获取了一些记录。如果我想找到代表这些记录的其他列详细信息怎么办?
假设我有如下查询。Select id,max(date) from records group by id;
获取 table 中的最新条目。
我希望获取代表这些记录的另一列。
我想做这样的事情(这个错误的查询只是为了举例):
Select type from (Select id,max(date) from records group by id)
但这里的类型在内部查询中不存在。
我无法用更简单的方式定义问题 manner.I 对此深表歉意。
感谢任何帮助。
编辑:
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 ;
如果我对问题的理解是正确的,那么这应该可行(或者至少为您提供一些可以使用的东西):
SELECT
b.id, b.maxdate, a.type
FROM
records a -- this is the records table, where you'll get the type
INNER JOIN -- now join it to the group by query
(select id, max(rdate) as maxdate FROM records GROUP BY id) b
ON -- join on both rdate and id, otherwise you'll get lots of duplicates
b.id = a.id
AND b.maxdate = a.rdate
请注意,如果同一 ID 和 rdate 组合的记录具有不同类型,您将得到重复项。
您可以使用 window function 轻松做到这一点:
SELECT id, rdate, type
FROM (
SELECT id, rdate, type, rank() OVER (PARTITION BY id ORDER BY rdate DESC) rnk
FROM records
WHERE rnk = 1
) foo
ORDER BY id;
window 定义 OVER (PARTITION BY id ORDER BY rdate DESC)
获取具有相同 id
值的所有记录,然后从最近到最不最近的顺序排序 rdate
并为每个记录分配一个等级排。 1 的排名是最近的,所以相当于 max(rdate)
.