oracle - Is there a way to compare multiple rows in one column in PL/SQL? -


i've been using sql on 25 years, i'm relatively new oracle. previous experience has been in db2, , sql server.

i asked write script through table following layout:

orderno int
clientid varchar(20)
order_date datetime
productcode varchar(5)
creditlimit money
accountno varchar(25)

the users enter client's creditaccountno on every order. (i know should pulled table, it's homegrown application, , didn't write way. someday, i'll them let me fix that.)

the problem that, while clients have more 1 credit account, have one. users mistakenly enter wrong account no. want me write script looks @ each clientid, , display clients have multiple account numbers in table.

normally, have (pseudocode):

clientid cursor     read distinct clientid     order cursor         read order record clientid         if first iteration, set variable x = accountno         if accountno <> x print clientid, x, accountno         read next row     loop     read next row loop 

i'm wondering if there isn't pl/sql command let me away second loop.

hopefully, question makes sense.

why not this?

select distinct client_id, accountno ( select client_id,         accountno,         count(distinct accountno)              on ( partition client_id) client_account_count your_table )  client_account_count > 1; 

update

added number of rows each account number, suggested in comments justin cave. helps 1 distinguish account numbers correct mistakes.

select distinct client_id, accountno, cnt ( select client_id, accountno,  count(distinct accountno) on ( partition client_id) client_account_count,  count(*) on ( partition client_id, accountno) cnt your_table )  client_account_count > 1; 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

wso2esb - How to concatenate JSON array values in WSO2 ESB? -