I wrote the following query, but it appears to have an error, and I was wondering if anyone could help me find it? I’m sorry about how long it is.
declare @Age int
declare @Sex varchar(20)
set @Age=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)
-- 5 more when statements
),
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)
--more WHEN's - mirrors the above case
)
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
I’ve had some errors in the code, and I’m not entirely sure where they’re from. Could someone please assist me in figuring out why I have an error in this SQL code?
1 Answer