简体   繁体   English

如何在单个mysql表中添加总计

[英]How to add the sum of totals in a single mysql table

I have the following MySQL table: 我有以下MySQL表:

  • Site_ID Site_ID
  • Serial Number 序列号
  • cost_per_month cost_per_month
  • cost_per_unit_A cost_per_unit_A
  • cost_per_unit_B cost_per_unit_B
  • opening_unit_A opening_unit_A
  • closing_unit_A close_unit_A
  • opening_unit_B opening_unit_B
  • closing_unit_B close_unit_B

    I execute the folowing MySQL query: 我执行以下MySQL查询:

     select SITE_ID as 'Site', Serial Number,cost_per_month cost_per_unit_A, cost_per_unit_B,opening_unit_A, closing_unit_A, opening_unit_B, closing_unit_B format(cost_per_month+(closing_unit_A-opening_unit_A)*cost_per_unit_A+(closing_unit_B-opening_unit_B)*cost_per_unit_A,2) as 'Total Vat Ex', format((cost_per_month+(closing_unit_A-opening_unit_A)*cost_per_unit_A+(closing_unit_B-opening_unit_B)*cost_per_unit_A,*0.14,2)) as 'Vat', format((cost_per_month+(closing_unit_A-opening_unit_A)*cost_per_unit_A+(closing_unit_B-opening_unit_B)*cost_per_unit_A,*1.14,2)) as 'Total Vat inclusive' where SITE_ID ='Site Name' 

    I would like to change this query so that the totals of the sum of the last addition queries are totaled. 我想更改此查询,以使最后一个加法查询的总和为合计。

    Would I use a group by statement? 我会使用group by语句吗?

  • select 
    SITE_ID as 'Site', 
    Serial Number,
    cost_per_month
    cost_per_unit_A, 
    cost_per_unit_B,
    opening_unit_A,
    closing_unit_A, 
    opening_unit_B, 
    closing_unit_B,
    SUM(format(cost_per_month+(closing_unit_A-opening_unit_A)*cost_per_unit_A+(closing_unit_B-opening_unit_B)*cost_per_unit_A,2)) as 'Total Vat Ex',
    SUM(format((cost_per_month+(closing_unit_A-opening_unit_A)*cost_per_unit_A+(closing_unit_B-opening_unit_B)*cost_per_unit_A,*0.14,2))) as 'Vat',
    SUM(format((cost_per_month+(closing_unit_A-opening_unit_A)*cost_per_unit_A+(closing_unit_B-opening_unit_B)*cost_per_unit_A,*1.14,2))) as 'Total Vat inclusive'
    From reading
    where SITE_ID ='Site Name'
    Group By 
    SITE_ID, 
    Serial Number,
    cost_per_month
    cost_per_unit_A, 
    cost_per_unit_B,
    opening_unit_A,
    closing_unit_A, 
    opening_unit_B, 
    closing_unit_B
    

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM