I'm having trouble coming up with the right query that does the following: Let's say we somehow take input from user for the value of two columns, datestamp
and brand
(what happens is a Looker UI feeds this to the query). For simplicity let's say our schema includes both columns and I only want to get them back, so:
SELECT brand, datestamp
FROM my_table
WHERE
I'm not sure how I should write the rest. Here's what's desired:
The user be able to type
yesterday
,today
,last x days
for somex
or a string like20181001
and the right date gets passed to the query.The user be able to specify what
brand
should be equal to and that gets passed to the query.( CASE WHEN position(' TO ' IN UPPER({% parameter filter_datestamp %})) > 0 THEN datestamp >= SUBSTR({% parameter filter_datestamp %}, 0, strpos(UPPER({% parameter filter_datestamp %}), ' TO ') - 1) AND datestamp <= SUBSTR({% parameter filter_datestamp %}, strpos(UPPER({% parameter filter_datestamp %}), ' TO ') + 4) AND brand LIKE {% parameter filter_brand %} WHEN filter_datestamp = 'today' THEN datestamp = date_format(CURRENT_DATE,'yyyyMMdd') AND brand LIKE {% parameter filter_brand %} WHEN datestamp = 'yesterday' THEN datestamp = date_format(DATE_ADD('day', -1, CURRENT_DATE),'%Y%m%d') AND brand LIKE {% parameter filter_brand %} WHEN datestamp LIKE 'last % days' THEN datestamp >= date_format(DATE_ADD('day', -CAST(split({% parameter filter_datestamp %}, ' ')[2] AS INT), CURRENT_DATE),'%Y%m%d') AND brand LIKE {% parameter filter_brand %} ELSE datestamp LIKE {% parameter filter_datestamp %} AND brand LIKE {% parameter filter_brand %} END )
If the query works, the result should be a number of rows with brand
and datestamp
equal to what the user wants. All rows will be identical (I actually want something else but think I can do that if the CASE
is resolved).
Please write the complete query for me. I'm not sure where WHERE
goes with respect to CASE
, WHEN
, AND
, and other keywords present in this case.