Note that there are some explanatory texts on larger screens.

plurals
  1. POAggregate function across two tables
    primarykey
    data
    text
    <p>I need for further working routine a query which calculates several functions across two (maybe more) tables. But once I import more than one table I got odd results caused by JOIN conditions. First I used that query:</p> <pre><code>SELECT sum(s.bedarf2050_kwh_a) AS bedarf_kWh_a, sum(s.bedarf2050_kwh_a)*0.2 AS netzverlust, sum(s.bedarf2050_kwh_a) + sum(s.bedarf2050_kwh_a)*0.2 AS gesamtbedarf, sum(pv.modulflaeche_qm) AS instbar_modulflaeche_qm FROM siedlungsareale_wbm s, pv_st_potenziale_gis pv WHERE s.vg_solar LIKE '%NWS 2%' AND ST_Covers(s.geom, pv.geom); </code></pre> <p>Using sum with DISTINCT returns some accurate values but only if all input values are unique. That's not a solution I can use:</p> <pre><code>SELECT SUM(DISTINCT s.bedarf2050_kwh_a) AS bedarf_kWh_a, SUM(DISTINCT s.bedarf2050_kwh_a)*0.2 AS netzverlust, SUM(DISTINCT s.bedarf2050_kwh_a) + SUM(DISTINCT s.bedarf2050_kwh_a)*0.2 AS gesamtbedarf, SUM(pv.modulflaeche_qm) AS instbar_modulflaeche_qm, (SUM(DISTINCT s.bedarf2050_kwh_a) + SUM(DISTINCT s.bedarf2050_kwh_a)*0.2)*0.01499 AS startwert_speichergroesse FROM siedlungsareale_wbm s, pv_st_potenziale_gis pv WHERE pv.vg_solar LIKE '%NWS 2%' AND ST_Covers(s.geom, pv.geom); </code></pre> <p>DISTINCT would be a proper solution if the DISTINCT refers to another column, not the column to use in the function. Or some subquery or other JOIN condition. But all I tried run in errors or false result values. </p> <p>I found some solutions using UNION dealing with aggregate function on multiple tables. But as I tried to fit the code on my query I got errors.</p> <p>For example like there: <a href="https://stackoverflow.com/questions/319262/can-sql-calculate-aggregate-functions-across-multiple-tables">Can SQL calculate aggregate functions across multiple tables?</a></p> <p>Hope someone can help me to build a working query for my task.</p> <p>[EDIT] simple example</p> <p>siedlungsareale</p> <pre><code>id | bedarf2050_kWh_a | a | b | c | vg_solar | geom ---|------------------|---|---|---|----------|----- 1 | 20 | | | | NWS 2 | xxxxx 2 | 10 | | | | NWS 2 | xxxxx 3 | 30 | | | | NWS 2 | xxxxx 4 | 5 | | | | NWS 2 | xxxxx 5 | 15 | | | | NWS 2 | xxxxx sum = 80 </code></pre> <p>pv_st_potenziale_gis</p> <pre><code>id | modulflaeche_qm | x | y | z | geom ---|------------------|---|---|---|--------- 1 | 10 | | | | xxxxx 2 | 10 | | | | xxxxx 3 | 20 | | | | xxxxx 4 | 10 | | | | xxxxx 5 | 30 | | | | xxxxx 6 | 30 | | | | xxxxx 7 | 10 | | | | xxxxx 8 | 10 | | | | xxxxx 9 | 10 | | | | xxxxx 10 | 10 | | | | xxxxx sum = 140 SELECT sum(s.bedarfxxxx) AS bedarf, sum(pv.mflaeche) As mflaeche FROM siedlungsareale s, pv_st_potenziale_gis pv WHERE s.vg_solar LIKE '%NWS 2%' AND ST_Covers(s.geom,pv.geom); </code></pre> <p>Expected correct result: </p> <pre><code>bedarf | mflaeche ---------|---------- 80 | 140 </code></pre> <p>There I would get the sum of all values for column 'bedarf' from 'siedlungsareale' and all for 'mflaeche' from 'pv_st_potenziale_gis'</p> <p>But the real calculated values of column 'bedarf' using this query are much higher caused of the CROSS JOIN condition.</p> <p>And the other query:</p> <pre><code>SELECT sum(DISTINCT s.bedarfxxxx) AS bedarf, sum(DISTINCT pv.mflaeche) As mflaeche FROM siedlungsareale s, pv_st_potenziale_gis pv WHERE s.vg_solar LIKE '%NWS 2%' AND ST_Covers(s.geom,pv.geom); </code></pre> <p>returns:</p> <pre><code>bedarf | mflaeche ---------|----------- 80 | 60 </code></pre> <p>Accurate value for 'bedarf' caused the values are unique. But for mflaeche where some values occurre several times the result is wrong. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload