我想编写一个函数,在postgresql的指定列中标记重复项。在查询中标记重复项的函数Postgresql
举例来说,如果我有如下表:
country | landscape | household
--------------------------------
TZA | L01 | HH02
TZA | L01 | HH03
KEN | L02 | HH01
RWA | L03 | HH01
我想能够运行下面的查询:
SELECT country,
landscape,
household,
flag_duplicates(country, landscape) AS flag
FROM mytable
,并得到以下结果:
country | landscape | household | flag
---------------------------------------
TZA | L01 | HH02 | duplicated
TZA | L01 | HH03 | duplicated
KEN | L02 | HH01 |
RWA | L03 | HH01 |
在函数体内部,我想我需要类似于:
IF (country || landscape IN (SELECT country || landscape FROM mytable
GROUP BY country || landscape)
HAVING count(*) > 1) THEN 'duplicated'
ELSE NULL
但我很困惑如何通过所有这些作为参数。我很感激帮助。我正在使用postgresql版本9.3。