Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to Specify a Parameter For AWS Athena Query?

I did some research and haven't yet found if this is possible, but does anyone know if you can specify a parameter for an AWS Athena Query?

For example, I want my query to be able to filter WHERE merchant_id = {merchant_id}. Is it possible to specify this so that I could use the same query for every merchant I want? And if so, where would I pass in the input to the Athena query?

like image 325
Louis Avatar asked Jan 03 '18 18:01

Louis


2 Answers

One cannot set variables with AWS Athena. Since Athena can run only one query in a session.

If you try to run more than one query you will get the following error.

enter image description here

Hope it helps.

like image 83
Kannaiyan Avatar answered Nov 01 '22 15:11

Kannaiyan


I appreciate this thread is a little old, but it's one of the first results I found when trying to do the same. I'm not sure if there would be significant performance issues, and I'm certain it can be improved upon, but the theory is as follows:

Using Athena's accepted WITH function, create a couple of queries to hold your variables, join these to your original query/table and then use them within the filters.

WITH startdate AS (SELECT DATE('2019-12-01') AS v_startdate), enddate AS (SELECT DATE('2019-12-31') AS v_enddate)
SELECT *SQL HERE* FROM table_name
    LEFT JOIN startdate ON 1=1
    LEFT JOIN enddate ON 1=1
    WHERE table_name.start_date >= v_startdate
            AND table_name.end_date <= v_enddate
like image 23
JonP Avatar answered Nov 01 '22 16:11

JonP