Solved

Sql query to compare where rows have the same value in different columns

Posted on 2014-04-02
9
7,532 Views
Last Modified: 2014-04-02
Have a sample table as follows:

  ID     Old    New
123       a         b
123       b         a
456       a         b
456       b         c

Would like to write a query to select where an ID has the same Old value as it does a New value.

Basically, looking for where the old value was changed and then changed back to the current value, and what those 2 values would be.

So the result set I want back is:

  ID     Old    New    Newest
123      a          b          a

I would not want to get any of the 456 IDs back because that went from a to b to c, not back to a again.

Hope this is enough information.

Thanks!
0
Comment
Question by:flip4h
  • 3
  • 3
  • 3
9 Comments
 
LVL 73

Expert Comment

by:sdstuber
ID: 39972642
how do we know the order of the rows?


123       a         b
123       b         a

could just as easily be

123       b         a
123       a         b


we have no way of knowing by looking at your data whether it changed from A to B to A
or from B to A to B
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 39972647
If we can make the assumption that order should be alphabetical based on the "old" column then try this...


SELECT o.old, o.new, n.new
  FROM yourtable o, yourtable n
 WHERE o.id = n.id AND o.new = n.old AND n.old > o.old

If there is some other criteria for determining which row came first, then please elaborate
0
 
LVL 32

Expert Comment

by:awking00
ID: 39972662
Is there any sort of change date available or perhaps some other indicator as to the order of change?
0
 

Author Comment

by:flip4h
ID: 39972693
Thanks for looking into this.

Yes, there would be a change date associated, I created a dummy "table" for this example.  Just think the order it is in is the indicator of change, added a Change# as the indicator:

So table could be this:

  ID     Change#    Old    New
123         1             a         b
123         2             b         a
456         1             a         b
456         2             b         c
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 32

Expert Comment

by:awking00
ID: 39972713
Is it possible for it to be something like this?
123         1             a         b
123         2             b         a
456         1             a         b
456         2             b         c
456         2             c         b  (or a)
0
 
LVL 32

Expert Comment

by:awking00
ID: 39972719
The last line should have had a Change# of 3
0
 

Author Comment

by:flip4h
ID: 39972749
Yes that scenario would be possible.

A little more background on scenario

This table is an audit table, when a change is made, a new record is created.  So could have numerous change#.  Trying to find out if an ID is being changed and then changed back.  So it would not matter what change# it is, just whether or not for the same ID if that record had an old value that matched the new value (but going at least in the order of the change#).

So in the scenario you just gave, no matter if that chnage# 3 has a or b as its new value, it would need to show.

Because
Change# 1 has a as old value
Change# 2 has b as old value
So if Change# 3 has a as new value, then because 3 > 1 and change# 1 old = change# 3 new, then it would show
If Change# 3 has b as new value, then because 3 > 2 and change# 2 old = change# 3 new, then it would show
Hope this explains it more!

Thanks again.
0
 

Author Comment

by:flip4h
ID: 39972803
Which I believe using sdstuber's query, and modifying it to look like:

SELECT o.old, o.new, n.new
  FROM yourtable o, yourtable n
 WHERE o.task_id = n.task_id AND o.old_value = n.new_value and o.change# < n.change#

Should give me exactly what I need then, correct?
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 39972866
yes


  the only thing missing from my query was the sorting rule, so I made up my own.

If you have a change# to use for sorting, then, as noted above, use that
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
null value 15 94
Remove alpha from alphanumeric 4 60
T-SQL Update Table using Input Parameter as Column Name 6 41
SQL Union 20 44
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now