Subject Problem with UNION
Author sifi24viper
Hello Folks

Follow i have a UNION-Query that doesn't work!
Im useing Firebird 1.5.2.4731

SELECT
MASCHINEN_HEAD.NAME,
0 AS "Amin_Total",
SUM(VERBRAUCH_KSA.MENGE) AS "Sand_Total"
FROM
MASCHINEN_HEAD
INNER JOIN MASCHINEN_ITEM ON (MASCHINEN_HEAD.NR=MASCHINEN_ITEM.NR)
INNER JOIN SPS_NODE ON (MASCHINEN_ITEM.SPS_NODE=SPS_NODE.SPS_NODE)
INNER JOIN GET_SPS_NODE(:BENUTZER) ON
(MASCHINEN_ITEM.SPS_NODE=GET_SPS_NODE.SPS)
AND (MASCHINEN_ITEM.KSA_MASCHINEN_NR=GET_SPS_NODE.KSA)
INNER JOIN VERBRAUCH_KSA ON
(MASCHINEN_ITEM.SPS_NODE=VERBRAUCH_KSA.SPS_NODE)
AND (MASCHINEN_ITEM.KSA_MASCHINEN_NR=VERBRAUCH_KSA.KSA_MASCHINEN_NR)
INNER JOIN KOMPONENTEN ON (VERBRAUCH_KSA.KOMPONENTEN_NR=KOMPONENTEN.NR)
WHERE
(SPS_NODE.MASCHINEN_TYP = 2) AND
(KOMPONENTEN.TYP = 0) AND
(VERBRAUCH_KSA.DATUM_ZEIT >= :DateFrom) AND
(VERBRAUCH_KSA.DATUM_ZEIT <= :DateTo)
GROUP BY
MASCHINEN_HEAD.NAME

UNION

SELECT
MASCHINEN_HEAD.NAME,
SUM(VERBRAUCH_BGG.MENGE) AS "Amin_Total",
0 AS "Sand_Total"
FROM
MASCHINEN_HEAD
INNER JOIN MASCHINEN_ITEM ON (MASCHINEN_HEAD.NR=MASCHINEN_ITEM.NR)
INNER JOIN SPS_NODE ON (MASCHINEN_ITEM.SPS_NODE=SPS_NODE.SPS_NODE)
INNER JOIN GET_SPS_NODE(:BENUTZER) ON
(MASCHINEN_ITEM.SPS_NODE=GET_SPS_NODE.SPS)
AND (MASCHINEN_ITEM.KSA_MASCHINEN_NR=GET_SPS_NODE.KSA)
INNER JOIN VERBRAUCH_BGG ON
(MASCHINEN_ITEM.SPS_NODE=VERBRAUCH_BGG.SPS_NODE)
WHERE
(SPS_NODE.MASCHINEN_TYP = 3) AND
(VERBRAUCH_BGG.DATUM_ZEIT >= :DateFrom) AND
(VERBRAUCH_BGG.DATUM_ZEIT <= :DateTo)
GROUP BY
MASCHINEN_HEAD.NAME

The error message are:

fmSQLEditor.Query:
Invalid token.Dynamic SQL Error.
SQL error code = -104.
Invalid command.
Data type unknown.


But when i try the two SELECT-Statment separatly they work fine!

May somebody have an idea!

Gregor