I'm trying to use a Named Map in Carto with the 'in' operator to avoid to make various calls to paint the same way a few features.

I want to do this:

SELECT * FROM madrid where id_tramo in ('2805200000011','2805200000041','2805200000051','2805200000061')

In my case, I'm using a string with simple quotes and commas to separate values.This is part of the call:

config: "provincia":"a_coruna","municipio":"15041","idtramos":"'1504100012461','1504100014191','1504100012951'","layer0":1}

You can see the parameter "idtramos" being "'1504100012461','1504100014191','1504100012951'". Of course, the Named Map is getting in trouble with simple quotes and commas because I'm using sql_literal as variable type. As we can see in Carto. And this is what I'm getting:

errors:["syntax error at or near "1504100012461""]

It's possible to do it with a Named Map? If not I will have to make a loop calling the each feature, but I understand is not the best way to do it. Maybe there is another way to do it.

This is the Named Map:

'paint_bloqued_tramos' : """{ "version": "0.0.1", "name": "paint_bloqued_tramos", "auth": { ... }, "placeholders": { "provincia": { "type": "sql_literal", "default": "No hay variable provincia y no puede filtrar en la query del NM" }, "municipio": { "type": "sql_literal", "default": "No hay variable municipio y por el NM da error" }, "idtramos": { "type": "sql_literal", "default": "No hay variable idtramo y no puede filtrar en la query del NM" } }, "layergroup": { "layers": [{ "type": "cartodb", "options": { "cartocss_version": "2.1.1", "cartocss": "#layer { line-width: 7; line-color: #000000; line-opacity: 0.3; line-comp-op: color-burn; }", "sql": "SELECT * FROM <%= provincia %>_tram where municipio_id = '<%= municipio %>' AND id_tramo in (<%= idtramos %>)" } }] } }

1 Answer 1


You need to use 'sql_ident' so quotes are treated correctly. So your template would have a definition like this (approx):

{ "auth": { "method": "open" }, "version": "0.0.1", "name": "named_map_test_in", "placeholders": { "filter": { "type": "sql_ident", "default": "'Spain'" } ... { "type": "cartodb", "options": { "sql": "select * from populated_places where adm0name in (<%= filter %>)", "layer_name": "populated_places", "cartocss": "#populated_places{ marker-line-opacity: 0; marker-width: 3; marker-fill: <%= color %>; marker-allow-overlap: true;}", "cartocss_version": "2.1.1", "interactivity": [ "cartodb_id", "name" ], .....

and an example of map instantiation you can try here http://jsfiddle.net/adndz2zr/1/ just change the list of countries and see how they are filtered.


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.