SQL group by和sum在不同的表/视图上

时间:2014-11-19 10:44:57

标签: sql join views relational-database coalesce

我有2个观点,A和B:

+--------------+------------------+
| Field        | Type             |
+--------------+------------------+
| id           | int(11) unsigned |
| vat_perc     | numeric          |
| vat_amount   | numeric          |
| project_id   | numeric          |
| ...                             |
+--------------+------------------+

我想要第三个视图C,它应该基本上具有相似的结构,但是在vat_amount字段中应该包含由前两个视图中的vat_perc和project_id分组的所有vat_amounts的总和。

C:

+--------------+------------------+
| Field        | Type             |
+--------------+------------------+
| id           | int(11) unsigned |
| vat_perc     | numeric          |
| vat_amount   | numeric          |
| project_id   | numeric          |
| ...                             |
+--------------+------------------+

例如,让我们说A包含

+--------------+------------------+------------------+
| project_id   | vat_perc         | vat_amount       |
+--------------+------------------+------------------+
|  1           |  4%              | 10               |                  
|  1           |  5%              | 15               |                  
|  2           |  5%              | 15               |                  
|  3           |  4%              | 10               |                  
|              |                  |                  |                  
+--------------+------------------+------------------+

和B包含

+--------------+------------------+------------------+
| project_id   | vat_perc         | vat_amount       |
+--------------+------------------+------------------+
|  3           |  5%              | 10               |                  
|  2           |  4%              | 15               |                  
|  2           |  5%              | 15               |                  
|  1           |  4%              | 15               |                  
|              |                  |                  |                  
+--------------+------------------+------------------+

然后表C应该包含

+--------------+------------------+------------------+
| project_id   | vat_perc         | vat_amount       |
+--------------+------------------+------------------+      
|  1           |  4%              | 25               |     
|  1           |  5%              | 15               |                  
|  2           |  4%              | 15               |     
|  2           |  5%              | 30               |                 
|  3           |  4%              | 10               |       
|  3           |  5%              | 10               |            
|              |                  |                  |                  
+--------------+------------------+------------------+

(我希望我说得够清楚,如果你想要一个扩展的例子,我当然可以让它更大')

由于

3 个答案:

答案 0 :(得分:1)

使用视图:

CREATE VIEW C(project_id,vat_perc,vat_amount)
AS
WITH CTE as (
SELECT project_id,vat_perc,vat_amount FROM A
  UNION ALL
SELECT project_id,vat_perc,vat_amount FROM B)
SELECT project_id,vat_perc,SUM(vat_amount) AS vat_amount FROM CTE
GROUP BY project_id,vat_perc

然后只选择值 SELECT * FROM C ORDER BY project_id

答案 1 :(得分:0)

使用

  

分组由a.project_id,a.vat_perc,b.project_id,b.vat_perc

组成

答案 2 :(得分:0)

您可以使用union allgroup by来实现这一目标:

SELECT project_id,
       vat_perc,
       Sum(vat_amount) vat_amount
FROM   (SELECT project_id,
               vat_perc,
               vat_amount
        FROM   tableA
        UNION ALL
        SELECT project_id,
               vat_perc,
               vat_amount
        FROM   tableB) t
GROUP  BY project_id,vat_perc
ORDER  BY project_id 
如果不需要,可以删除

order by子句。