I have a table of locations with associated data combined from multiple providers. I had the provider id, the latitude and the longitude (both coordinates are double precision
) as primary keys. But now, I would like to generate an id of type varchar(50)
from the provider, the latitude and the longitude (both coordinates are double-precision values):
UPDATE mytable
SET id = CONCAT(provider, '-', latitude, '-', longitude)
But when I try to set the new column as primary key
ALTER TABLE mytable ADD PRIMARY KEY (id)
I get the error
Key (id)=(2-53.69706-12.71236) is duplicated.
Turns out that there are indeed two entries with id='2-53.69706-12.71236'
, but only one of them has longitude 12.71236. The other one has longitude 12.71235999999999. And while I can select them by these different longitudes:
SELECT
longitude,
to_char(longitude, '999D9999999999999999') AS longitude_to_char,
data->>'ID' AS id_within_provider
FROM mytable
WHERE provider=2 AND latitude=53.69706 AND longitude=12.71236
longitude | longitude_to_char | id_within_provider
12.71236 | 12.7123600000000 | 42266
SELECT
longitude,
to_char(longitude, '999D9999999999999999') AS longitude_to_char,
data->>'ID' AS id_within_provider
FROM mytable
WHERE provider=2 AND latitude=53.69706 AND longitude=12.71235999999999
longitude | longitude_to_char | id_within_provider
12.71236 | 12.7123600000000 | 84742
the output longitude stays the same even if I try to convert it to char with an explicit precision. Funny enough, to_char(12.71235999999999, '999D9999999999999999')
yields the original number while to_char(longitude, '999D9999999999999999')
always yields the rounded number.
So I've got two different double precision values that I have no way to output in a different way, how they should be. What can I do? Am I doing something wrong?