I have table like this:
p_id | store | createdat | device | deviceserial | application
------+-------+---------------------+---------+--------------+-------------
| z10 | 2020-09-02 08:02:39 | Android | 636363636890 | app-a
| z10 | 2020-09-02 08:08:18 | Android | 636363636890 | app-a
| z10 | 2020-09-02 08:10:10 | Android | 636363636890 | app-a
| z10 | 2020-09-02 08:20:10 | Android | 636363636890 | app-a
| z10 | 2020-09-02 10:40:11 | IOS | 6625839827 | app-b
| z10 | 2020-09-02 10:45:11 | IOS | 6625839827 | app-b
| z10 | 2020-09-02 10:50:11 | IOS | 6625839827 | app-b
| z11 | 2020-09-02 08:47:10 | Android | 636363636891 | app-a
| z11 | 2020-09-02 08:55:10 | Android | 636363636891 | app-a
| z11 | 2020-09-02 08:59:10 | Android | 636363636891 | app-a
| z11 | 2020-09-02 13:01:11 | IOS | 6625839828 | app-b
| z11 | 2020-09-02 13:15:11 | IOS | 6625839828 | app-b
| z10 | 2020-09-02 12:03:10 | Android | 636363636890 | app-a
| z10 | 2020-09-02 12:09:10 | Android | 636363636890 | app-a
| z10 | 2020-09-02 12:12:10 | Android | 636363636890 | app-a
| z10 | 2020-09-02 15:15:11 | IOS | 6625839827 | app-b
| z10 | 2020-09-02 15:20:11 | IOS | 6625839827 | app-b
| z11 | 2020-09-02 10:25:10 | Android | 636363636891 | app-a
| z11 | 2020-09-02 10:35:10 | Android | 636363636891 | app-a
I am trying to insert some queries to another table(device_usage_test1).Thats my table:
create table if not exists device_usage_test1(id SERIAL,deviceserial VARCHAR(50) UNIQUE,device VARCHAR(50),deviceusage DOUBLE PRECISION)
And this is my insert command :
insert into device_usage_test1(deviceserial,device,deviceusage) select deviceserial,device,sum(deviceusage) as deviceusage
from (
select deviceserial,device,
extract(epoch from (max(createdat)::timestamp - min(createdat)::timestamp)) as deviceusage,
date_trunc('hour', createdat) +
(((date_part('minute', createdat)::integer / 10::integer) * 10::integer)|| ' minutes')::interval AS hr
FROM datatable
group by deviceserial,hr,device
) t
group by deviceserial,device;
Later I will make upsert query on device_usage_test1.So my deviceserial have to be unique.
But when I try the insert with deviceserial (unique).It gives me error:
ERROR:duplicate key value violates unique constraint "device_usage_deviceserial_key""
DETAIL: Key (deviceserial)=(636363636890) already exists.