Results 1 to 12 of 12

Thread: How to use fields alias name in sql query?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Pakistan
    Posts
    436

    How to use fields alias name in sql query?

    Hi friends,

    I wrote following query where i summed a field billed amount as amount, when i am trying to use this alias in other calculation, query returns error " Invalid column name amount". How i can re-use field alias name in sql query.

    VB Code:
    1. .Open "select b.trdate,e.jobno,e.jobdesc,p.partyname,b.manualbillid as BillNo , " & _
    2.             "sum(b.billedqty) as Quantity,b.billedrate as Rate,round(sum(b.billedamount),0) as Amount, " & _
    3.             "round(amount*15/100,'0') as GSt,round(sum(b.billedamount*15/100),0) + round(sum(b.billedamount),0) as TotValue " & _
    4.             "FROM enquiry e,parties p, bills b " & _
    5.             " " & FilterText & " " & _
    6.             "GROUP BY b.trdate,e.jobno,e.jobdesc,p.partyname,b.manualbillid, " & _
    7.             "b.billedrate order by p.partyname,b.manualbillid,b.trdate "
    second, as u see in my query that i added a lot fields in group, because i have in need individual information of each job and simultaneously ccumulated information of each job, no doubt i got the desired result but i am not satisfy, my suspect is, this junk of fields in group my slow down the query result, please check out this query and suggest what should be the possible smart query for same desired result

  2. #2
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    Re: How to use fields alias name in sql query?

    You can't use the alias in another calculation.
    Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment.

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How to use fields alias name in sql query?

    As simonm said you cannot use the "alias" in another equation in another column...

    Simply put the same "SUM" in both columns - the query should not take a negative hit because of that - it should realize while parsing the query that it's the same "sub-equation".

    What database?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Pakistan
    Posts
    436

    Re: How to use fields alias name in sql query?

    Well, it is working in access why not in sql server 2000

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to use fields alias name in sql query?

    Even tho they are both database systems by the same company, they are very different products, and have several SQL syntax differences (such as: joins, wildcards, temp tables, ...).

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Pakistan
    Posts
    436

    Re: How to use fields alias name in sql query?

    Does it not a weakness of sql server 2000, once we got a complicated calculation resulte in a coloumn, which might be 2 or three row in length does it not better to reuse this coloumn again where other calcuation may base upon its result. Besides this we have to apply same formula for each time.

  7. #7
    Banned
    Join Date
    Jul 2003
    Location
    New delhi
    Posts
    143

    Re: How to use fields alias name in sql query?

    Hi the alias as name you mantion in the recordset.
    means you fatche the alias name through the qury .
    but you can not generate the alias name in other query , you have to store the value in a temp table or you have to make it a cursor


    a alias name local to that query or thread

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Pakistan
    Posts
    436

    Re: How to use fields alias name in sql query?

    i want to re-use alias name in same query.

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How to use fields alias name in sql query?

    As was already said, you cannot do this.

    SQL offers STORED PROCEDURES and TABLE VARIABLES and other ways to accomplish this.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: How to use fields alias name in sql query?

    Quote Originally Posted by zubairkhan
    Does it not a weakness of sql server 2000, once we got a complicated calculation resulte in a coloumn, which might be 2 or three row in length does it not better to reuse this coloumn again where other calcuation may base upon its result. Besides this we have to apply same formula for each time.
    SQL Server has UDF (User Defined Function) that will help you with this problem. UDFs will also make it easier to read queries with complex calculations since you don't see the calculation in the final query, therefore making it easier to get a good overview. If you want to see how a specific calculation is executed you view the UDF.

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How to use fields alias name in sql query?

    Kaffenils - I agree - UDF's make for taking complex query equations and puts them in a layer below - very appropriate.

    Just keep in mind that doing table I/O in a UDF is usually recommended against - as the query optimizer cannot see that I/O for determining execution plan.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: How to use fields alias name in sql query?

    Quote Originally Posted by szlamany
    Just keep in mind that doing table I/O in a UDF is usually recommended against - as the query optimizer cannot see that I/O for determining execution plan.
    Good point!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width