0

I have 4 mySQL tables namely Exams, Marks, Student and Subject. Sql with data is provided below. I need the output like below screenshot.

enter image description here

Marks is calculated from all Marks added together for the particular subject and student pair with their proper Weightage in Percentage (Exams with 0 Weightage are ignored)

I tried using the below query but the result didn't gave the cumulative marks so need help.

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
               CONCAT('MAX(IF(sj.SubjectId = ''', SubjectId,''', pa.Marks, NULL)) AS ',SubjectId)
              ) INTO @sql
FROM Subject;

SET @sql = CONCAT('SELECT s.ID, s.StudentID, s.FirstName, s.LastName, ', @sql, ' 
                  FROM Student s
                  JOIN Marks AS pa 
                  ON pa.StudentID = s.StudentID AND pa.OrganizationId = s.OrganizationId 
                  JOIN Exams p
                  ON p.ExamId = pa.ExamId AND p.OrganizationId = pa.OrganizationId
                  JOIN Subject sj
                  ON p.SubjectId = sj.SubjectId AND pa.OrganizationId = sj.OrganizationId
                  WHERE p.Weightage > 0
                  GROUP BY s.ID');

PREPARE stmt FROM @sql;
EXECUTE stmt;

Please help. FIDDLE LINK


CREATE TABLE `Exams` (
  `ID` int(6) NOT NULL,
  `ExamId` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `Name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  `Date` datetime(6) DEFAULT NULL,
  `TotalMarks` int(6) NOT NULL,
  `SubjectId` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  `Weightage` int(6) NOT NULL DEFAULT '0',
  `OrganizationId` int(6) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `Exams`
--

INSERT INTO `Exams` (`ID`, `ExamId`, `Name`, `Date`, `TotalMarks`, `SubjectId`, `Weightage`, `OrganizationId`) VALUES
(8, 'EX_0001', 'Test 1', '2020-05-30 17:15:38.000000', 50, 'SUB_0002', 0, 116),
(9, 'EX_0002', 'Test 2', '2020-05-17 17:15:19.000000', 30, 'SUB_0001', 0, 116),
(10, 'EX_0003', 'Test 3', '2020-05-17 17:15:51.000000', 30, 'SUB_0003', 10, 116),
(11, 'EX_0004', 'Test 45', '2020-05-19 15:15:08.000000', 30, 'SUB_0001', 0, 116),
(12, 'EX_0005', 'Final Exam', '2020-05-20 15:30:53.000000', 100, 'SUB_0001', 80, 116),
(13, 'EX_0006', 'Terminal 3', '2020-05-20 15:30:03.000000', 50, 'SUB_0001', 10, 116);

-- --------------------------------------------------------

--
-- Table structure for table `Marks`
--

CREATE TABLE `Marks` (
  `ID` int(11) NOT NULL,
  `StudentId` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  `ExamId` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  `Marks` int(6) NOT NULL,
  `OrganizationId` int(6) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `Marks`
--

INSERT INTO `Marks` (`ID`, `StudentId`, `ExamId`, `Marks`, `OrganizationId`) VALUES
(14, 'S_100000001', 'EX_0004', 30, 116),
(15, 'S_100000001', 'EX_0003', 25, 116),
(16, 'S_100000001', 'EX_0002', 77, 116),
(17, 'S_100000003', 'EX_0003', 15, 116),
(18, 'S_100000003', 'EX_0004', 12, 116),
(19, 'S_100000003', 'EX_0001', 12, 116),
(20, 'S_100000002', 'EX_0004', 20, 116),
(21, 'S_100000002', 'EX_0003', 21, 116),
(22, 'S_100000001', 'EX_0005', 80, 116),
(23, 'S_100000002', 'EX_0005', 90, 116);

-- --------------------------------------------------------

--
-- Table structure for table `Student`
--

CREATE TABLE `Student` (
  `ID` int(6) NOT NULL,
  `GradeId` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
  `StudentID` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
  `OrganizationId` int(6) DEFAULT NULL,
  `FirstName` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  `LastName` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  `FatherFirstName` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
  `FatherLastName` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  `DateOfBirth` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
  `PlaceOfBirth` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
  `Sex` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
  `Carnet` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
  `MobilePhone` bigint(8) DEFAULT NULL,
  `Address` varchar(60) COLLATE utf8_unicode_ci DEFAULT NULL,
  `MotherFirstName` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  `MotherLastName` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  `FatherMobilePhone` bigint(8) DEFAULT NULL,
  `MotherMobilePhone` bigint(8) DEFAULT NULL,
  `FatherProfession` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
  `MotherProfession` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
  `Observations` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `Student`
--

INSERT INTO `Student` (`ID`, `GradeId`, `StudentID`, `OrganizationId`, `FirstName`, `LastName`, `FatherFirstName`, `FatherLastName`, `DateOfBirth`, `PlaceOfBirth`, `Sex`, `Carnet`, `MobilePhone`, `Address`, `MotherFirstName`, `MotherLastName`, `FatherMobilePhone`, `MotherMobilePhone`, `FatherProfession`, `MotherProfession`, `Observations`) VALUES
(21, 'G_016', 'S_100000001', 116, 'Student', 'One', '', '', '', '', 'male', NULL, 8178109047, '', '', '', 0, 0, NULL, NULL, NULL),
(22, 'G_016', 'S_100000002', 116, 'Student', 'two', '', '', '', '', 'female', NULL, 0, '', '', '', 0, 0, NULL, NULL, NULL),
(23, 'G_002', 'S_100000003', 116, 'Student3', 'three', NULL, NULL, NULL, NULL, 'male', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);

-- --------------------------------------------------------

--
-- Table structure for table `Subject`
--

CREATE TABLE `Subject` (
  `ID` int(6) NOT NULL,
  `SubjectId` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `Name` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
  `Abbreviation` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
  `GradeId` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
  `OrganizationId` int(6) NOT NULL,
  `StaffId` varchar(30) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `Subject`
--

INSERT INTO `Subject` (`ID`, `SubjectId`, `Name`, `Abbreviation`, `GradeId`, `OrganizationId`, `StaffId`) VALUES
(12, 'SUB_0001', 'English 1A', 'Eng_1A', 'G_016', 116, 'E_100000030'),
(13, 'SUB_0002', 'English 1B', 'Eng_1B', 'G_002', 116, '0'),
(14, 'SUB_0003', 'Science 1A', 'Sci_1A', 'G_016', 116, 'E_100000030');


  • 2
    Consider handling issues of data display in application code – Strawberry May 20 '20 at 19:14
  • What @Strawberry said. Doing pivots in SQL, especially in MySQL, is a notorious pain in the xxx neck. – O. Jones May 20 '20 at 19:16
  • After examaning your data, your query is correct. Subjet2 has no Weightage bigger than 0, what result do oyu expect – nbk May 20 '20 at 19:36
  • @nbk After increasing the weightage the value doesn't changes and I am sure how to do the calculation to get the cumulative marks on basis of weightage. – TRIKONINFOSYSTEMS May 20 '20 at 19:59
  • your weightage selects the exams that are selected, in the example 3 5 and 6 and for them you get the marks. for every student. I don't understand what you want as result, so please show us that – nbk May 20 '20 at 20:04
  • @nbk I updated the fiddle https://dbfiddle.uk/?rdbms=mysql_5.6&fiddle=a68a6145e61c5dd1a90d6d3431db0b66 with updated weightage, as you can see 'SUB_0001' has 4 exams, 3 of them are having Weightage. 'S_100000001' appeared 3 Exams with subjectid 'SUB_0001', out of which 2 Exams with ID 'EX_0002' and 'EX_0005' are having Weightage 10 and 80 so the Marks should be (10% of 77) + (80% of 80) = 71.7 But it is showing 80 only – TRIKONINFOSYSTEMS May 20 '20 at 22:06

1 Answers1

1

Ok with your last comment, your query would look like this

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
               CONCAT('SUM(IF(sj.SubjectId = ''', SubjectId,''', ROUND((pa.`Marks` * p.`Weightage` / 100),1),0)) AS ',SubjectId)
              ) INTO @sql
FROM Subject;

SET @sql = CONCAT('SELECT s.ID, s.StudentID, s.FirstName, s.LastName, ', @sql, ' 
                  FROM Student s
                  JOIN Marks AS pa 
                  ON pa.StudentID = s.StudentID AND pa.OrganizationId = s.OrganizationId 
                  JOIN Exams p
                  ON p.ExamId = pa.ExamId AND p.OrganizationId = pa.OrganizationId
                  JOIN Subject sj
                  ON p.SubjectId = sj.SubjectId AND pa.OrganizationId = sj.OrganizationId
                  WHERE p.Weightage > 0
                 GROUP BY s.ID
                 ORDER BY s.ID');
#SELECT @sql;
PREPARE stmt FROM @sql;
EXECUTE stmt;


ID | StudentID   | FirstName | LastName | SUB_0001 | SUB_0002 | SUB_0003
-: | :---------- | :-------- | :------- | -------: | -------: | -------:
21 | S_100000001 | Student   | One      |     71.7 |      0.0 |      2.5
22 | S_100000002 | Student   | two      |     72.0 |      0.0 |      2.1
23 | S_100000003 | Student3  | three    |      0.0 |      1.2 |      1.5

db<>fiddle here

Community
  • 1
  • 1
nbk
  • 45,398
  • 8
  • 30
  • 47