florian86
Goto Top

SQL SUM(Case when)

Hallo Zusammen,

kann man den folgenden Code auch kürzer schreiben?...

      
sum(case when NSDM_V_MSEG.BWART = 101 then NSDM_V_MSEG.MENGE * 1
                when NSDM_V_MSEG.BWART = 102 then NSDM_V_MSEG.MENGE * -1
                when NSDM_V_MSEG.BWART = 262 then NSDM_V_MSEG.MENGE * 1
                when NSDM_V_MSEG.BWART = 261 then NSDM_V_MSEG.MENGE * -1

ich dachte da an eine OR als Verknüpfung leider bekomme ich da andere Ergebnisse...

sum(case when NSDM_V_MSEG.BWART = 101 OR NSDM_V_MSEG.BWART = 262 then NSDM_V_MSEG.MENGE * 1
                   when NSDM_V_MSEG.BWART = 102 OR NSDM_V_MSEG.BWART = 261 then NSDM_V_MSEG.MENGE * -1


Grüße Florian

Content-Key: 648119

Url: https://administrator.de/contentid/648119

Printed on: April 19, 2024 at 07:04 o'clock

Member: LauneBaer
Solution LauneBaer Feb 04, 2021 at 10:33:57 (UTC)
Goto Top
Hallo,

probier das mal mit Klammern:

sum(case when (NSDM_V_MSEG.BWART = 101 OR NSDM_V_MSEG.BWART = 262) then NSDM_V_MSEG.MENGE * 1
                   when (NSDM_V_MSEG.BWART = 102 OR NSDM_V_MSEG.BWART = 261) then NSDM_V_MSEG.MENGE * -1

Grüße
Member: em-pie
Solution em-pie Feb 04, 2021 at 10:37:10 (UTC)
Goto Top
Moin
Zitat von @LauneBaer:
sum(case when (NSDM_V_MSEG.BWART = 101 OR NSDM_V_MSEG.BWART = 262) then NSDM_V_MSEG.MENGE * 1
>                    when (NSDM_V_MSEG.BWART = 102 OR NSDM_V_MSEG.BWART = 261) then NSDM_V_MSEG.MENGE * -1
Geht noch kürzer:
sum(case 
  when NSDM_V_MSEG.BWART in (101, 262) then NSDM_V_MSEG.MENGE * 1
  when NSDM_V_MSEG.BWART in (102, 261) then NSDM_V_MSEG.MENGE * -1
END)

Gruß
em-pie