join - SQL group by and sum on different tables/views -


i have 2 views, , b:

+--------------+------------------+ | field        | type             | +--------------+------------------+ | id           | int(11) unsigned | | vat_perc     | numeric          | | vat_amount   | numeric          | | project_id   | numeric          | | ...                             | +--------------+------------------+ 

i want third view c should have similar structure in vat_amount field should contain sum of vat_amounts grouped vat_perc , project_id 2 previous views.

c:

+--------------+------------------+ | field        | type             | +--------------+------------------+ | id           | int(11) unsigned | | vat_perc     | numeric          | | vat_amount   | numeric          | | project_id   | numeric          | | ...                             | +--------------+------------------+ 

for example, let's contains

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

and b contains

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

then table c should contain

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

(i hope made clear enough, if want expanded example of course make 'bigger')

thanks

use view:

create view c(project_id,vat_perc,vat_amount) cte ( select project_id,vat_perc,vat_amount   union select project_id,vat_perc,vat_amount b) select project_id,vat_perc,sum(vat_amount) vat_amount cte group project_id,vat_perc

then select values select * c order project_id


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -