0

I have this query:

SELECT DISTINCT par.id, par.title, cod.id AS id_codebook, cod.title AS title_codebook
FROM catalog_parameter_codebook cod
JOIN catalog_parameter par ON (par.id=cod.id_parameter)
JOIN catalog_parameter_product_value_parameter_codebook_mm codmm ON (cod.id=codmm.id_parameter_codebook)
JOIN catalog_parameter_product_value pv ON (pv.id=codmm.id_parameter_product_value)
JOIN catalog_product p ON (p.id=pv.id_product)
WHERE p.state=2 AND p.hidden=0 AND par.type=2 AND par.display_in_parameter_search=1 AND par.hidden=0 
ORDER BY par.sorting, cod.title 

And i get this error:

LINE 8: ORDER BY par.sorting, cod.title

ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list

I am a total beginner with postgreSQL. Thank you.

1 Answer 1

2

as error message states, par.sorting must be in the select list

if you use distinct or group by, all columns you are sorting by, must be in the select column list

SELECT DISTINCT par.id, par.title, cod.id AS id_codebook, cod.title AS title_codebook, par.sorting
FROM catalog_parameter_codebook cod
JOIN catalog_parameter par ON (par.id=cod.id_parameter)
JOIN catalog_parameter_product_value_parameter_codebook_mm codmm ON  (cod.id=codmm.id_parameter_codebook)
JOIN catalog_parameter_product_value pv ON (pv.id=codmm.id_parameter_product_value)
JOIN catalog_product p ON (p.id=pv.id_product)
WHERE p.state=2 AND p.hidden=0 AND par.type=2 AND par.display_in_parameter_search=1 AND par.hidden=0 
ORDER BY par.sorting, cod.title 
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I understand it, but when I add par.sorting to the SELECT list, i don´t get a output which required. I get duplicated rows.
why do you get duplicate? give us a primitive table schema, preferably with a small sampledata, and explain what do you want to acomplish

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.