Zvoni
Erfahrenes Mitglied
OK, steh gerade auf dem Schlauch weil ich die Ergebnisse der SQL's nicht verstehe
DBMS = DB2
Ergibt das (Korrekt!) --> Hinweis: FETCH FIRST 1 ROWS ONLY wäre in anderen DBMS ein LIMIT 1 weil ich nur die Kundennummer mit dem niedrigsten VK-Preis haben will
Dann habe ich dieses SQL
Ergibt das (Korrekt!)
Mache ich aber das hier:
bekomme ich das:
HÄ?
Ist doch ein LEFT JOIN
DBMS = DB2
SQL:
SELECT LOFIRM, LOWKNR, LOTENR, LOKDNR || ' - ' || KDNAME AS MINVKD, MIN(LOPR01) AS LOPR01
FROM X500PRDSD.LPKO
INNER JOIN X500PRDSD.KDST
ON KDFIRM=LOFIRM AND KDWKNR=LOWKNR AND KDKDNR=LOKDNR AND KDVENR=0
WHERE LOFIRM='2' AND LOWKNR='050' AND LOTENR='100273' AND
INT(VARCHAR_FORMAT(CURRENT_DATE, 'YYYYMMDD')) BETWEEN LOGUVO AND LOGUBI
GROUP BY LOFIRM, LOWKNR, LOTENR, LOKDNR || ' - ' || KDNAME
ORDER BY LOPR01 FETCH FIRST 1 ROWS ONLY
LOFIRM | LOWKNR | LOTENR | MINVKD | LOPR01 |
2 | 050 | 100273 | 2001082 - Anonymer Kunde | 0,25 |
Dann habe ich dieses SQL
SQL:
SELECT K1.LOFIRM, K1.LOWKNR, K1.LOTENR, K1.KDNRMIN || ' - ' || K2.KDNAME AS KUNDEMIN, K1.KDNRMAX || ' - ' || K3.KDNAME AS KUNDEMAX
FROM (SELECT LOFIRM, LOWKNR, LOTENR, MIN(LOKDNR) AS KDNRMIN, MAX(LOKDNR) AS KDNRMAX
FROM X500PRDSD.LPKO WHERE INT(VARCHAR_FORMAT(CURRENT_DATE, 'YYYYMMDD'))
BETWEEN LOGUVO AND LOGUBI
GROUP BY LOFIRM, LOWKNR, LOTENR) AS K1
INNER JOIN (SELECT DISTINCT KDFIRM, KDWKNR, KDKDNR, KDNAME FROM X500PRDSD.KDST WHERE KDVENR=0) AS K2
ON K2.KDFIRM=K1.LOFIRM AND K2.KDWKNR=K1.LOWKNR AND K2.KDKDNR=K1.KDNRMIN
INNER JOIN (SELECT DISTINCT KDFIRM, KDWKNR, KDKDNR, KDNAME FROM X500PRDSD.KDST WHERE KDVENR=0) AS K3
ON K3.KDFIRM=K1.LOFIRM AND K3.KDWKNR=K1.LOWKNR AND K3.KDKDNR=K1.KDNRMAX
WHERE K1.LOFIRM='2' AND K1.LOWKNR='050' AND K1.LOTENR='100273'
LOFIRM | LOWKNR | LOTENR | KUNDEMIN | KUNDEMAX |
2 | 050 | 100273 | 2000848 - Geht euch nix an | 2001188 - Geht euch auch nix an |
Mache ich aber das hier:
SQL:
SELECT K1.LOFIRM, K1.LOWKNR, K1.LOTENR, K1.KDNRMIN || ' - ' || K2.KDNAME AS KUNDEMIN, K1.KDNRMAX || ' - ' || K3.KDNAME AS KUNDEMAX, K5.MINVKD, K5.LOPR01
FROM (SELECT LOFIRM, LOWKNR, LOTENR, MIN(LOKDNR) AS KDNRMIN, MAX(LOKDNR) AS KDNRMAX
FROM X500PRDSD.LPKO WHERE INT(VARCHAR_FORMAT(CURRENT_DATE, 'YYYYMMDD')) BETWEEN LOGUVO AND LOGUBI
GROUP BY LOFIRM, LOWKNR, LOTENR) AS K1
INNER JOIN (SELECT DISTINCT KDFIRM, KDWKNR, KDKDNR, KDNAME FROM X500PRDSD.KDST WHERE KDVENR=0) AS K2
ON K2.KDFIRM=K1.LOFIRM AND K2.KDWKNR=K1.LOWKNR AND K2.KDKDNR=K1.KDNRMIN
INNER JOIN (SELECT DISTINCT KDFIRM, KDWKNR, KDKDNR, KDNAME FROM X500PRDSD.KDST WHERE KDVENR=0) AS K3
ON K3.KDFIRM=K1.LOFIRM AND K3.KDWKNR=K1.LOWKNR AND K3.KDKDNR=K1.KDNRMAX
LEFT JOIN (SELECT LOFIRM, LOWKNR, LOTENR, LOKDNR || ' - ' || KDNAME AS MINVKD, MIN(LOPR01) AS LOPR01 FROM X500PRDSD.LPKO
INNER JOIN X500PRDSD.KDST ON KDFIRM=LOFIRM AND KDWKNR=LOWKNR AND KDKDNR=LOKDNR AND KDVENR=0
WHERE INT(VARCHAR_FORMAT(CURRENT_DATE, 'YYYYMMDD')) BETWEEN LOGUVO AND LOGUBI
GROUP BY LOFIRM, LOWKNR, LOTENR, LOKDNR || ' - ' || KDNAME ORDER BY LOPR01 FETCH FIRST 1 ROWS ONLY) AS K5
ON K5.LOFIRM=K1.LOFIRM AND K5.LOWKNR=K1.LOWKNR AND K5.LOTENR=K1.LOTENR
WHERE K1.LOFIRM='2' AND K1.LOWKNR='050' AND K1.LOTENR='100273'
bekomme ich das:
LOFIRM | LOWKNR | LOTENR | KUNDEMIN | KUNDEMAX | MINVKD | LOPR01 |
2 | 050 | 100273 | 2000848 - Geht euch nix an | 2001188 - Geht euch auch nix an | - | - |
HÄ?
Ist doch ein LEFT JOIN