locked
How to sum values in a row RRS feed

  • Question

  • User-1557173817 posted

    Hello, want to sum up two values in a row but was not successful. Here is my code

     var db = Database.Open("StarterSite");
        var  ID = Request.QueryString["ID"];
        var selectQueryString = "SELECT s.Name, s.SID, a.Subject, a.Exam + a.Test AS Total, a.Grade, a.Term, a.Year, a.Remark FROM studentclass s JOIN resulta a ON s.SID=a.SID WHERE a.SID=@0";

    This is what i want to achieve.

    Subject

    Test score

    Examination score

    Total

    Mathematics

    30

    42

    72

    English language

    22

    28

    50

    Science

    30

    29

    59

    Any help...

     
    Tuesday, December 15, 2015 2:07 PM

All replies

  • User-1980594115 posted

    Here is a prototype. Use GROUP BY to form the groupings you desire, such as Subject in your example.

    var selectQueryString = "SELECT a.Subject, SUM(a.Test) AS TestScore, SUM(a.Exam) AS ExaminationScore, SUM(a.Exam + a.Test) AS Total FROM studentclass s JOIN resulta a ON s.SID=a.SID GROUP BY a.Subject WHERE a.SID=@0"; 


    Tuesday, December 15, 2015 4:12 PM
  • User-1557173817 posted

    Hello dblaire, sorry for the late reply, after rendering the above code as below i got the a parsing error.

                                 
    
     @foreach (var row in db.Query(selectQueryString, ID)) {
                                        <tr>
                                           
                                            <td style="background-color: #E8E8E8">@row.Subject</td>
                                            <td>@row.Test</td>
                                            <td>@row.Score</td>
                                            <td style="background-color: #E8E8E8">@row.Grade</td>
                                         
                                            <td>@row.Term</td>
                                            <td style="background-color: #E8E8E8">@row.Year</td>
                                           
                                            
    
                                        </tr>
                                    }
                                   
                                    
    
    Friday, January 15, 2016 9:07 AM
  • User-1980594115 posted

    Can you show the coding of selectQueryString and the parsing error information? Thanks.

    Friday, January 15, 2016 4:06 PM