user3606437
user3606437

Reputation: 1

PL/SQL: How to compare 2 sets of data

if I have 2 rows stored in a table with just 3 columns (ID, Name, Salary) to make it simple:

Row 1: 123, Sal, 1000
Row 2: 123, Sal, 1001

How can I write a pl/sql program to compare row 1 data (column by column) with Row 2? Of course in my example, there will be a lot more columns and rows. I just need to be able to compare 2 rows (column by column) and verify they have the same values.

Upvotes: 0

Views: 2574

Answers (3)

Calipso
Calipso

Reputation: 967

you can use minus. if therows are not the same it will return a record. if they are the same nothing will return

select name, salary
  from table
 where id = 1

minus

select name, salary
  from table
 where id = 2

Upvotes: 0

xacinay
xacinay

Reputation: 909

For such a general form of question, I suppose GROUP BY would be the answer. So, in your terms:

  select ID, Name, Salary from Salaries
  group by ID, Name, Salary
  having count(*) > 1

This sql statement will output all records with equal "ID, Name, Salary" field values (due 'having' condition).

Upvotes: 1

Ronnis
Ronnis

Reputation: 12833

Here is a start that gets you two rows side-by-side.

select r1.id
      ,r1.name
      ,r1.salary
      ,r2.id
      ,r2.name
      ,r2.salary      
  from your_data r1
      ,your_data r2
 where r1.id = 1
   and r2.id = 2 

Upvotes: 0

Related Questions