0

Here is an example of the current rows as is:

id    sdate    subject    exam1    assgn1    overallmark    result    credits
____________________________________________________________________________
 1    082013   Math      40.0      0.00       33             F         7.50
 2    082013   Math      0.0       25.00      33             F         7.50
 3    082013   Science   80.0      0.00       76             P         7.50

And this is what i want it to be:

id    sdate    subject    exam1    assgn1    overallmark    result    credits
____________________________________________________________________________
 1    082013   Math      40.0      25.00       33             F         7.50

Essentials i want to wrap multiple results of the same subject into one row, what would be the best way to go about this? I've attempted using GROUP_CONCAT but my mysql knowledge isn't exactly up to scratch.

I've been using a simple MySQL query to get this results displayed in my first code block:

SELECT * FROM exam_results WHERE sdate = :sdate AND studno = :studno

Edit*

Thanks to user @echo_Me this solution works:

select id ,sdate,subject,sum(exam1)exam1,sum(assgn1)assgn1,overallmark,result,
credits from exam_results group by studno,subject

However, could the same syntax be used in postgres? How would i change it accordingly to fit in with a postgres installation?

2
  • do you really think that inserting 2 rows for same student to store exam1 and assgn1 marks is neccessary, if not then insert only one row for each student, and your problem is solved Commented Jun 23, 2014 at 9:32
  • Unfortunately I have no control over the database schema or how results are inserted.
    – Zy0n
    Commented Jun 23, 2014 at 9:40

2 Answers 2

2

try that:

  select id ,sdate,subject,sum(exam1)exam1,sum(assgn1)assgn1,overallmark,result,
  credits
  from exam_results
  group by studno,subject
4
  • That looks to have done it, I'll test on my live environment and see how it does. Thanks!
    – Zy0n
    Commented Jun 23, 2014 at 9:25
  • Out of interest, could this same syntax be used for a postgres installation?
    – Zy0n
    Commented Jun 23, 2014 at 10:11
  • @CianGallagher: no, Postgres would not allow an invalid group by usage. And the values returned in MySQL are "random" (MySQL's calls this "indeterminate": mysqlperformanceblog.com/2006/09/06/…
    – user330315
    Commented Jun 23, 2014 at 11:07
  • @a_horse_with_no_name thanks for the reply. How would i go about rewriting the above query to work with psql?
    – Zy0n
    Commented Jun 23, 2014 at 11:08
0

I'm not sure what you're trying achieve but you can take a look at this post about group concat: CONCAT with GROUP_CONCAT in mysql

From your expected result I don't really see a group_concat need. If you want to get the min value of exam1 just Group By credits or sDate and select MIN(exam1). The group concatenation is used when you want to have values from different rows returned in one. E.g: have overall mark = 33,33,76

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.