Question : Query problem / Joining and max function

Hey guys! I have a PL/SQL statement that is set up dynamically. Below I have the core of this query in simple SQL. My problem lies within the max function. This is due to the fact that many records do not exist in TBL_ARBEIT_ZU_GEBIET (B). Thus max(LNG_ARBEITSSCHRITT) is not available. The query works but outputs 145 for all records. My table TBL_GEBIET contains all records, which I need to output. Hence I tried to do an outer right join. However, the result set contains only 96 rows i.e. the ones which exist in TBL_ARBEIT_ZU_GEBIET. I have 875 overall though and need to select the max(LNG_ARBEITSSCHRITT) distinctively even if the record does not exist in table B. Do you have a brilliant suggestion for me? Have worked on it for hours without finding any solution.

BR,

Seb
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
SELECT
   CNT_GEBIET,
   CNT_GEBIET DISPLAY,
   CNT_GEBIET BUTTON,
   STR_NAME,
   STR_ART_GEBIETE, 
   STR_REG_KART_TEAMS,
   STR_BESCHREIBUNG, 
   BOOL_ANZEIGEN,
   STR_FEDERFUEHRENDE,
   VT_TBL_GEBIET.INT_VERFAHREN,
   STR_VERFAHREN,
   VT_TBL_GEBIET_ZU_BIOGEO.ST_BIOGEO,
   VT_TBL_GEBIET_ZU_BIOGEO.STR_GEBIET,
   VT_TBL_BIOGEO.STR_BIOGEO,
   VT_TBL_BIOGEO.INT_BIOGEO,
CASE WHEN TO_CHAR(MIN(VT_TBL_ARBEIT_CRIT_DATE.DATE_END), 'YYYY') <= TO_CHAR(SYSDATE, 'YYYY')  THEN 'red'
ELSE 'green' END AS MXCOLOUR,
   TO_CHAR(MIN(VT_TBL_ARBEIT_CRIT_DATE.DATE_START), 'YYYY')  DATE_START,
   TO_CHAR(MIN(VT_TBL_ARBEIT_CRIT_DATE.DATE_END), 'YYYY')  DATE_END,
   TBL_REGBEZE.GEB,
   TBL_REGBEZE.STR_REGBEZE,
   VT_TBL_GEBIET_ZU_ALF.ALF,
   TBL_REGBEZE.LNG_REG_KART_TEAMS,
   MAX(NVL(LNG_ARBEITSSCHRITT,0))
FROM 
   VT_TBL_ARBEITSSCHRITTE,
   VT_TBL_ART_GEBIETE,
   VT_TBL_FEDERFUEHRENDE,
   VT_TBL_GEBIET_ZU_BIOGEO,
   VT_TBL_BIOGEO,
   VT_TBL_VERFAHREN,
   VT_TBL_REG_KART_TEAMS,
   VT_TBL_GEBIET_ZU_ALF,
   TBL_REGBEZE,
   VT_TBL_GEBIET LEFT OUTER JOIN
   VT_TBL_ARBEIT_CRIT_DATE ON VT_TBL_GEBIET.CNT_GEBIET = VT_TBL_ARBEIT_CRIT_DATE.LNG_GEBIET
RIGHT OUTER JOIN
   VT_TBL_ARBEIT_ZU_GEBIET ON VT_TBL_GEBIET.CNT_GEBIET =  VT_TBL_ARBEIT_ZU_GEBIET.LNG_GEBIET
WHERE 
   VT_TBL_ARBEIT_ZU_GEBIET.LNG_ARBEITSSCHRITT = VT_TBL_ARBEITSSCHRITTE.CNT_ARBEITSSCHRITT
   AND
   VT_TBL_ART_GEBIETE.CNT_ART_GEBIETE = VT_TBL_GEBIET.INT_ART_GEBIET
   AND
   VT_TBL_FEDERFUEHRENDE.CNT_FEDERFUEHRENDE = VT_TBL_GEBIET.LNG_FEDER
   AND
   VT_TBL_GEBIET_ZU_BIOGEO.STR_GEBIET = VT_TBL_GEBIET.CNT_GEBIET
   AND
   VT_TBL_GEBIET_ZU_BIOGEO.ST_BIOGEO = VT_TBL_BIOGEO.INT_BIOGEO
   AND
   VT_TBL_GEBIET.INT_VERFAHREN = VT_TBL_VERFAHREN.CNT_VERFAHREN
   AND
   VT_TBL_REG_KART_TEAMS.CNT_REG_KART_TEAMS = VT_TBL_GEBIET.INT_REG_KART_TEAMS  
   AND
   TBL_REGBEZE.GEB = VT_TBL_GEBIET.CNT_GEBIET
AND  
   TBL_REGBEZE.LNG_REG_KART_TEAMS = VT_TBL_REG_KART_TEAMS.CNT_REG_KART_TEAMS
AND 
   VT_TBL_GEBIET.CNT_GEBIET = VT_TBL_GEBIET_ZU_ALF.GEBIET
GROUP BY 
     CNT_GEBIET,
     STR_NAME,
     STR_ART_GEBIETE,
     STR_REG_KART_TEAMS, 
     LNG_REG_KART_TEAMS,
     CNT_REG_KART_TEAMS,
     VT_TBL_GEBIET_ZU_ALF.ALF,
     STR_BESCHREIBUNG,
     BOOL_ANZEIGEN,
     STR_FEDERFUEHRENDE,
     VT_TBL_GEBIET_ZU_BIOGEO.ST_BIOGEO,
     VT_TBL_GEBIET_ZU_BIOGEO.STR_GEBIET,
     VT_TBL_BIOGEO.STR_BIOGEO,
     VT_TBL_BIOGEO.INT_BIOGEO,
     VT_TBL_VERFAHREN.STR_VERFAHREN,
     VT_TBL_GEBIET.INT_VERFAHREN,
     TBL_REGBEZE.GEB,
     TBL_REGBEZE.STR_REGBEZE

Answer : Query problem / Joining and max function

I labelled your tables in the order they appeared above and then rearranged them according to what I could gather as the connection.

SELECT
  CNT_GEBIET,
  CNT_GEBIET DISPLAY,
  CNT_GEBIET BUTTON,
  STR_NAME,
  STR_ART_GEBIETE,
   STR_REG_KART_TEAMS,
  STR_BESCHREIBUNG,
   BOOL_ANZEIGEN,
  STR_FEDERFUEHRENDE,
  K.INT_VERFAHREN,
  STR_VERFAHREN,
  D.ST_BIOGEO,
  D.STR_GEBIET,
  E.STR_BIOGEO,
  E.INT_BIOGEO,
CASE WHEN TO_CHAR(MIN(L.DATE_END), 'YYYY') <= TO_CHAR(SYSDATE, 'YYYY')  THEN 'red'
ELSE 'green' END AS MXCOLOUR,
  TO_CHAR(MIN(L.DATE_START), 'YYYY')  DATE_START,
  TO_CHAR(MIN(L.DATE_END), 'YYYY')  DATE_END,
  I.GEB,
  I.STR_REGBEZE,
  H.ALF,
  I.LNG_REG_KART_TEAMS,
  MAX(NVL(LNG_ARBEITSSCHRITT,0))
FROM VT_TBL_ARBEIT_ZU_GEBIET AS M
LEFT OUTER JOIN (
  VT_TBL_GEBIET AS K
   INNER JOIN VT_TBL_ART_GEBIETE AS B ON B.CNT_ART_GEBIETE = K.INT_ART_GEBIET
  INNER JOIN VT_TBL_FEDERFUEHRENDE AS C ON C.CNT_FEDERFUEHRENDE = K.LNG_FEDER
  INNER JOIN VT_TBL_GEBIET_ZU_BIOGEO AS D ON D.STR_GEBIET = K.CNT_GEBIET
  INNER JOIN VT_TBL_BIOGEO AS E ON D.ST_BIOGEO = E.INT_BIOGEO
  INNER JOIN VT_TBL_VERFAHREN AS F ON F.CNT_VERFAHREN = K.INT_VERFAHREN
  INNER JOIN VT_TBL_REG_KART_TEAMS AS G ON G.CNT_REG_KART_TEAMS = K.INT_REG_KART_TEAMS
  INNER JOIN VT_TBL_GEBIET_ZU_ALF AS H ON H.GEBIET = K.CNT_GEBIET
  INNER JOIN TBL_REGBEZE AS I ON I.GEB = K.CNT_GEBIET
      AND I.LNG_REG_KART_TEAMS = G.CNT_REG_KART_TEAMS
  INNER JOIN TBL_ARBEITSSCHRITTE AS J ON ??
  INNER JOIN VT_TBL_ARBEIT_CRIT_DATE AS L ON L.LNG_GEBIET = K.CNT_GEBIET
) ON M.LNG_GEBIET = K.CNT_GEBIET
LEFT OUTER JOIN VT_TBL_ARBEITSSCHRITTE AS A
  ON M.LNG_ARBEITSSCHRITT = A.CNT_ARBEITSSCHRITT
GROUP BY
     CNT_GEBIET,
    STR_NAME,
    STR_ART_GEBIETE,
    STR_REG_KART_TEAMS,
     LNG_REG_KART_TEAMS,
    CNT_REG_KART_TEAMS,
    H.ALF,
    STR_BESCHREIBUNG,
    BOOL_ANZEIGEN,
    STR_FEDERFUEHRENDE,
    D.ST_BIOGEO,
    D.STR_GEBIET,
    E.STR_BIOGEO,
    E.INT_BIOGEO,
    F.STR_VERFAHREN,
    K.INT_VERFAHREN,
    I.GEB,
    I.STR_REGBEZE


Note that table J didn't have any JOIN defined.  Is a CROSS JOIN meant here.  If so, which tables should it be CROSS JOIN'd?

You have some columns not specified as to which table they come from, but I have tried to organize what I knew so see if this works better now.  You can remove table J if it is not being used anywhere.
Random Solutions  
 
programming4us programming4us