Home > DeveloperSection > Forums > compare two fields of a single table in oracle sql
Mark Devid
Mark Devid

Total Post:102

Points:714
Posted on    May-09-2013 3:38 AM

 MSSQL Server MSSQL Server 
Ratings:


 1 Reply(s)
 972  View(s)
Rate this:
Hi All!

I'm having a table like below, I need to write a sql query to compare and validate the records are in proper order. ex: US is having no1 in column2 like that each 

country have their own no's , I need to write a code to validate that no country has the same no's assigned for more than one country and need to check weather the country has his assigned no only.

My table structure as per following

| column1 | column2 |
--------------------------------
| US   | no1     |
| Japan | no2     |
| dravid | no3 |

Please help me as soon as possible.



AVADHESH PATEL

Total Post:604

Points:4228
Posted on    May-09-2013 8:02 AM

Hi Mark!

Try as following

SELECT 
    column2, COUNT(*) TotalCount
FROM YourTable
GROUP BY column2
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
where TotalCount returns the count of number assigned multiple times. or

select * From(    
  SELECT distinct Column1, Column2, COUNT(*) over (partition by Column2) TotalCount
  FROM YourTable
)x 
where TotalCount>1

I hope it resolve your problem

Don't want to miss updates? Please click the below button!

Follow MindStick