user2669261 user2669261 - 6 months ago 24
SQL Question

Mysql: Sum calculated columns

enter image description here

Hey there!
I do have a question regarding SQL. Is there a way to sum the red marked values? So e.g. AAG will not be seperated into 2 columns but aggregated into one. So AAG 2 and AAG 4 become one single row with AAG 6

Help is appreciated :)
Have a nice day

Answer

this would do it:

You need to use SUM()

SELECT t.TEILEID
    , SUM(t.BESTAND - t.RESERVIERT)
FROM   TEILE t
      INNER JOIN AUFTRAGSPOSDS a ON a.TEILEID = t.TEILEID
WHERE  a.AUFTRNR = 15
GROUP BY t.TEILEID;

After reading your comment I think you need this:

SELECT t.TEILEID
    , SUM(t.BESTAND - t.RESERVIERT)
FROM   TEILE t
WHERE  t.TEILEID IN
                (SELECT a.TEILEID
                FROM   AUFTRAGSPOSDS a
                WHERE  a.AUFTRNR = 15)
GROUP BY t.TEILEID;