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
 General SQL Server Forums
 New to SQL Server Programming
 SQL compare column in table

Author  Topic 

pamyral_279
Posting Yak Master

143 Posts

Posted - 2006-08-15 : 22:40:03
I have table with the following structure :
ID Result1 Reusult2 NewResult1 NewResult2 FinalResult
-1----10------6-----------7------------9----
-2----32------16----------32-----------16---
-3----12------2-----------1------------9----
-4----50------6-----------9------------7----
-5----90------9-----------90-----------9----
-6----18------2-----------3------------2----



My problem :

I want to compare each record in column Result1 and NewResult1,Result2 and NewResult2
Such as my above table :
With ID=2 and ID=5 : I need to update column "Final Result" with value equal 1 ,otherwise any records with value equal zero.
Conclusion :
If ((Result1=NewResult1) and (Result2=NewResult2)) --> Final Result =1
else
Other Records -->Final Result=0
How to write sql statement ?
Thank you very much !

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-08-15 : 22:53:31
"With ID=2 and ID=5"
you mean ID = 2 or ID = 5 ?


KH

Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-08-15 : 22:57:41
[code]
update MyTable
set
FinalResult =
case
when Result1 = NewResult1 and
Result2 = NewResult2
then 1
else 0 end
where
FinalResult is null or
FinalResult <>
case
when Result1 = NewResult1 and
Result2 = NewResult2
then 1
else 0 end
[/code]

CODO ERGO SUM
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-08-15 : 23:07:59
Oh ! that's what pamyral_279 required


KH

Go to Top of Page

pamyral_279
Posting Yak Master

143 Posts

Posted - 2006-08-16 : 02:16:25
This is great forum !
Can you show me some link guide sql statement like yours !
Thank you very much!


quote:
Originally posted by Michael Valentine Jones


update MyTable
set
FinalResult =
case
when Result1 = NewResult1 and
Result2 = NewResult2
then 1
else 0 end
where
FinalResult is null or
FinalResult <>
case
when Result1 = NewResult1 and
Result2 = NewResult2
then 1
else 0 end


CODO ERGO SUM

Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-08-16 : 02:22:47
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp

Chirag
Go to Top of Page
   

- Advertisement -