How to get the sum of field in a column?

风流意气都作罢 提交于 2019-12-11 10:36:32

问题


I have this table student_resources and here are data that it hold:

p_id   p_name     res_code         tax       base_amt    date
2300  |A student |WC158 - EWT 1%  |133.93   |13392.86   |2015-07-01
2300  |A student |WC158 - EWT 1%  |62.50    |6250.00    |2015-07-01
901   |B student |WC158 - EWT 1%  |8.31     |830.58     |2015-06-09
2831  |C student |WC160 - EWT 2%  |2736.84  |136842.11  |2015-06-04
905   |D student |WC158 - EWT 1%  |31.25    |3125.00    |2015-06-16
905   |D student |WC158 - EWT 1%  |31.25    |3125.00    |2015-06-29
905   |D student |WC158 - EWT 1%  |31.25    |3125.00    |2015-06-29
905   |D student |WC158 - EWT 1%  |26.79    |2678.57    |2015-06-16
959   |G student |WC158 - EWT 1%  |114.29   |11428.57   |2015-01-10
959   |G student |WC158 - EWT 1%  |478.90   |47890.18   |2015-01-20
2424  |L student |WC158 - EWT 1%  |45.54    |4553.58    |2015-03-03

and in my class student_resources_report i have this definition:

...
    cr.execute("select es.id from student_resource es where date >= '"+str(ewt.date_from)+"' and date <= '"+str(ewt.date_to)+"'")
    source_id = cr.fetchall()
    _logger.info("\n\t\t\tSource ... %s"%(str(source_id)))
    s_id = self.pool.get('student.resource').search(cr,uid,[('id','in',source_id)])
    #_logger.info("\n\t\t\tSource in a list ... %s"%(str(s_id)))
   for resource in self.pool.get('student.resource').browse(cr,uid,s_id,context=context):
        if 'WC158' in resource.res_code:
                atc_code = 'WC158'
                nature = 'NOTEBOOK'
                seq = 1
                base += resource.base_amt
                amount += resource.tax
        elif 'WC160' in resource.res_code:
                atc_code = 'WC160'
                nature = 'BACKPACK'
                base += resource.base_amt
                amount += resource.tax
                seq = 2
        elif 'WC010' in resource.res_code:
                atc_code = 'WC010'
                nature = 'COLOR'
                base = resource.base_amt
                amount = resource.tax
                seq = 3
        elif 'WC140' in ait.res_code:
                atc_code = 'WC140'
                nature = 'BOOKS'
                base = resource.base_amt
                amount = resource.tax
                seq = 4            
        percent = self._get_ewt_percentage(cr, uid, ids, resource.res_code, context=context)
        partner_id = resource.partner_id.id

        dictionary = {
                  'name' : ewt.id,
                  'parent_id' : ewt.id,
                  'partner_id' : partner_id,
                  'atc_code' : atc_code,
                  'nature_of_resource' : nature,
                  'base_amount' : base,
                  'percentage' : percent,
                  'tax_amount' : amount,
                  'seq' : seq,
                 }
        self.pool.get('student.resource.line').create(cr, uid, dictionary, context=context)
    list = self.pool.get('student.resource.line').search(cr,uid,[('parent_id','=',ewt.id)])
    lines = [line.id if line.id else False for line in self.pool.get('student.resource.line').browse(cr,uid,list,context=context)]
value = {
             "value" : {
                        'name' : 'Student Resources Report',
                        "ewt_line" : lines
                        }
             }
return value

What i can't understand is that when my view is being rendered it output only looks like this:

Seq Stud        Code    Nature      Base Amt   Percent  Tax
1   B student   WC158   NOTEBOOK    830.58     1.0     8.31
1   D student   WC158   NOTEBOOK    2678.57    1.0     26.79
2   C student   WC160   BACKPACK    136842.11  2.0     2736.84

What i expected was the figure below and this is my desired output, that is to get the sum of all base_amount and tax_amount of particular student :

Seq Stud        Code    Nature      Base Amt   Percent  Tax
1   B student   WC158   NOTEBOOK    830.58     1.0     8.31
1   D student   WC158   NOTEBOOK    12053.57   1.0     120.54  ----> since (3125.00+3125.00+3125.00+2678.57) = 12053.57 and (31.25+31.25+31.25+26.79) =  120.54
2   C student   WC160   BACKPACK    136842.11  2.0     2736.84

Any help/suggestions/comments is very much appreciated

来源:https://stackoverflow.com/questions/32065162/how-to-get-the-sum-of-field-in-a-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!