Oracle query using DISTINCT

Allen Pitts
Allen Pitts used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Distinguished Expert 2018
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
Distinguished Expert 2017

Commented:
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.
Most Valuable Expert 2012
Distinguished Expert 2018

Commented:
Allen,
I just noticed you posted this in Oracle and Access.

My response is for Oracle.  Pat's is for Access.
Allen PittsBusiness analyst

Author

Commented:
Thanks for the code. Works perfectly.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial