I have this table
[1|Sub1|Mark1|Sub2|Mark2|Subj3|Mark3|...]
[2|JSP |85 |ASP |70 |PHP |95 |...]
I need the average Marks of the two highest subjects?
For example
I have this table
[1|Sub1|Mark1|Sub2|Mark2|Subj3|Mark3|...]
[2|JSP |85 |ASP |70 |PHP |95 |...]
I need the average Marks of the two highest subjects?
For example
May be are you asking this?
SELECT ID,
(SELECT AVG(v)
FROM (VALUES (Mark1), (Mark2), (Mark3), (Mark4), (Mark5)) AS value(v)) as [AverageMarks]
FROM Table1
If you are looking for only Average of 2 highest subjects among 3 subjects then try this.
SELECT ID,
(SELECT (SUM(v)-MIN(V))/2
FROM (VALUES (Mark1), (Mark2), (Mark3)) AS value(v)) as [AverageMarks]
FROM Table1
Consider the following:
DROP TABLE IF EXIST my_table;
CREATE TABLE my_table
(subject CHAR(3) PRIMARY KEY
,mark INT NOT NULL
);
INSERT INTO my_table VALUES
('JSP',85),
('ASP',70),
('PHP',95);
SELECT AVG(a.mark)
FROM
( SELECT mark FROM my_table ORDER BY mark DESC LIMIT 2 ) a;
+-------------+
| AVG(a.mark) |
+-------------+
| 90.0000 |
+-------------+