Skip to content
Advertisement

specific select within sql server

I have a table vbap : For all distinct POSNR, PARVW, I need to check if POSNR=POSNR2, if it’s the case I select KUNNR. if POSNR<>POSNR2 and POSNR2 =’00000′ I select KUNNR

enter image description here

Result:

enter image description here

I didn’t understand how to do it?

Advertisement

Answer

This seems like just a where cause:

select t.*
from t
where POSNR2 in (POSNR, '00000')

Perhaps you are trying to prioritize the rows, to select one row per kunnr. If so:

select t.*
from (select t.*,
             row_number() over (partition by parvw order by psnr2 desc) as seqnum
      from t
      where POSNR2 in (POSNR, '00000')
     ) t
where seqnum = 1;
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement