SQL not liking alias

  • I'm trying to run a simple query where I'm aliasing. SQL keeps giving me syntax error on the first alias call(SELECT tm.task_id). Does anyone have any ideas as to why this would be happening?

    select tm.task_id from task_mstr tm,task_appointment_xref tex,appointments pe,practice p where tm.subject like '%Active Coverage Found%' and tm.task_id=tex.task_id and tex.appointment_id=pe.appt_id and pe.practice_id=p.practice_id and p.practice_name='Andrea D. Pedano DO' and tm.create_timestamp between '2015-11-16' and '2015-11-16 23:59:59' 

  • The FROM needs to appear at the end of your column list.

    I recommend that you format your code with indentations and white space. It makes it easier to read and more likely that you'll detect syntax errors such as this one.

    John

  • John Mitchell-245523 (11/17/2015)


    The FROM needs to appear at the end of your column list.

    He is using the old ansi-89 style joins.

    SELECT

    tm.task_id

    FROM

    task_mstr AS tm

    INNER JOIN task_appointment_xref AS tex ON tm.task_id = tex.task_id

    INNER JOIN appointments AS pe ON tex.appointment_id = pe.appt_id

    INNER JOIN practice AS p ON pe.practice_id = p.practice_id

    WHERE tm.subject LIKE '%Active Coverage Found%'

    AND p.practice_name = 'Andrea D. Pedano DO'

    AND tm.create_timestamp BETWEEN '2015-11-16' AND '2015-11-16 23:59:59';

    Now we can see better what is going on.

    http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-old-style-joins.aspx

    Catch-all queries done right [/url]
    Gail Shaw's Performance Blog[/url]

  • cjefferson (11/17/2015)


    I'm trying to run a simple query where I'm aliasing. SQL keeps giving me syntax error on the first alias call(SELECT tm.task_id). Does anyone have any ideas as to why this would be happening?

    select tm.task_id from task_mstr tm,task_appointment_xref tex,appointments pe,practice p where tm.subject like '%Active Coverage Found%' and tm.task_id=tex.task_id and tex.appointment_id=pe.appt_id and pe.practice_id=p.practice_id and p.practice_name='Andrea D. Pedano DO' and tm.create_timestamp between '2015-11-16' and '2015-11-16 23:59:59' 

    Here it is again, reformatted somewhat, with the FROM moved to the end as suggested:

    select tm.task_id tm

    ,task_appointment_xref tex

    ,appointments pe

    ,practice p

    from task_mstr

    where tm.subject like '%Active Coverage Found%'

    and tm.task_id = tex.task_id

    and tex.appointment_id = pe.appt_id

    and pe.practice_id = p.practice_id

    and p.practice_name = 'Andrea D. Pedano DO'

    and tm.create_timestamp between '2015-11-16'

    and '2015-11-16 23:59:59';

    It remains unworkable, because the table aliases tm, tex, pe and p are not defined.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • MadAdmin (11/17/2015)


    John Mitchell-245523 (11/17/2015)


    The FROM needs to appear at the end of your column list.

    He is using the old ansi-89 style joins.

    SELECT

    tm.task_id

    FROM

    task_mstr AS tm

    INNER JOIN task_appointment_xref AS tex ON tm.task_id = tex.task_id

    INNER JOIN appointments AS pe ON tex.appointment_id = pe.appt_id

    INNER JOIN practice AS p ON pe.practice_id = p.practice_id

    WHERE tm.subject LIKE '%Active Coverage Found%'

    AND p.practice_name = 'Andrea D. Pedano DO'

    AND tm.create_timestamp BETWEEN '2015-11-16' AND '2015-11-16 23:59:59';

    Now we can see better what is going on.

    http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-old-style-joins.aspx

    Well done on unscrambling that.

    The final row in the WHERE clause would be better as:

    AND tm.create_timestamp >= '20151116'

    AND tm.create_timestamp <'20151117'

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply