Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
SqlGirl87
Starting Member
26 Posts |
Posted - 2012-09-03 : 12:19:07
|
Hi,I am trying to run the below script but get the error "Conversion failed when converting the varchar value 'DIFF' to data type int." - Is it possible to convert the temp computed column in the script to an integer for me to run the and <> '0' clause without having to create this into a temp table and run it that way?Thanks!SELECT A_ID, (ISNULL(val_a,0) - tesbak.total) AS 'DIFF'FROM tesMAST FINNER JOIN (select p_a_id, SUM(ISNULL(val_n,0)) as total from tesmast where and level_number <> '1'group by p_a_id) tesBAK on f.a_id = tesbak.p_a_id WHERE f.level_number ='1' and 'DIFF' <> 0 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2012-09-03 : 13:27:00
|
| You can't use calculated columns from your where clause in the where clauseSELECT A_ID, (ISNULL(val_a,0) - tesbak.total) AS 'DIFF'FROM tesMAST FINNER JOIN (select p_a_id, SUM(ISNULL(val_n,0)) as total from tesmast where and level_number <> '1'group by p_a_id) tesBAK on f.a_id = tesbak.p_a_id WHERE f.level_number ='1' -- is level_number a string ot int? and (ISNULL(val_a,0) - tesbak.total) <> 0JimEveryday I learn something that somebody else already knew |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-03 : 14:57:33
|
or if you want to use aliasSELECT *FROM(SELECT A_ID, (ISNULL(val_a,0) - tesbak.total) AS 'DIFF'FROM tesMAST FINNER JOIN (select p_a_id, SUM(ISNULL(val_n,0)) as total from tesmast where and level_number <> '1'group by p_a_id) tesBAK on f.a_id = tesbak.p_a_id WHERE f.level_number ='1' )tWHERE DIFF <> 0 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
SqlGirl87
Starting Member
26 Posts |
Posted - 2012-09-04 : 04:54:30
|
Thanks Jim for the reponsequote: Originally posted by visakh16 or if you want to use aliasSELECT *FROM(SELECT A_ID, (ISNULL(val_a,0) - tesbak.total) AS 'DIFF'FROM tesMAST FINNER JOIN (select p_a_id, SUM(ISNULL(val_n,0)) as total from tesmast where and level_number <> '1'group by p_a_id) tesBAK on f.a_id = tesbak.p_a_id WHERE f.level_number ='1' )tWHERE DIFF <> 0 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Thanks Visakh that worked a treat! |
 |
|
|
|
|
|
|
|