Oracle query using DISTINCT

Hello expert,

Using the query copied herewith below yields 4758 rows

Running a similar query with
Select DISITNCT  PAYER_ID  
FROM CCM.CCM_COMPONENT_PRICING
  where last_update_user = 1

yields 202 rows.

The 202 rows is needed but with the other fields
resultant  from the longer query.

Tried creating a join with short table against the long table
and tried listing all 202 PAYER_IDs in an 'IN' statement
but both grab all of the PAYER IDs

Is there someway to mimic the return of the DISTINCT
query but return the other fields also?

Thanks

Allen in Dallas


++++++++++++++++LONG QUERY++++++++++++++
Select
COMPONENT_PRICING_ID,
  COMPONENT_ID,
  PAYER_ID,
  VARY_BY_MARKET,
  COMPONENT_PRICE,
  IS_OVERRIDE,
  CREATION_USER,
  CREATION_DATE,
  LAST_UPDATE_USER,
  LAST_UPDATE_DATE
  from
  CCM.CCM_COMPONENT_PRICING
  where last_update_user = 1
LVL 1
Allen PittsBusiness analystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
If you have duplicate payer_ids and some of the other column has different data, which of the rows is valid?

Of you want any of them then this might work:
Select
 COMPONENT_PRICING_ID,
   COMPONENT_ID,
   PAYER_ID,
   VARY_BY_MARKET,
   COMPONENT_PRICE,
   IS_OVERRIDE,
   CREATION_USER,
   CREATION_DATE,
   LAST_UPDATE_USER,
   LAST_UPDATE_DATE
   from 
(
Select
 COMPONENT_PRICING_ID,
   COMPONENT_ID,
   PAYER_ID,
   VARY_BY_MARKET,
   COMPONENT_PRICE,
   IS_OVERRIDE,
   CREATION_USER,
   CREATION_DATE,
   LAST_UPDATE_USER,
   LAST_UPDATE_DATE,
row_number() over(partition by PAYER_ID order by payer_id) rn
   from 
   CCM.CCM_COMPONENT_PRICING
   where last_update_user = 1 
)
where rn=1

Open in new window


It basically sorts by the payer_id column and returns the first one it finds.

If you want the payer_id by the last updated data just change the row_number function to:
row_number() over(partition by PAYER_ID order by payer_id, last_update_date desc) rn

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PatHartmanCommented:
Is there someway to mimic the return of the DISTINCT
query but return the other fields also?
No.  How would the query engine choose which value of the other columns to return?

If you really don't  care which value of COMPONENT_PRICING_ID for example to return, then use  a totals query.  Use the Group By argument for the "distinct" columns and choose a different aggregation function for all the other columns, First, Last, Min, Max are all possible or you can use Sum, Avg, etc for numeric fields if those functions make sense.
slightwv (䄆 Netminder) Commented:
Allen,
I just noticed you posted this in Oracle and Access.

My response is for Oracle.  Pat's is for Access.
Allen PittsBusiness analystAuthor Commented:
Thanks for the code. Works perfectly.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.