0% found this document useful (0 votes)
18 views

SQL Cod

Uploaded by

Özgür Şahin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

SQL Cod

Uploaded by

Özgür Şahin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CLASS zcl_sql_window_functions DEFINITION

PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun .
TYPES: BEGIN OF ty_students,
student_id TYPE int4,
student_name TYPE c LENGTH 40,
dep_name TYPE c LENGTH 40,
score TYPE int4,
END OF ty_students.
DATA: lt_students TYPE TABLE OF ty_students WITH DEFAULT KEY.
METHODS: constructor.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.

CLASS zcl_sql_window_functions IMPLEMENTATION.


METHOD constructor.
lt_students = VALUE #( ( student_id = 1 student_name = 'Bharathi S' dep_name = 'IT' score = 90 )
( student_id = 2 student_name = 'Dhakshan' dep_name = 'CSE' score = 87 )
( student_id = 3 student_name = 'Shroy' dep_name = 'EEE' score = 95 )
( student_id = 4 student_name = 'Bhuvi' dep_name = 'EEE' score = 78 )
( student_id = 5 student_name = 'Thara' dep_name = 'IT' score = 85 )
( student_id = 6 student_name = 'Anish' dep_name = 'EEE' score = 95 )
( student_id = 7 student_name = 'Aravind' dep_name = 'MECH' score = 91 )
( student_id = 8 student_name = 'Mohan' dep_name = 'CSE' score = 82 )
( student_id = 9 student_name = 'Saurabh' dep_name = 'IT' score = 92 )
( student_id = 10 student_name = 'Rohan' dep_name = 'CSE' score = 65 )
( student_id = 11 student_name = 'Surya' dep_name = 'MECH' score = 72 )
( student_id = 12 student_name = 'Boopalan' dep_name = 'MECH' score = 67 )
( student_id = 13 student_name = 'Shasank' dep_name = 'CSE' score = 75 )
( student_id = 14 student_name = 'Kaviya' dep_name = 'EEE' score = 90 ) ).
ENDMETHOD.

METHOD if_oo_adt_classrun~main.
SELECT FROM @lt_students AS lt_students
FIELDS student_id,
student_name,
dep_name,
score,

" note: If partition by is not mentioned whole data is consider as single result set
SUM( score ) OVER( ) AS total_score,
MAX( score ) OVER( ) AS maximum_score,
MIN( score ) OVER( ) AS minimum_score,
CAST( AVG( score ) OVER( ) AS INT4 ) AS average_score,

SUM( score ) OVER( PARTITION BY dep_name ORDER BY dep_name ASCENDING ) AS dep_total_score,


MIN( score ) OVER( PARTITION BY dep_name ORDER BY dep_name ASCENDING ) AS dep_min_score,
MAX( score ) OVER( PARTITION BY dep_name ORDER BY dep_name ASCENDING ) AS dep_max_score,
CAST( AVG( score ) OVER( PARTITION BY dep_name ORDER BY dep_name ASCENDING ) AS INT4 ) AS
dep_avg_score,

ROW_NUMBER( ) OVER( ORDER BY student_name ) AS name_serial_number,


RANK( ) OVER( PARTITION BY dep_name ORDER BY score DESCENDING ) AS dep_wise_rank_with_gaps,
DENSE_RANK( ) OVER( PARTITION BY dep_name ORDER BY score DESCENDING ) AS
dep_wise_rank_wo_gaps,

LAG( score ) OVER( PARTITION BY dep_name ORDER BY score ) AS prev_score_by_dept,


LEAD( score ) OVER( PARTITION BY dep_name ORDER BY score ) AS next_score_by_dept,

SUM( score ) OVER( ORDER BY dep_name ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT
ROW ) AS cumulative_sum,

FIRST_VALUE( student_id ) OVER( PARTITION BY dep_name ORDER BY score DESCENDING ) AS


top_performer_dep_wise,
LAST_VALUE( student_id ) OVER( PARTITION BY dep_name ORDER BY score DESCENDING ) AS
low_performer_dep_wise,
NTILE( 3 ) OVER( ORDER BY score ) AS group_students_by_3_levels

ORDER BY dep_name, score DESCENDING


INTO TABLE @DATA(lt_result).
out->write( lt_result ).
ENDMETHOD.
ENDCLASS.

OUTPUT:

You might also like