OhSnap
OhSnap

Reputation: 376

Group By and Join (DB2)

I have a little problem with an SQL-Statement right now. I need to somehow get a group by condition inside of a Join. Well now I do know that this doesn't work, but there most be a way to do this and I hope you guys can give me a clue on how to do this.

Statement:

SELECT 
   btst.Ordnungsbegriff AS BBNR,
   COUNT(vp.KVNR) 
   FROM T_Betriebsstaette btst
      JOIN T_VERSICHERUNGSZEIT vz
         ON vz.BETRIEBSNUMMER = btst.Ordnungsbegriff
         AND vz.VALIDTO = date('2999-12-31')
         AND vz.PERSONENKREIS = 'Beschäftigte'
         AND vz.BEITRAGSGRUPPEKV_IID > 0
         AND vz.PERSONENGRUPPENSCHLUESSEL IN (101, 102, 103, 118, 119)
         JOIN T_VERSICHERTEPERSON vp
            ON vp.OID = vz.VERSPERS_VERSPERSON_ID
            AND ('1' = '0' or '1' = '1' and (vp.VIPKENNZEICHEN is null or  NOT vp.VIPKENNZEICHEN is null and vp.VIPKENNZEICHEN  NOT  in (5, 6)))

The Group By Clause I have to get in there:

GROUP BY vz."VALIDTO", vz."BETRIEBSNUMMER"

I'm not really an expert in SQL. Hope you guys can help me out a little. :)

Upvotes: 0

Views: 2571

Answers (1)

podiluska
podiluska

Reputation: 51494

When using a group by clause, any element in the select clause must either be in the group by clause, or an aggregate function (sum, max, etc)

So

SELECT 
   btst.Ordnungsbegriff AS BBNR,
   COUNT(vp.KVNR) 
   FROM 

   ...
GROUP BY vz."VALIDTO", vz."BETRIEBSNUMMER", btst.Ordnungsbegriff 

Upvotes: 1

Related Questions