Pypika Query for SELECT AS not working as intended

1.2k Views Asked by At

I am trying to use Pypika(https://pypika.readthedocs.io/en/latest/3_advanced.html) for building a simple SQL select query where I need to provide aliases to the columns of imported data. The query i expect to see generated is as follows.

SELECT "date" AS "x_date","count" AS "x_count" FROM "trades_features" WHERE "ticker"='AAU' AND "date" BETWEEN '2012-12-26' AND '2016-12-29';

and the Pypika syntax that I have tried is as follows

from pypika import Query,Table
tb = Table('trades_features')
query = Query.from_(tb).select(tb.date.as_('x_date'), tb.count.as_('x_count')).where(tb.ticker == 'AAU').where(tb.date['2012-12-26':''2016-12-29'])
print(query.get_sql())

which generates,

SELECT "date" "x_date","count" "x_count" FROM "trades_features" WHERE "ticker"='AAU' AND "date" BETWEEN '2015-01-01' AND '2016-01-01'

why is the AS caluse missing? what am i missing here?

1

There are 1 best solutions below

0
Kazi Mohammad Ali Nur Romel On
SELECT "date" "x_date","count" "x_count" FROM "trades_features" WHERE "ticker"='AAU' AND "date" BETWEEN '2015-01-01' AND '2016-01-01'

Above query will generate same result as below query in all major RDBMS.

SELECT "date" AS "x_date","count" AS "x_count" FROM "trades_features" WHERE "ticker"='AAU' AND "date" BETWEEN '2012-12-26' AND '2016-12-29';

I never used Pypika. But after some google searching I think it just omits as since it's not mandatory.