这个SQL查询有一些错误

时间:2012-01-12 05:42:37

标签: sql-server-2005

我写了以下查询,但似乎有错误,我想知道是否有人可以帮我找到它?对不起,我很抱歉。

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

我在代码中遇到了一些错误,我不确定它们来自哪里。有人可以帮我弄清楚为什么我在这个SQL代码中有错误吗?

3 个答案:

答案 0 :(得分:3)

刚刚纠正了语法错误,我们不会讨论查询有多奇怪。

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

答案 1 :(得分:0)

来自Patient_Test_3SC p

也许它需要在表Patient_test_3SC和p

之间有一个','

答案 2 :(得分:0)

declare @Age int 
declare @Sex varchar(20)
declare @pid varchar(20)

set @Age=20
set @Sex='M'

SELECT
    d.Dep_Name,d.Dep_Code,g.Group_Name,t.Test_Name,t.Test_Unit,
    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 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
    END as 'var1',
    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
    END as 'var2'
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