Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7769445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:09:42+00:00 2026-06-01T16:09:42+00:00

Edit: Solved with a lot of help from Dems, I will post a truncated

  • 0

Edit: Solved with a lot of help from Dems, I will post a truncated version of the query which is now working very well!

            SELECT
  *

FROM

    O_PERSONS

    /*The below left join returns the most recently added ethnicity classification*/
  LEFT JOIN 

  (SELECT
   ROW_NUMBER() OVER (PARTITION BY O_CLASSIFICATIONS.CLA_SUBJECT_ID ORDER BY O_CLASSIFICATIONS.CLA_DATE_NOTIFIED DESC) AS Sequence_ID,
   O_CLASSIFICATIONS.CLA_CAT_ID,
   O_CLASSIFICATIONS.CLA_SUBJECT_ID
   FROM
   O_CLASSIFICATIONS
   WHERE
   O_CLASSIFICATIONS.CLA_SUBJECT_IND = 'P'
   AND
   O_CLASSIFICATIONS.CLA_TOP_CAT_ID   =  'ETHNIC'
   AND
   O_CLASSIFICATIONS.CLA_CAT_ID <> 'DECLINED'
   ) ETHNIC 
   ON ETHNIC.CLA_SUBJECT_ID = O_PERSONS.PER_ID 
   AND ETHNIC.Sequence_ID = 1

    /*The below left join returns the most recently added PCG classification*/
   LEFT JOIN 

  (SELECT
   ROW_NUMBER() OVER (PARTITION BY O_CLASSIFICATIONS.CLA_SUBJECT_ID ORDER BY O_CLASSIFICATIONS.CLA_DATE_NOTIFIED DESC) AS Sequence_ID,
   O_CLASSIFICATIONS.CLA_CAT_ID,
   O_CLASSIFICATIONS.CLA_SUBJECT_ID
   FROM
   O_CLASSIFICATIONS
   WHERE
   O_CLASSIFICATIONS.CLA_SUBJECT_IND = 'P'
   AND
   O_CLASSIFICATIONS.CLA_TOP_CAT_ID   =  'PRIMARY'
   AND
   O_CLASSIFICATIONS.CLA_CAT_ID <> 'DECLINED'
   ) PCG
   ON PCG.CLA_SUBJECT_ID = O_PERSONS.PER_ID 
   AND PCG.Sequence_ID = 1


  WHERE

   O_PERSONS.PER_ID LIKE 'P%'

I am currently trying to write a query which will return certain details for clients on our system. Whilst a client will only have one date of birth, gender, and P ID, they may have multiple ethnicities (as a result of data quality) and also client groups (this is more legitimate as a client’s needs may change). However, for these particular items, I am only interested in the most recently added ethnicities or client groups. Below I have written a query which assigns a row number to the ethnicity and to the client group with a plan to return only those which equal 1, as these generally will be the most recent. However, I have run into 2 issues, one of which I can resolve, the other of which I’m not so sure.

First off, some clients will not have an ethnicity or a client group recorded. When running the below query, they return with very odd row numbers (often in the thousands). However, I know in my ‘where’ that I could specify that a row is returned if client group or ethnicity was empty (as these will be data quality cases which need to be addressed).

The second issue is slightly trickier which I will try to represent with a table below. Please note I have abbreviated the table for ease of input and understanding.

  P ID Ethnicity   PCG RN E RN P
  P1   WB          OV  1    2 
  P1   WI          OV  2    1          

The above client had two ethnicities, though WB is the most recent (therefore the 1 in Row Number E is correct). However, the client has only had one PCG recorded but the row number returns a 2 in the first row (arguably the row I want to return). I’m not sure why though I guess because ETHNIC.CLA_SUBJECT_ID is joined on O_PERSONS.PER_ID and PER_ID occurs twice, that is why it thinks there is two rows for that particular field. However, even if that is the case, is there a way to force just 1’s to appear in one row? Or could I do this a completely different way? Hopefully this query makes some sense, apologies if parts are unclear. Thanks,

                         SELECT
          O_PERSONS.PER_ID as "P ID",
          olm_bo.get_per_name(O_PERSONS.PER_ID) as "Full Name",
          O_PERSONS.PER_BIRTH_DATE as "Date of Birth",
          case 
            when O_PERSONS.PER_DECEASED_DATE is null then FLOOR(MONTHS_BETWEEN(sysdate,O_PERSONS.PER_BIRTH_DATE)/12) 
            else FLOOR(MONTHS_BETWEEN(O_PERSONS.PER_DECEASED_DATE,O_PERSONS.PER_BIRTH_DATE)/12) 
          end as "Age",
          O_PERSONS.PER_DECEASED_DATE as "Date Deceased",
          olm_bo.get_gender_desc('P', O_PERSONS.PER_GENDER) as "Gender",
          CASE 
            WHEN ETHNIC.CLA_CAT_ID IN ('C1','C2','C3','C4','ABAN','AIND','AOTH','APKN') THEN 'Asian or Asian British'
            ELSE NULL
          End as "Ethnicity - Top" ,
          CASE 
            WHEN ETHNIC.CLA_CAT_ID IN ('BAFR','D2') THEN 'African'
            ELSE NULL
          End as "Ethnicity - Detail" ,
          CASE 
            WHEN PCG.CLA_CAT_ID IN ('ASYLUM','REFUGEE') THEN 'Asylum Seeker/Refugee'
            ELSE NULL
          End as "PCG",
          CASE 
            WHEN PCG.CLA_CAT_ID IN ('ASYLUM','REFUGEE') THEN 'Asylum Seeker/Refugee'
            ELSE NULL
          End as "PCG - Top",
          CASE 
            WHEN PCG.CLA_CAT_ID IN ('ASYLUM','REFUGEE') THEN 'Asylum Seeker/Refugee'
            ELSE NULL
          End as "PCG - DETAIL",

          to_char(row_number() over(PARTITION BY  ETHNIC.CLA_SUBJECT_ID ORDER BY abs(sysdate - ETHNIC.CLA_DATE_NOTIFIED) asc)) as "Row Number E",
          to_char(row_number() over(PARTITION BY  PCG.CLA_SUBJECT_ID ORDER BY abs(sysdate - PCG.CLA_DATE_NOTIFIED) asc))as "Row Number P"

        FROM

          O_PERSONS

          LEFT JOIN O_CLASSIFICATIONS ETHNIC ON ETHNIC.CLA_SUBJECT_ID = O_PERSONS.PER_ID 
          AND ETHNIC.CLA_SUBJECT_IND = 'P'
          AND ETHNIC.CLA_TOP_CAT_ID   =  'ETHNIC'
          AND ETHNIC.CLA_CAT_ID <> 'DECLINED'

          LEFT JOIN O_CLASSIFICATIONS PCG ON PCG.CLA_SUBJECT_ID = O_PERSONS.PER_ID 
          AND PCG.CLA_SUBJECT_IND = 'P'
          AND PCG.CLA_TOP_CAT_ID   =  'PRIMARY'
          AND PCG.CLA_CAT_ID <> 'DECLINED'

          WHERE
          /*Following line excludes any clients whose is less than 18)*/
            O_PERSONS.PER_BIRTH_DATE > trunc(add_months(O_PERSONS.PER_BIRTH_DATE,-216))
            AND  O_PERSONS.PER_ID LIKE 'P%'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:09:43+00:00Added an answer on June 1, 2026 at 4:09 pm

    You should apply ROW_NUMBER() before you do the joins.

    SELECT
      *     -- Your calculations here, '*' used for brevity
    FROM
      O_PERSONS
    LEFT JOIN
    (
      SELECT
        ROW_NUMBER() OVER (PARTITION BY CLA_SUBJECT_ID ORDER BY CLA_DATE_NOTIFIED DESC) AS sequence_id,
        *
      FROM
        O_CLASSIFICATIONS
      WHERE
            CLA_SUBJECT_IND =  'P'
        AND CLA_TOP_CAT_ID  =  'ETHNIC'
        AND CLA_CAT_ID      <> 'DECLINED'
    )
      ETHNIC
        ON  ETHNIC.CLA_SUBJECT_ID = O_PERSONS.PER_ID
        AND ETHNIC.sequence_id    = 1
    LEFT JOIN
    (
      SELECT
        ROW_NUMBER() OVER (PARTITION BY CLA_SUBJECT_ID ORDER BY CLA_DATE_NOTIFIED DESC) AS sequence_id,
        *
      FROM
        O_CLASSIFICATIONS
      WHERE
            CLA_SUBJECT_IND =  'P'
        AND CLA_TOP_CAT_ID  =  'PRIMARY'
        AND CLA_CAT_ID      <> 'DECLINED'
    )
      PCG
        ON  PCG.CLA_SUBJECT_ID = O_PERSONS.PER_ID
        AND PCG.sequence_id    = 1
    WHERE
         /*Following line excludes any clients whose is less than 18)*/
         O_PERSONS.PER_BIRTH_DATE > trunc(add_months(O_PERSONS.PER_BIRTH_DATE,-216))
    AND  O_PERSONS.PER_ID LIKE 'P%'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit : Solved, there was a trigger with a loop on the table (read
Edit: Solved. Hi, I'm starting with Qt, I try to connect a slot to
edit: Solved - mod_rewrite was the problem I can't get CI to work as
EDIT: SOLVED Thanks Brooks. Your question led me to keep digging into if the
EDIT: solved it, turns out I should use %c not %s because foodSelect and
[EDIT: Problem solved. Please see my answer below.] In my app I call the
edit #2: Question solved halfways. Look below As a follow-up question, does anyone know
Edit: I have solved this by myself. See my answer below I have set
Answer solved in edit below I had this piece of code Dictionary<Merchant, int> remaingCards
EDIT: This problem has been solved. See below. Hey all. I'm building an iPhone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.