sql - Filtering table using sum value from another table -


working postgresql, trying filter data table using data one. example:

table 1:

  id|app|area_app    1| |   4.7    2| |   4.7    3| |   4.7 

table 2:

  id|spart|area_spart    1| 1a  |   1.2    1| 1b  |   1.8    2| 2a  |   2.1    2| 2b  |   2.3    3| 3a  |   0.6 

i filter second table selecting rows (with same id of first table) sum of area_spart equal area_app of first table; in example resulting table should be:

 id|spart|area_spart   1| 1b  |   1.8   2| 2b  |   2.3   3| 3a  |   0.6 

total area_spart = area_app = 4.7

thanks all!

use inner join if getting duplicate record table use query

select table1.id,table2.spart,sum(table2.area_spart) area_spart table1 inner join table2 on (table1.id = table2.id) group table1.id 

if getting id based record use one

select table1.id,table2.spart,sum(table2.area_spart) area_spart table1 inner join table2 on (table1.id = table2.id) table1.id = 'your_id' 

if record not based on id , want 1 record try 1 limit

select table1.id,table2.spart,sum(table2.area_spart) area_spart table1 inner join table2 on (table1.id = table2.id) limit 1 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -