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 2005 Forums
 Transact-SQL (2005)
 Help with Row Comparison

Author  Topic 

srujanavinnakota
Starting Member

34 Posts

Posted - 2010-07-02 : 13:08:40
Hi,

Can anyone help me with the logic.
I have a scenario where I need to check for the value 50 in OldVal and NewVal columns for each ID.

No Matter where it is, If OldVAl Or NewVal is 50 then it need to pick up the ID.


ID OldVal NewVal
1 100 10
1 10 50
1 50 150
1 150 100
2 10 1
3 300 30
3 30 50
4 50 100
4 100 50
4 50 150
5 300 30
5 30 10

Thanks In Advance.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-02 : 13:20:34
SELECT ID
FROM YourTable
WHERE OldVal = 50 OR NewVal = 50

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

srujanavinnakota
Starting Member

34 Posts

Posted - 2010-07-02 : 13:31:24
Thanks for your help.

Also can you please tell me how can I count the no of times it has been triggered to 50 in both oldval or newval columns.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-02 : 13:38:34
Do you mean this?:

SELECT COUNT(*)
FROM YourTable
WHERE OldVal = 50 OR NewVal = 50

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-07-02 : 13:38:35
SELECT ID,COUNT(*)
FROM YourTable
WHERE OldVal = 50 OR NewVal = 50
GROUP BY ID
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-07-02 : 13:39:32
Hmm...1 sec...
Go to Top of Page

srujanavinnakota
Starting Member

34 Posts

Posted - 2010-07-02 : 13:39:47
Thank you.

quote:
Originally posted by tkizer

Do you mean this?:

SELECT COUNT(*)
FROM YourTable
WHERE OldVal = 50 OR NewVal = 50

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

Go to Top of Page

srujanavinnakota
Starting Member

34 Posts

Posted - 2010-07-02 : 13:40:21
Thank you


quote:
Originally posted by vijayisonly

SELECT ID,COUNT(*)
FROM YourTable
WHERE OldVal = 50 OR NewVal = 50
GROUP BY ID

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-02 : 13:43:57
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-02 : 13:44:41
quote:
Originally posted by vijayisonly

Hmm...1 sec...



They're different solutions, so not a snipe. I wasn't sure which one the OP wanted, so I went with the one that had less typing.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -