Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Query to compare the contents of a field using PL/SQL

Posted on 2007-11-19
Medium Priority
1,544 Views
I need to compare the contents of a field to make sure that the fields have the same value.  For example here is the table stucture:

PONUM              ITEMNUM         POLINENUM         CCNUMBER
1234                   3214                       1                    012345678
1234                   4325                       2                    987654321

I need to compare the contents of CCNUMBER to make sure they are the same.  How would I best handle this task using PL/SQL?
0
Question by:ewgf2002
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 3
• 2

LVL 143

Expert Comment

ID: 20312823
>I need to compare the contents of CCNUMBER to make sure they are the same.
can you explain a bit more what you mean with "they need to be the same"?

I guess, for the same PONUM you must have the same CCNumber?
0

Author Comment

ID: 20312837
That is correct.
0

LVL 143

Expert Comment

ID: 20312851
``````select PONUM  , max(CCNUMBER), min(CCNUMBER)
from yourtable
GROUP BY PONUM
HAVING max(CCNUMBER) <> min(CCNUMBER)
``````
0

LVL 74

Expert Comment

ID: 20312858
I wouldn't use pl/sql at all.  I'd do it with sql.

SELECT *
FROM (SELECT ponum, ccnumber, COUNT(DISTINCT ccnumber) OVER(PARTITION BY ponum) cnt
FROM my_table)
WHERE cnt > 1
0

LVL 74

Expert Comment

ID: 20312891
select PONUM  , max(CCNUMBER), min(CCNUMBER)
from yourtable
GROUP BY PONUM
HAVING max(CCNUMBER) <> min(CCNUMBER)

This doesn't work if ccnumber has any nulls in it

0

Author Comment

ID: 20312920
What if CCNUMBER does have nulls
0

LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 1000 total points
ID: 20312975
if there are nulls, you can try this
``````select PONUM  , max(CCNUMBER), min(CCNUMBER)
from yourtable
GROUP BY PONUM
HAVING max(NVL(CCNUMBER, 'XXXXXXXXXXXXXXXXXX')) <> min(NVL(CCNUMBER,'000000000000000000'))
``````
0

LVL 74

Assisted Solution

sdstuber earned 1000 total points
ID: 20312988
you'll need to nvl them to some dummy value
actually same thing applies to my query as well.
I noticed that as soon as I posted the hole in angeliii's query, mine had the same hole.  :)

SELECT *
FROM (SELECT ponum, ccnumber, COUNT(DISTINCT NVL(ccnumber, -999)) OVER(PARTITION BY ponum) cnt
FROM my_table)
WHERE cnt > 1;

select PONUM  , max(nvl(CCNUMBER,-999)), min(nvl(CCNUMBER,-999))
from yourtable
GROUP BY PONUM
HAVING max(nvl(CCNUMBER,-999)) <> min(nvl(CCNUMBER,-999))

0

LVL 74

Expert Comment

ID: 20313062
note,  my version of angeliii's updated query doesn't return the same results as his updated version.

If all ccnumbers for a ponum are null, I count that as the same ccnumber.
angeliii does not

both are reasonable answers to your question, only you can decide which is more appropriate for your application though.

We both count a mix of null and not null as different ccnumber's.

0

## Featured Post

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For manyâ€¦
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
###### Suggested Courses
Course of the Month8 days, 2 hours left to enroll