CREATE TABLE INQUIRIES_DEMOGRAPHICAL_AND_ACADEMICAL (
	ID_INTERNAL INT(11) NOT NULL PRIMARY KEY,
	KEY_EXECUTION_PERIOD INT(11),
    KEY_EXECUTION_DEGREE INT(11),
    KEY_STUDENT_SCHOOL_CLASS INT(11),
    KEY_REGISTRATION INT(11),
    STUDENT_CURRICULAR_YEAR INT(11),
    AGE INT(11),
    GENDER TEXT,
    DAY_PERIOD_TYPE TEXT,
    IS_WORKING_STUDENT TINYINT(1),
    REPROVED_ANY_COURSE TINYINT(1),
    NUM_REPROVED_COURSES INT(11),
    INGRESS_UNIVERSITY_FIRST_CHOICE TINYINT(1),
    INGRESS_DEGREE_FIRST_CHOICE TINYINT(1),
	IS_DISLOCATED TINYINT(1),
	HAS_SOCIAL_SERVICES_SCHOLARSHIP TINYINT(1),
	MOTHER_SCHOLARITY_LEVEL TEXT,
	FATHER_SCHOLARITY_LEVEL TEXT,
	MOTHER_PROFESSIONAL_SITUATION TEXT,
	FATHER_PROFESSIONAL_SITUATION TEXT,
	DEGREE_THEORETICAL_PART_CLASSIFICATION DOUBLE,
	DEGREE_PRACTICAL_PART_CLASSIFICATION DOUBLE,
	DEGREE_COURSEWARE_DIVISION_CLARITY DOUBLE,
	DEGREE_GROUP_WORK_LEARNING DOUBLE,
	DEGREE_IDEAS_AND_COMMUNICATION_CLASSIFICATION DOUBLE,
	DEGREE_JOB_MARKET_INTEGRATION DOUBLE,
	DEGREE_COURSES_TOTAL_NUMBER DOUBLE,
	DEGREE_WEEKLY_LOAD DOUBLE,
	DEGREE_EVALUATION_ELEMENTS_NUMBER DOUBLE,
	DEGREE_COURSE_SHIFTS_SIZE DOUBLE,
	DEGREE_WORK_VOLUME DOUBLE,
	DEGREE_GLOBAL_SATISFACTION DOUBLE,
	UNIVERSITY_GLOBAL_SATISFACTION DOUBLE,
	NUM_REPROVED_YEARS INT(11),
	REPROVED_ANY_YEAR TINYINT(1),
	CREATION_ENTITY TEXT,
	CREATION_ENTITY_DATE DATE,
	KEY_ROOT_DOMAIN_OBJECT INT(11) DEFAULT 1,
    KEY `KEY_ROOT_DOMAIN_OBJECT` (`KEY_ROOT_DOMAIN_OBJECT`)
) ENGINE = INNODB;


INSERT INTO INQUIRIES_DEMOGRAPHICAL_AND_ACADEMICAL 
(KEY_EXECUTION_PERIOD,KEY_EXECUTION_DEGREE,KEY_ROOT_DOMAIN_OBJECT,ID_INTERNAL,KEY_STUDENT_SCHOOL_CLASS,STUDENT_CURRICULAR_YEAR)
SELECT KEY_EXECUTION_PERIOD,
		KEY_EXECUTION_DEGREE_STUDENT,
		KEY_ROOT_DOMAIN_OBJECT,
		ID_INTERNAL,
		KEY_STUDENT_SCHOOL_CLASS,
		STUDENT_CURRICULAR_YEAR
FROM INQUIRIES_COURSE GROUP BY KEY_EXECUTION_PERIOD,
KEY_EXECUTION_DEGREE_STUDENT,
KEY_ROOT_DOMAIN_OBJECT;

alter table INQUIRIES_COURSE ADD COLUMN KEY_INQUIRIES_DEMOGRAPHICAL_AND_ACADEMICAL INT(11);

update INQUIRIES_COURSE X 
SET X.KEY_INQUIRIES_DEMOGRAPHICAL_AND_ACADEMICAL=
	(SELECT Y.ID_INTERNAL FROM INQUIRIES_DEMOGRAPHICAL_AND_ACADEMICAL Y WHERE 
	 	Y.KEY_EXECUTION_DEGREE=X.KEY_EXECUTION_DEGREE_STUDENT AND Y.KEY_ROOT_DOMAIN_OBJECT=X.KEY_ROOT_DOMAIN_OBJECT
	 	AND Y.KEY_EXECUTION_PERIOD=X.KEY_EXECUTION_PERIOD);
