в этом sql запросе есть какая-то ошибка - PullRequest
0 голосов
/ 12 января 2012

Я написал следующий запрос, но он, похоже, содержит ошибку, и мне было интересно, если кто-нибудь может помочь мне найти его?Я сожалею о том, как долго.

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 ]

3 голосов
/ 12 января 2012

Только что исправили синтаксические ошибки, и мы не будем обсуждать, насколько странен запрос.

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
0 голосов
/ 12 января 2012
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
0 голосов
/ 12 января 2012

из Patient_Test_3SC p

возможно, ему нужно иметь ',' между таблицей Patient_test_3SC и p

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...