1

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:

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);

Using sum with DISTINCT returns some accurate values but only if all input values are unique. That's not a solution I can use:

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);

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.

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.

For example like there: Can SQL calculate aggregate functions across multiple tables?

Hope someone can help me to build a working query for my task.

[EDIT] simple example

siedlungsareale

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

pv_st_potenziale_gis

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);

Expected correct result:

bedarf   | mflaeche
---------|----------
      80 |      140

There I would get the sum of all values for column 'bedarf' from 'siedlungsareale' and all for 'mflaeche' from 'pv_st_potenziale_gis'

But the real calculated values of column 'bedarf' using this query are much higher caused of the CROSS JOIN condition.

And the other query:

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);

returns:

bedarf   | mflaeche
---------|-----------
     80  |      60

Accurate value for 'bedarf' caused the values are unique. But for mflaeche where some values occurre several times the result is wrong.

4

0 回答 0