Asked in a Github issue #3424
I want fetch data from SQL and use it as filter condition… how to do it?
- Use
sql.from()
to query the SQL data source - Do one of the following:
- Use
findColumn()
to extract values in a specific column as an array. - Use
findRecord()
to extract a specific row as a record.
- Use
- Reference values in the extracted array or record in your filter function.
Here’s a Postgres example:
import "sql"
sqlValues = sql.from(
driverName: "postgres",
dataSourceName: "postgresql://username:password@localhost:5432",
query: "SELECT labels FROM example_table WHERE this = 'something'",
)
|> findColumn(fn: (key) => true, column: "example-col")
from(bucket: "example-bucket")
|> range(start: -1d)
|> filter(fn: (r) => r.someTag == sqlValues[0])