0

I have this query in MsSQL

SELECT PersonnelID, Name, EmpStartCalc,
    MAX(PositionDetailsValidFromCalc) PD , 
    Max(PositionHierValidFromCalc) PH, 
    Max(PWAValidFromCalc) Position, 
    Max(RowId) As RowId
    
FROM TV_IAMintegration_VW
    
where 
    EmpStartCalc >= 20200101 and
    EmpStartCalc <= 20200131 and
    
    ((20200131 > PositionDetailsValidFromCalc And 
    20200101 < PositionDetailsValidToCalc) or
    PositionDetailsValidToCalc is null)
    
GROUP BY PersonnelID, Name, EmpStartCalc
    
ORDER BY PersonnelID Asc.

I have a scenario where I need the query to return Null, example:

When where clause doesn't successfully meet I need the query to return null in the following columns

MAX(PositionDetailsValidFromCalc) PD , Max(PositionHierValidFromCalc) PH, Max(PWAValidFromCalc) Position

but rest of the row remains same.

AliNawaz
  • 27
  • 3

1 Answers1

1

You could use a CTE:

with cte as (
      your query here
     )
select cte.*
from cte
union all
select null, null, null, null, null, null
from (values (1)) v(x)
where not exists (select 1 from cte);
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786