Results 1 to 11 of 11
  1. #1
    twgonder is offline Expert
    Windows 10 Access 2016
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658

    Where clause not using a calculated AS field

    I may have asked this kind of question in another thread here, after hours of work I found a workaround,


    However, it's now getting to be a pain to deal with this common type of SQL statement.

    I've got a search form that builds a SQL string to use as record source.
    There is a "calculated" (if that's the proper name here) field that is a concatenation of 4 fields and it gets a AS name.
    When the WHERE part runs the Enter Parameter Value box pops up.
    It seems the WHERE clause is lost in figuring out what the "calculated" field is.
    What am I doing wrong, or is this just another "come on Access!" thing?

    Check the highlighted part:

    Click image for larger version. 

Name:	221216Where1.jpg 
Views:	22 
Size:	81.3 KB 
ID:	49321

    Click image for larger version. 

Name:	221216Where2.jpg 
Views:	22 
Size:	16.0 KB 
ID:	49322

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I think it's a "come on SQL!" thing.

    You must repeat the calculation in WHERE clause. Or don't use WHERE clause and either build another query using the first as data source or apply filter to form or report.
    Last edited by June7; 12-16-2022 at 11:52 PM.

  3. #3
    twgonder is offline Expert
    Windows 10 Access 2016
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    Quote Originally Posted by June7 View Post
    I think it's a "come on SQL!" thing.

    You must repeat the calculation in WHERE clause. ...
    Okay thanks, that's another head-sized dent in the wall.

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    Alternatives are:

    use or’s instead of a concatenated string in the where clause

    wrap the bulk of your query in an alias and have the like clause outside of it

  5. #5
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Just as an FYI, SQL Server (and most other larger scale DBMS) doesn't allow you to refer to aliased fields names at all in the same query.
    So if you were ever to upscale you would be into a re-write.

    I have recently had to re-write an Access query for use in SQL server and it went from 400 characters to about 1500...
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  6. #6
    twgonder is offline Expert
    Windows 10 Access 2016
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658

    The new paradigm...

    Quote Originally Posted by Minty View Post
    Just as an FYI, SQL Server (and most other larger scale DBMS) doesn't allow you to refer to aliased fields names at all in the same query.
    So if you were ever to upscale you would be into a re-write.

    I have recently had to re-write an Access query for use in SQL server and it went from 400 characters to about 1500...
    I guess I'm no longer in Kansas Dorthy. Imagine, I wrote hundreds of aliases in my old query language (more than a 50-year-old design), and could use in it an alias in an infinite number of queries without having to duplicate the expression. I upscaled through five platforms and never had to rewrite any alias expression or the query itself for that matter. Imagine! Oh, but I didn't have hundreds of color options for the forms, so I guess...

  7. #7
    twgonder is offline Expert
    Windows 10 Access 2016
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658

    Final result, with duplicate expression for a pseudo field

    QUOTE=CJ_London;505156]Alternatives are:

    use or’s instead of a concatenated string in the where clause

    wrap the bulk of your query in an alias and have the like clause outside of it[/QUOTE]

    I'm not sure what all that means. In the program, I've broken the SQL statement into three parts. Then it is assembled into one statement that serves the particular search purpose, and in some cases, all I change is the Order By in the form, without having to do a requery. Here's how it turned out from my example above:

    Click image for larger version. 

Name:	221217Where3.jpg 
Views:	20 
Size:	282.3 KB 
ID:	49325[

  8. #8
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    I think you may have misunderstood me, in SQL Server you can use the alias in another query, just not the same one you created it in.
    So you can do this

    Code:
    SELECT Q1.MyCalc , Q1.MyOtherCalc , Q1.MyCalc - Q1.MyOtherCalc as NewResult 
    FROM 
    (
    SELECT Price1 * Uplift as MyCalc, Price2 * Uplift as MyOtherCalc
    From MyTable
    ) As Q1
    But not
    Code:
    SELECT Price1 * Uplift as MyCalc, Price2 * Uplift as MyOtherCalc,  MyCalc - MyOtherCalc as NewResult
    From MyTable
    As Q1
    The latter would work in Access however, it doesn't object provided you do things in the correct order.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  9. #9
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    if you provided your sql as text rather than an image, I could show you

    but basically

    SELECT * FROM (SELECT tbl_3TranIt.ID blah, blah
    FROM table1 inner join table2 on blah, blah) as q
    WHERE lblsMsgHlp like "*caci*"

  10. #10
    Join Date
    Apr 2017
    Posts
    1,680
    Quote Originally Posted by Minty View Post
    I think you may have misunderstood me, in SQL Server you can use the alias in another query, just not the same one you created it in.
    ...
    But not
    SELECT Price1 * Uplift as MyCalc, Price2 * Uplift as MyOtherCalc, MyCalc - MyOtherCalc as NewResult
    From MyTable
    As Q1
    ...
    But you can have (this works for both Access and SQL Server)
    Code:
    SELECT Q1.Price1 * Q1.Uplift as MyCalc, Q1.Price2 * Q1.Uplift as MyOtherCalc,  Q1.Q1.Price1 * Q1.Uplift - Q1.Price2 * Q1.Uplift as NewResult
    From MyTable Q1
    But probably you did mean field aliases, not table alias? Those you can't use in same query in SQL Server. I'm not sure about Access, as I never tried to use this kind of syntax there.

  11. #11
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,044
    if you know how T-SQL works, it's rather obvious why you need to repeat the calculation:
    -First the from clause is evaluated
    -Then secondly the WHERE clause
    -Then the SELECT is evaluated and here the alias is build
    -and last of all the ORDER BY
    So on the moment the WHERE clause is executed the alias field doesn't exist yet. But you can use the alias in the ORDER BY.

    If you don't want your DBA to get a heart attack: rewrite your Access queries when switching to SQL server.
    General rules:
    - don't use select *, but only use the columns you really need,
    - write queries so the indexes can be use, example don't use calculations in the first part of the criteria (example WHERE year(Mydate) > ...)
    - try to avoid sub queries: use joins instead or use a CTE (example: with qry1 as (select ...)
    - SQL server has a great tool to check your query performance: the query store

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 09-09-2022, 01:47 PM
  2. Replies: 5
    Last Post: 03-26-2018, 03:18 AM
  3. Replies: 1
    Last Post: 05-22-2017, 05:59 AM
  4. Replies: 2
    Last Post: 12-30-2014, 01:32 PM
  5. Replies: 1
    Last Post: 08-21-2013, 12:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums