florian86
Goto Top

SQL unterabfragen als Spalte

Hallo Zusammen,

ich habe folgenden Code...

select Spalte1,
            Spalte2,
           (select Count(*) from Tabelle where MATNR = "Wert Spalte 1"  
from Tabelle

Ich möchte also zusätzlich eine 3. Spalte mit einem Count haben, welche als Filter aber den Wert als Materialnummer
aus Spalte 1 hat.

versuche ich das so...

select Spalte1,
            Spalte2,
           (select Count(*) from Tabelle where MATNR = Spalte1
from Tabelle

... bekomme ich den Count immer über alles in der Abfrage.

Grüße

Florian

Content-Key: 647673

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

Printed on: April 26, 2024 at 17:04 o'clock

Member: NordicMike
NordicMike Feb 03, 2021 updated at 14:09:25 (UTC)
Goto Top
Fehlt da nicht irgendwo ein Klammer zu? )

select
Spalte1, Spalte2, (select Count(Spalte1) as Spalte 3 where Spalte1=MATNR)
from Tabelle
Member: Florian86
Florian86 Feb 03, 2021 updated at 14:16:41 (UTC)
Goto Top
Ja sorry habe die ) vergessen hinzuschreiben im Code ist sie.

select distinct cast(NSDM_V_MSEG.MATNR as int),
       MAKT.MAKTX,
       (select count(*) from SAPABAP1.NSDM_V_MSEG inner join SAPABAP1.MAKT on NSDM_V_MSEG.MATNR = MAKT.MATNR inner join 
       SAPABAP1.NSDM_V_MKPF on NSDM_V_MSEG.MBLNR = NSDM_V_MKPF.MBLNR 
                        where NSDM_V_MSEG.MATNR = NSDM_V_MSEG.MATNR and NSDM_V_MSEG.WERKS = '2000'   
                        and NSDM_V_MKPF.USNAM = ""  
                        and cast(NSDM_V_MKPF.BUDAT as Datetime) = '03.02.2021' and NSDM_V_MSEG.BWART between 101 and 102)  

from SAPABAP1.NSDM_V_MSEG inner join SAPABAP1.MAKT on NSDM_V_MSEG.MATNR = MAKT.MATNR inner join SAPABAP1.NSDM_V_MKPF on NSDM_V_MSEG.MBLNR = NSDM_V_MKPF.MBLNR 
where NSDM_V_MSEG.MATNR between 50000 and 50399 and NSDM_V_MSEG.WERKS = '2000' and NSDM_V_MKPF.USNAM = ""  
      and cast(NSDM_V_MKPF.BUDAT as Datetime) = '03.02.2021'  

Es geht halt um diese Stelle... where NSDM_V_MSEG.MATNR = NSDM_V_MSEG.MATNR
Member: it-frosch
Solution it-frosch Feb 03, 2021 updated at 14:59:59 (UTC)
Goto Top
Es geht halt um diese Stelle... where NSDM_V_MSEG.MATNR = NSDM_V_MSEG.MATNR
Die Stelle macht für mich überhaupt keinen Sinn.

Wenn du dieselbe Tabelle noch einmal einbindest dann nimm einen anderen Alias.

Select a.feld1,b.feld1 from tabelle a
inner join tabelle b
on a.idnr=b.idnr 

Ich möchte also zusätzlich eine 3. Spalte mit einem Count haben, welche als Filter aber den Wert als Materialnummer
aus Spalte 1 hat.
Wenn ich dich richtig verstehe, willst du so etwas erhalten:
artikelnr      Artikelinfo          Anzahl

Dann wäre das so:
Select artikelnr,artikelinfo, count(*) from tabelle group by artikelnr,artikelinfo


grüße vom it-frosch
Member: Florian86
Florian86 Feb 04, 2021 at 06:08:15 (UTC)
Goto Top
Hallo it-frosch,

genau das möchte ich so

Select artikelnr,artikelinfo, count(*) from tabelle group by artikelnr,artikelinfo

Danke hat funktioniert.