Solved

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

Posted on 2007-11-19
9
1,539 Views
Last Modified: 2013-12-07
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
Comment
Question by:ewgf2002
  • 4
  • 3
  • 2
9 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
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

by:ewgf2002
ID: 20312837
That is correct.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20312851
what about this code to identify them:
select PONUM  , max(CCNUMBER), min(CCNUMBER)

from yourtable

GROUP BY PONUM

HAVING max(CCNUMBER) <> min(CCNUMBER)

Open in new window

0
 
LVL 73

Expert Comment

by:sdstuber
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 73

Expert Comment

by:sdstuber
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

by:ewgf2002
ID: 20312920
What if CCNUMBER does have nulls
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 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'))

Open in new window

0
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 250 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 73

Expert Comment

by:sdstuber
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Action link in Union Reports Not Working in OBIEE 11g 1 54
How to return an OUT parameter from and ORACLE 3 55
oracle 11g 23 47
sort a spool into file output in oracle 1 22
Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

911 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