我有一个Postgres表(9.3),它有一个名为a.locations的字段。我想生成一个临时表,其序列相同,但具有相同的序列号具有相同的位置。为了使事情更清楚,我当前的表(仅示例):
ID | Locations
1 | Bangalore
2 | New York
3 | London
4 | Bangalore
5 | New York
我需要的是:
ID | Locations
1 | Bangalore
1 | Bangalore
2 | London
3 | New York
3 | New York
感谢任何帮助。提前谢谢。
答案 0 :(得分:0)
尝试此查询:
INSERT INTO #temp (ID, locations)
SELECT l2.minID, l1.locations
FROM location l1
INNER JOIN
(
SELECT locations, MIN(ID) AS minID
FROM location
GROUP BY locations
) l2
ON l1.locations = l2.locations