2

I have a dataset

case_id    subcase_id
1      | 1-1
1      | 1-2
1      | 1-3
1      | 1-6
2      | 2-1
2      | 2-7

I want the following output. The idea is to count the occurence of a subcase corresponding to a case.

case_id  subcase_id
1      | 1-1          | 1
1      | 1-2          | 2
1      | 1-3          | 3
1      | 1-6          | 4
2      | 2-1          | 1
2      | 2-7          | 2
4
  • What is your MySQL server version ? Commented Oct 30, 2018 at 3:46
  • I am using oracle 12 Commented Oct 30, 2018 at 3:47
  • Please remove the MySQL tag. Oracle <> Mysql Commented Oct 30, 2018 at 3:47
  • Does the leading number in subcase_id always map to the case_id ? Can the trailing number in the subcase_id extend into two or more digits? Does it matter what order the count is in? Commented Oct 30, 2018 at 7:34

3 Answers 3

3

You can try using row_number() function

select 
   caseid,
   subcase_id, 
   row_number() over(partition by caseid 
      order by 
      cast(SUBSTR(subcase_id, 1,INSTR(subcase_id, '-') -1) as number), 
      cast(SUBSTR(subcase_id, INSTR(subcase_id, '-') +1) as number)) as rn
from tablename
Sign up to request clarification or add additional context in comments.

4 Comments

This might not sort properly with a subcase_id like 10-12, because then it would sort as text, and not as separate numbers.
Yes I know @TimBiegeleisen
Check this demo to see what I mean ^ ^
@TimBiegeleisen thanks I knew that it's string and I need to split it and then convert it to number
1

You may use count() over (partition by .. order by ..) clause as :

    with t(case_id,subcase_id) as
    (   
     select 1,'1-1' from dual union all
     select 1,'1-2' from dual union all
     select 1,'1-3' from dual union all
     select 1,'1-6' from dual union all
     select 2,'2-1' from dual union all
     select 2,'2-7' from dual
    )
     select t.*,
            count(*) over (partition by case_id order by subcase_id)              
            as result
       from t;

    CASE_ID     SUBCASE_ID   RESULT
    -------     ----------   ------ 
       1            1-1        1
       1            1-2        2
       1            1-3        3
       1            1-6        4
       2            2-1        1
       2            2-7        2

where subcase_id is changes frequently and distinct for all values while case_id changes rarely.

Rextester Demo

Comments

0

Here is a query which should behave as you want. We have to isolate the two numeric components of the subcase_id, and then cast them to integers, to avoid sorting this column as text.

SELECT
    case_id,
    subcase_id,
    ROW_NUMBER() OVER (PARTITION BY case_id
        ORDER BY TO_NUMBER(SUBSTR(subcase_id, 1, INSTR(subcase_id, '-') - 1)),
                 TO_NUMBER(SUBSTR(subcase_id, INSTR(subcase_id, '-') + 1))) rn
FROM yourTable
ORDER BY
    case_id,
    TO_NUMBER(SUBSTR(subcase_id, 1, INSTR(subcase_id, '-') - 1)),
    TO_NUMBER(SUBSTR(subcase_id, INSTR(subcase_id, '-') + 1));

Demo

It is not a good idea to treat the subcase_id column as both text and numbers. If you really have a long term need to sort on this column, then I suggest breaking out the two numeric components as separate number columns.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.