hive query for avg of marks?


1 Answer(s)


Hi Rakesh,
you can use split function to split the column with marks to 5 column and then calculate the average of it.

CREATE TABLE demo.marks(marks_string STRING);

INSERT INTO TABLE marks VALUES('20,30,40');
INSERT INTO TABLE marks VALUES('50,60,70'); 
INSERT INTO TABLE marks VALUES('80,90,100');

SELECT marks_string FROM demo.marks;
 
CREATE TABLE subjects(eng STRING,math STRING,comp STRING);
 
INSERT INTO TABLE demo.subjects
SELECT
MARKSS.split_marks[0] AS eng,
MARKSS.split_marks[1] AS math,
MARKSS.split_marks[2] AS comp
FROM
(SELECT
split(marks_string,',') AS split_marks FROM demo.marks)MARKSS;

SELECT eng,math,comp FROM demo.subjects;

SELECT AVG(eng+math+comp)/3 FROM demo.subjects;

 I hope this helps.