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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Comparison of fields

Author  Topic 

flchico
Starting Member

46 Posts

Posted - 2007-06-14 : 16:24:08
Hi,

I have the following Query:

SELECT a.iSO_id,
(SELECT TOP 1 cItemCode
FROM ACC_Item_Codes zz
WHERE zz.cGroup_id = e.cGroup_id AND
RIGHT('0000000' + CAST(a.iUPC_Mfg AS VARCHAR(7)), 7) +
RIGHT('0000000' + CAST(a.iUPC_Item AS VARCHAR(7)), 7) +
RIGHT('0000000' + CAST(a.iUPC_Case AS VARCHAR(7)), 7) = zz.cItemCodeKey_id_MIC) AS cItemCode_MIC,
(SELECT TOP 1 cItemCode
FROM ACC_Item_Codes zzz
WHERE zzz.cGroup_id = e.cGroup_id AND
RIGHT('0000000' + CAST(a.iUPC_Mfg AS VARCHAR(7)), 7) +
RIGHT('0000000' + CAST(a.iUPC_Item AS VARCHAR(7)), 7) = zzz.cItemCodeKey_id_MI) AS cItemCode_MI,
(SELECT TOP 1 cItemcode
FROM ACC_Item_Codes zzzz
WHERE zzzz.cGroup_id = e.cGroup_id AND
RIGHT('0000000' + CAST(a.iUPC_Mfg AS VARCHAR(7)), 7) +
RIGHT('0000000' + CAST(a.iUPC_Case AS VARCHAR(7)), 7) = zzzz.cItemCodeKey_id_MC) AS cItemCode_MC
FROM ACC_SO_Dtl a
LEFT JOIN ACC_SO b
ON a.iSO_id = b.iSO_id
LEFT JOIN ACC_Division e
ON b.cTo_id = e.cDivision_id
WHERE a.iSO_id = 38618

I have these columns 'cItemCode_MIC','cItemCode_MI' AND 'cItemCode_MC' and I need an extra one doing a comparison between these 3 columns to come up with the extra column.

I know I can just add another column but I have to write all the SQL Statements again and I'm thinking that's very inneficient and the query is gonna take twice as long.

Is there a way to tell SQL Server, Take Column 2 and Compare it to Column1 and Column3, without forcing SQL Server to run the column subqueries again?

Thanks in Advance.

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-06-14 : 16:31:39
If you dont want it to do the calculation again, get the result of above SELECT into a table variable and run an UPDATE on it. Then, do a SELECT from the table variable for the final result.

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

flchico
Starting Member

46 Posts

Posted - 2007-06-14 : 16:47:27
Thanks,

I guess it can't be done. That's been my only option right now, to grab the result and do the comparison in my app. I was able to solve this issue by moving the Column Selects to LEFT JOINS and then I could compare in the column, worked great until I saw sometimes there is more than one record returned for a JOIN when and it's supposed to return at most one record per JOIN, so I needed to do a TOP 1 that I don't know how to handle in the JOIN.
Go to Top of Page

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-06-14 : 17:17:05
Yes if you have an option you can do it at the front end, if there arent too many rows being returned.

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page
   

- Advertisement -