|
-
Oct 25th, 2005, 04:29 AM
#1
Thread Starter
Hyperactive Member
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:
.Open "select b.trdate,e.jobno,e.jobdesc,p.partyname,b.manualbillid as BillNo , " & _
"sum(b.billedqty) as Quantity,b.billedrate as Rate,round(sum(b.billedamount),0) as Amount, " & _
"round(amount*15/100,'0') as GSt,round(sum(b.billedamount*15/100),0) + round(sum(b.billedamount),0) as TotValue " & _
"FROM enquiry e,parties p, bills b " & _
" " & FilterText & " " & _
"GROUP BY b.trdate,e.jobno,e.jobdesc,p.partyname,b.manualbillid, " & _
"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
-
Oct 25th, 2005, 05:04 AM
#2
Fanatic Member
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. 
-
Oct 25th, 2005, 07:19 AM
#3
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?
-
Oct 25th, 2005, 10:36 PM
#4
Thread Starter
Hyperactive Member
Re: How to use fields alias name in sql query?
Well, it is working in access why not in sql server 2000
-
Oct 26th, 2005, 11:26 AM
#5
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, ...).
-
Oct 26th, 2005, 10:51 PM
#6
Thread Starter
Hyperactive Member
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.
-
Oct 27th, 2005, 01:37 AM
#7
Banned
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
-
Oct 27th, 2005, 02:43 AM
#8
Thread Starter
Hyperactive Member
Re: How to use fields alias name in sql query?
i want to re-use alias name in same query.
-
Oct 27th, 2005, 05:58 AM
#9
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.
-
Oct 27th, 2005, 07:03 AM
#10
Re: How to use fields alias name in sql query?
 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.
-
Oct 27th, 2005, 08:02 AM
#11
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.
-
Oct 27th, 2005, 08:23 AM
#12
Re: How to use fields alias name in sql query?
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|