你好,我有一个带有15000 Records
的表,我需要在一列中插入其他不重复的表记录
这是我的sql:
INSERT INTO ceny_min2(id, idt, kod_k, nrdok, data_z, odczytano, ost_update, cena_min)
SELECT *
FROM cenymin
WHERE id IN (
SELECT MIN(id)
FROM cenymin
WHERE (SELECT distinct idt FROM cenymin)
)
idt-是我只需要检查和翻转第一个记录的列。
错误:
ERROR: argument of WHERE must be type boolean, not type integer
********** Błąd **********
ERROR: argument of WHERE must be type boolean, not type integer
Stan SQL: 42804
如果您想防止表中出现重复,那么您应该使用唯一的约束或索引。然后,如果你想插入,你可以使用on conflict ignore
:
insert into ceny_min2 (id, idt, kod_k, nrdok, data_z, odczytano, ost_update, cena_min)
select *
from cenymin
on conflict on constraint <unique constraint> do nothing;
我会建议特定的语法,但我不知道你真正想做什么
我怀疑您想要:
insert into ceny_min2(id, idt, kod_k, nrdok, data_z, odczytano, ost_update, cena_min)
select id, idt, kod_k, nrdok, data_z, odczytano, ost_update, cena_min
from cenymin c
where c.id = (select min(id) from cenymin c1 where c1.idt = c.idt)
这将为每个idt
提供具有较小id
的行。
你也可以用窗口函数来表达这一点:
insert into ceny_min2(id, idt, kod_k, nrdok, data_z, odczytano, ost_update, cena_min)
select id, idt, kod_k, nrdok, data_z, odczytano, ost_update, cena_min
from (
select c.*, row_number() over(partition by idt order by id) rn
from cenymin c
) c
where rn = 1
或使用distinct on
:
insert into ceny_min2(id, idt, kod_k, nrdok, data_z, odczytano, ost_update, cena_min)
select distinct on (idt) id, idt, kod_k, nrdok, data_z, odczytano, ost_update, cena_min
from cenymin c
order by idt, id