What is the proper syntax for usage jinja templates for columns in superset? - sqlalchemy

Here https://superset.apache.org/docs/installation/sql-templating/ described several parameters for handling columns:
columns: columns which to group by in the query
groupby: columns which to group by in the query (deprecated)
table_columns: columns available in the dataset
time_column: temporal column of the query (None if undefined)
How should I use these parameters?
I've tried this manual https://www.preset.io/blog/intro-jinja-templating-apache-superset/ but this is for values not for columns

Here is a syntax for table_columns and time_column:
SELECT
{{ ', '.join(table_columns) }}
from my_table where {{ time_column }} < '{{ from_dttm }}'
limit 100

Related

"Group" Datatype, group_concat without a "group"

For group_concat the MySQL manual says:
This function returns a string result with the concatenated non-NULL values from a group.
group in this context seems like it is a datatype of some sort. Is it possible to create this data type manually?
An example, I have one and two in a table and I want to retrieve those two values separated as a CSV:
select group_concat(column) from table group by column
This concatenates
'one', 'two'
as expected, however:
select group_concat('one', 'two');
returns:
'onetwo'
as if it were just concat.
The group is not a data type. In this context, it refers to a set of rows, defined by the rows with the same value in the column you name in the GROUP BY clause.
GROUP_CONCAT() with multiple arguments acts like CONCAT(), as mentioned in the comments above. That is, multiple arguments are concatenated into a single string.
You seem to want to concatenate multiple arguments with a comma separator, without taking the values from a set of rows. For this, you could use MySQL's string function CONCAT_WS():
select concat_ws(',', 'one', 'two') as my_list;
+---------+
| my_list |
+---------+
| one,two |
+---------+

how can i retrieve static value as column in sequelize findAll

In mysql query if I want to retrieve static value as column in select query I would normally use following statement:
SELECT
...,
"static value" as static_value
FROM table_name;
How can I accomplish such queries in sequelize findAll?
You can use sequelize.literal()
Model.findAll({
attributes: ['foo', 'bar', [sequelize.literal('static value'), 'static_value']]
});
Which is the equivalent of
SELECT foo, bar, 'static value' AS static_value FROM ...

Superset sql query editor using parameter in where clause

I am trying to use a parameter in where clause of a query. This query is going to run on an instance of a mysql database. This is my sample query that finds expired orders:
SELECT * FROM orders WHERE status = "expired"
I want the status part to be dynamically filled during visualization. When I replace it with a parameter like this:
SELECT * FROM orders WHERE status = {{ status }}
and define the parameter as:
{"status": "expired"}
I get this error:
Unknown column 'expired' in 'where clause'
It seems it is because of double quotation around expired.
I can use parameters successfully outside of where clause.
I think you are just missing one quote here.
SELECT * FROM orders WHERE status = '{{ status }}'

MySQL order by syntax

Ive just seen the following syntax : select * from table order by column = "b" desc
I guess that the rows with value "b" will come first but i am not sure.
What does the query mean and how sorting works in this case.
Ive searched on google about it but ... no success.
In SQL, you are not limited by sorting on values of existing columns: you can specify complex expressions inside your order by clause. This query sorts by the value of a boolean expression: the expression will be true for the rows where column = 'b', and false in all other rows. As the result, rows with column = 'b' will come first, because the order by clause specifies descending order, and in SQL, true is represented as 1, and false is 0.

Concat a string to SELECT * MySql

The following query works fine with MySQL:
SELECT concat(title,'/') FROM `socials` WHERE 1
It Concat / to the selected title field.
However, when I try to do the following:
SELECT concat(*,'/') FROM `socials` WHERE 1
It returns the follwoing Error:
#1064 - You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near '*,'/') FROM `socials` WHERE 1 LIMIT 0, 30' at line 1
So is there any way to make such sql query to work with MySql
You simply can't do that in SQL. You have to explicitly list the fields and concat each one:
SELECT CONCAT(field1, '/'), CONCAT(field2, '/'), ... FROM `socials` WHERE 1
If you are using an app, you can use SQL to read the column names, and then use your app to construct a query like above. See this stackoverflow question to find the column names: Get table column names in mysql?
If you want to concatenate the fields using / as a separator, you can use concat_ws:
select concat_ws('/', col1, col2, col3) from mytable
You cannot escape listing the columns in the query though. The *-syntax works only in "select * from". You can list the columns and construct the query dynamically though.
You cannot concatenate multiple fields with a string. You need to select a field instand of all (*).
You cannot do this on multiple fields. You can also look for this.