Только что исправили синтаксические ошибки, и мы не будем обсуждать, насколько странен запрос.
declare @Age int
set @Age=20
declare @Sex varchar(20)
set @Sex='M'
select
d.Dep_Name,d.Dep_Code,g.Group_Name,t.Test_Name,t.Test_Unit,
case
when at1.First_Age>=@Age and @Sex='M' then convert(varchar(10),at1.L_Bound_M_LessThan_1yr)+'-'+convert(varchar(10),at1.U_Bound_M_LessThan_1yr)
when (at1.First_Age<@Age and at1.Second_Age>=@Age) and @Sex='M' then convert(varchar(10),at1.L_Bound_M_LessThan_20yr)+'-'+convert(varchar(10),at1.U_Bound_M_LessThan_20yr)
when (at1.Second_Age<@Age and at1.Third_Age>=@Age) and @Sex='M' then convert(varchar(10),at1.L_Bound_M_LessThan_40yr)+'-'+convert(varchar(10),at1.U_Bound_M_LessThan_40yr)
when (at1.Third_Age<@Age and at1.Fourth_Age>=@Age) and @Sex='M' then convert(varchar(10),at1.L_Bound_M_LessThan_60yr)+'-'+convert(varchar(10),at1.U_Bound_M_LessThan_60yr)
when (at1.Fourth_Age<@Age and at1.Fifth_Age>=@Age) and @Sex='M' then convert(varchar(10),at1.L_Bound_M_LessThan_125yr)+'-'+convert(varchar(10),at1.U_Bound_M_LessThan_125yr)
when at1.First_Age>=@Age and @Sex='F' then convert(varchar(10),at1.L_Bound_F_LessThan_1yr)+'-'+convert(varchar(10),at1.U_Bound_F_LessThan_1yr)
when (at1.First_Age<@Age and at1.Second_Age>=@Age) and @Sex='F' then convert(varchar(10),at1.L_Bound_F_LessThan_20yr)+'-'+convert(varchar(10),at1.U_Bound_F_LessThan_20yr)
when (at1.Second_Age<@Age and at1.Third_Age>=@Age) and @Sex='F' then convert(varchar(10),at1.L_Bound_F_LessThan_40yr)+'-'+convert(varchar(10),at1.U_Bound_F_LessThan_40yr)
when (at1.Third_Age<@Age and at1.Fourth_Age>=@Age) and @Sex='F' then convert(varchar(10),at1.L_Bound_F_LessThan_60yr)+'-'+convert(varchar(10),at1.U_Bound_F_LessThan_60yr)
when (at1.Fourth_Age<@Age and at1.Fifth_Age>=@Age) and @Sex='F' then convert(varchar(10),at1.L_Bound_F_LessThan_60yr)+'-'+convert(varchar(10),at1.U_Bound_F_LessThan_60yr)
ELSE NULL END AS x ,
st.Sub_Test_Name,st.Sub_Test_Unit,p.Result_Type,p.Numeric_Value,p.Paragraph_Value,p.Result_Normal,p.Sub_Test_ID,
case
when ast.First_Age>=@Age and @Sex='M' then convert(varchar(10),ast.L_Bound_M_LessThan_1yr)+'-'+convert(varchar(10),ast.U_Bound_M_LessThan_1yr)
when (ast.First_Age<@Age and ast.Second_Age>=@Age) and @Sex='M' then convert(varchar(10),ast.L_Bound_M_LessThan_20yr)+'-'+convert(varchar(10),ast.U_Bound_M_LessThan_20yr)
when (ast.Second_Age<@Age and ast.Third_Age>=@Age) and @Sex='M' then convert(varchar(10),ast.L_Bound_M_LessThan_40yr)+'-'+convert(varchar(10),ast.U_Bound_M_LessThan_40yr)
when (ast.Third_Age<@Age and ast.Fourth_Age>=@Age) and @Sex='M' then convert(varchar(10),ast.L_Bound_M_LessThan_60yr)+'-'+convert(varchar(10),ast.U_Bound_M_LessThan_60yr)
when (ast.Fourth_Age<@Age and ast.Fifth_Age>=@Age) and @Sex='M' then convert(varchar(10),ast.L_Bound_M_LessThan_125yr)+'-'+convert(varchar(10),ast.U_Bound_M_LessThan_125yr)
when ast.First_Age>=@Age and @Sex='F' then convert(varchar(10),ast.L_Bound_F_LessThan_1yr)+'-'+convert(varchar(10),ast.U_Bound_F_LessThan_1yr)
when (ast.First_Age<@Age and ast.Second_Age>=@Age) and @Sex='F' then convert(varchar(10),ast.L_Bound_F_LessThan_20yr)+'-'+convert(varchar(10),ast.U_Bound_F_LessThan_20yr)
when (ast.Second_Age<@Age and ast.Third_Age>=@Age) and @Sex='F' then convert(varchar(10),ast.L_Bound_F_LessThan_40yr)+'-'+convert(varchar(10),ast.U_Bound_F_LessThan_40yr)
when (ast.Third_Age<@Age and ast.Fourth_Age>=@Age) and @Sex='F' then convert(varchar(10),ast.L_Bound_F_LessThan_60yr)+'-'+convert(varchar(10),ast.U_Bound_F_LessThan_60yr)
when (ast.Fourth_Age<@Age and ast.Fifth_Age>=@Age) and @Sex='F' then convert(varchar(10),ast.L_Bound_F_LessThan_60yr)+'-'+convert(varchar(10),ast.U_Bound_F_LessThan_60yr)
ELSE NULL END AS y
from Patient_Test_3SC p
left join((Tests t inner join Advanced_test_detail at1 on t.test_id=at1.test_id )
left join (Sub_Tests st inner join Advanced_Sub_tests ast on st.sub_test_id=ast.sub_test_id) on t.Test_Code=st.Sub_Tests_Test_Code) on p.Test_ID=t.Test_ID
inner join Department d on p.Department_Code=d.Dep_Code
left join Groups g on p.Group_Code=g.Group_Code
where p.Patient_ID=@pid