Dynamic queries

Hi there!
Can I create dynamic queries in metabase?

thanks,

Helena Maia

Hi @Helena
What is "dynamic queries"? Do you mean with parameters? Then yes:
https://www.metabase.com/docs/latest/users-guide/13-sql-parameters.html

1 Like

Here is an example:

(SELECT 'SELECT ')

UNION ALL

(SELECT
'SUM(CASE WHEN answer = ' || answer || ' THEN 1 ELSE 0 END) AS res' || answer || ','
FROM t
GROUP BY answer
ORDER BY answer)

UNION ALL

(SELECT 'COUNT(*) AS info2,
SUM(CASE WHEN answer IS NOT NULL THEN 1 ELSE 0 END) AS info3,
SUM(CASE WHEN answer IS NULL THEN 1 ELSE 0 END) AS info4
FROM t;')

It is not this case with parameters.

@Helena I don't understand. Can you reference the documentation of a database that supports this?

A friend told me that you can create a string, which hold an SQL statement. If you create that string it can contain a dynamic query. Most of the times such a thing can be done in a stored procedure. Here you can find more information: https://www.postgresql.org/docs/13/sql-prepare.html

@Helena Yeah, but something needs to execute that string. That's not really what Metabase is good at. That should be handled on the database.