Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alias not being specified for complex criteria #521

Open
jcoetzee opened this issue Nov 11, 2020 · 2 comments
Open

Alias not being specified for complex criteria #521

jcoetzee opened this issue Nov 11, 2020 · 2 comments

Comments

@jcoetzee
Copy link

I'm attempting to use the code snippet below however my alias directives are not being honoured for the middle two SELECT clauses (Q2 and Q3) while the are being specified for Q1 and Q4 (which are simple less than or greater than comparisons). The function returns without error so it's failing silently. Am I specifying something incorrectly? I also tried with Criterion.all([]) with the same result. Python 3.9.0 and PyPika 0.44.0

from pypika import MySQLQuery, Table, functions
metrics = Table('metrics')
q = MySQLQuery.from_(metrics).select(
                functions.Count('1').as_('count'),
                (metrics.date < "2020-04-01 00:00:00").as_('Q1'),
                ((metrics.date >= "2020-04-01 00:00:00") & (metrics.date < "2020-07-01 00:00:00")).as_('Q2'),
                ((metrics.date >= "2020-07-01 00:00:00") & (metrics.date < "2020-10-01 00:00:00")).as_('Q3'),
                (metrics.date >= "2020-10-01 00:00:00").as_('Q4')
            ).where(metrics.date >= "2020-01-01 00:00:00").groupby("Q1", "Q2", "Q3", "Q4")
print(q.get_sql())

Output:

SELECT COUNT('1') `count`,`date`<'2020-04-01 00:00:00' Q1,`date`>='2020-04-01 00:00:00' AND `date`<'2020-07-01 00:00:00',`date`>='2020-07-01 00:00:00' AND `date`<'2020-10-01 00:00:00',`date`>='2020-10-01 00:00:00' Q4 FROM `metrics` WHERE `date`>='2020-01-01 00:00:00' GROUP BY `Q1`,`Q2`,`Q3`,`Q4`
@jtetrault
Copy link

jtetrault commented Feb 26, 2021

I am seeing a similar issue. I am expecting this to give a single column named "in_the_future", but the alias is not coming out in the generated SQL.

from pypika import Table, Query
from pypika.functions import Now, Coalesce

table = Table('table')

query = Query.from_(table).select(
    (table.some_boolean_column | Coalesce(table.timestamp > Now(), False)).as_('in_the_future'),
)

print(query.get_sql())

Expected output:

SELECT "some_boolean_column" OR COALESCE("timestamp">NOW(),false) in_the_future FROM "table"

Actual output:

SELECT "some_boolean_column" OR COALESCE("timestamp">NOW(),false) FROM "table"

@Ga68
Copy link

Ga68 commented Jul 7, 2023

see #735

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants