1

I'm having problem with the following:

create table tab1 (col1 double precision, col2 double precision)
Insert into tab1 
select distinct col1, null 
from tab1

I can't do that, because of error column "col2" is of type double precision but expression is of type text

But this has no error:

Insert into tab1 
select col1, null 
from tab1 

Why do I have an error with distinct and no error without it?

3
  • 1
    Try adding ::double precision after the NULL. Commented Jul 1, 2019 at 14:41
  • did you put ; after the create table? Commented Jul 1, 2019 at 14:42
  • Yes, adding ::double precision is helping here. but my question is WHY error is here? what is the difference between select distinct col1, null and select col1, null in logic of posgresql Commented Jul 1, 2019 at 15:07

1 Answer 1

2

The DISTINCT is either calculated as a hash aggregate or by sorting, and both operations require that the type of the second column be determined (originally it is unknown).

So according to the rules for determining the type of output columns, PostgreSQL will resolve the NULL to text in this case.

You can easily override that with an explicit type cast.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.