sqloracle-databaseuuidguiddense-rank

Create GUID instead of dense rank for a table data


I have a requirement for writing an SQL query. I am statiing that requirement with a fictional example.

I have an ORACLE database table named COURSE_REGISTRATION_DETAILS which has 3 columns COURSE , SUBJECT, STUDENT. Each course has multiple subjects. Each course can be taken by multiple students. Dual degree is also possible, that is, a student can take up 2 engineering at a time.

Course Subject Student
Electronics and communication engineering Power Electronics Tom
Electronics and communication engineering Radio Communication Tom
Electronics and communication engineering Embedded Systems Tom
Electronics and communication engineering Power Electronics Jerry
Electronics and communication engineering Radio Communication Jerry
Electronics and communication engineering Embedded Systems Jerry
Computer Science engineering Information Theory Popeye
Computer Science engineering Algorithms Popeye
Computer Science engineering Information Theory Tom
Computer Science engineering Algorithms Tom

I need to create a unique GUID for each distinct combination of COURSE and STUDENT. I tried DENSE_RANK(), but it gives a rank integer only. I have a requirement of creating GUID for each combination.

Please help me with coming up a SQL query for doing the same.


Solution

  • You can set the GUID for one row in each partition and then find the maximum GUID over that partition to set all the other rows:

    SELECT course,
           subject,
           student,
           MAX(guid) OVER (PARTITION BY course, student) AS guid
    FROM   (
      SELECT course,
             subject,
             student,
             CASE ROW_NUMBER() OVER (PARTITION BY course, student ORDER BY ROWNUM)
             WHEN 1
             THEN SYS_GUID()
             END AS guid
      FROM   table_name
    )
    

    Which, for the sample data:

    CREATE TABLE table_name (Course, Subject, Student) AS
    SELECT 'Electronics and communication engineering', 'Power Electronics', 'Tom' FROM DUAL UNION ALL
    SELECT 'Electronics and communication engineering', 'Radio Communication', 'Tom' FROM DUAL UNION ALL
    SELECT 'Electronics and communication engineering', 'Embedded Systems', 'Tom' FROM DUAL UNION ALL
    SELECT 'Electronics and communication engineering', 'Power Electronics', 'Jerry' FROM DUAL UNION ALL
    SELECT 'Electronics and communication engineering', 'Radio Communication', 'Jerry' FROM DUAL UNION ALL
    SELECT 'Electronics and communication engineering', 'Embedded Systems', 'Jerry' FROM DUAL UNION ALL
    SELECT 'Computer Science engineering', 'Information Theory', 'Popeye' FROM DUAL UNION ALL
    SELECT 'Computer Science engineering', 'Algorithms', 'Popeye' FROM DUAL UNION ALL
    SELECT 'Computer Science engineering', 'Information Theory', 'Tom' FROM DUAL UNION ALL
    SELECT 'Computer Science engineering', 'Algorithms', 'Tom' FROM DUAL;
    

    Outputs:

    COURSE SUBJECT STUDENT GUID
    Computer Science engineering Information Theory Popeye 0x23E8C93E19AD1A8DE063182BA8C02A1E
    Computer Science engineering Algorithms Popeye 0x23E8C93E19AD1A8DE063182BA8C02A1E
    Computer Science engineering Information Theory Tom 0x23E8C93E19AE1A8DE063182BA8C02A1E
    Computer Science engineering Algorithms Tom 0x23E8C93E19AE1A8DE063182BA8C02A1E
    Electronics and communication engineering Power Electronics Jerry 0x23E8C93E19AF1A8DE063182BA8C02A1E
    Electronics and communication engineering Radio Communication Jerry 0x23E8C93E19AF1A8DE063182BA8C02A1E
    Electronics and communication engineering Embedded Systems Jerry 0x23E8C93E19AF1A8DE063182BA8C02A1E
    Electronics and communication engineering Power Electronics Tom 0x23E8C93E19B01A8DE063182BA8C02A1E
    Electronics and communication engineering Radio Communication Tom 0x23E8C93E19B01A8DE063182BA8C02A1E
    Electronics and communication engineering Embedded Systems Tom 0x23E8C93E19B01A8DE063182BA8C02A1E

    fiddle