我想編寫一個函數,在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。