求一SQL,数据区间判断。。。。。。。。。。。。。。

luxi0194 2010-07-07 02:09:21
检查结论表 Result
检查结论ID 结论 严重等级
ResultID Result Seriouslevel
1 正常 1
2 保健 2
3 复查 3
4 异常 4


标注数据表 StandardData
检查类型 从年龄 从年龄(单位:月/年) 到年龄 到年龄(单位:年/月) 从数值区间:[/( 从数值 到数值 到数值区间:]/) 检查结论ID 诊断意见
EyesightType FromAge FromAgeUnit ToAge ToAgeUnit LeftInterZone FromData ToData RightInterZone ResultID Suggestion
c 6 月 2 年 [ -9.99 -1 ] 4 到眼科诊治
c 2 年 4 年 [ -9.99 -1 ] 4 到眼科诊治
c 4 年 6 年 [ -9.99 -1 ] 4 到眼科诊治
c 6 年 10 年 [ -9.99 -1 ] 4 到眼科诊治
c 10 年 [ -9.99 -1 ] 4 到眼科诊治
c 6 月 2 年 ( -1 1 ) 1 正常
c 2 年 4 年 ( -1 1 ) 1 正常
c 4 年 6 年 ( -1 1 ) 1 正常
c 6 年 10 年 ( -1 1 ) 1 正常
c 10 年 ( -1 1 ) 1 正常
c 6 月 2 年 [ 1 9.99 ] 4 到眼科诊治
c 2 年 4 年 [ 1 9.99 ] 4 到眼科诊治
c 4 年 6 年 [ 1 9.99 ] 4 到眼科诊治
c 6 年 10 年 [ 1 9.99 ] 4 到眼科诊治
c 10 年 [ 1 9.99 ] 4 到眼科诊治
s 6 月 2 年 [ -9.99 -1 ] 4 到眼科诊治
s 2 年 4 年 [ -9.99 -1 ] 4 到眼科诊治
s 4 年 6 年 [ -9.99 -1 ] 4 到眼科诊治
s 6 年 10 年 [ -9.99 -1 ] 4 到眼科诊治
s 10 年 [ -9.99 -1.5 ] 4 到眼科诊治
s 6 月 2 年 [ -1 2.5 ) 2 眼保健
s 2 年 4 年 ( -1 2 ) 2 眼保健
s 4 年 6 年 ( -1 2 ) 2 眼保健
s 6 年 10 年 ( -1 1.5 ) 2 眼保健
s 6 月 2 年 [ 2.5 3.5 ] 1 正常
s 2 年 4 年 [ 2 3 ) 1 正常
s 4 年 6 年 [ 2 2.5 ) 1 正常
s 6 年 10 年 [ 1.5 2 ) 1 正常
s 10 年 ( -1.5 1.5 ) 1 正常
s 6 月 2 年 ( 3.5 9.99 ] 4 异常
s 2 年 4 年 [ 3 9.99 ] 4 异常
s 4 年 6 年 [ 2.5 9.99 ] 4 异常
s 6 年 10 年 [ 2 9.99 ] 4 异常
s 10 年 [ 1.5 9.99 ] 4 异常


眼睛检查数值表 ExamineData:该表中的S,C对应的是表StandardData中的EyesightType字段
ID 年龄(单位:年) 左眼S值 左眼C值 右眼S值 右眼C值
ID Age LSValue LCValue RSValue RCValue
1 0.5 2.5 -1.2 3 -1
2 3 4 4 4 4
3 5 3.5 2 3.5 2
4 11 1 1 1 1

最后得出:
ID Age LSValue LCValue RSValue RCValue 检查结论 意见
1 0.5 2.5 -1.2 3 -1 1 正常
2 3 4 4 4 4 4 到眼科诊治
3 5 3.5 2 3.5 2 4 到眼科诊治
4 11 1 1 1 1 1 正常


备注:LSValue,LCValue,RSValue,RCValue四个值如果得出的结果“检查结论,意见”如果完全一致,则无异议,如果得出来的结论不完全一致,则按严重等级选择,选择最严重的那个(比如有可能LSValue是1,正常,而LCValue是4,异常,则结果按4,异常处理。)

如果ToAge为空或者NUll 则代表>=FromAge

[]:代表数学中的区间的意思
():代表数学中的区间的意思

[包含起始值,(不包含起始值
]包含结束值,)不包含结束值
...全文
135 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
永生天地 2010-07-07
  • 打赏
  • 举报
回复
--查询
select *
from
(select t.*,s.ResultID,s.Suggestion from ExamineData t
left join StandardData s on EyesightType='s'
and t.age >=case when FromAgeUnit ='月' then FromAge *1./12 else FromAge end
and t.age <= case when toage is null then t.age else toage end
and (t.LSValue>= (case when LeftInterZone ='[' then FromData else null end) or t.LSValue> (case when LeftInterZone ='(' then FromData else null end) )
and (t.LSValue<= (case when RightInterZone =']' then toData else null end) or t.LSValue< (case when RightInterZone =')' then toData else null end) )
union all
select t.*,s.ResultID,s.Suggestion from ExamineData t
left join StandardData s on EyesightType='c'
and t.age >=case when FromAgeUnit ='月' then FromAge *1./12 else FromAge end
and t.age <= case when toage is null then t.age else toage end
and (t.LcValue>= (case when LeftInterZone ='[' then FromData else null end) or t.LcValue> (case when LeftInterZone ='(' then FromData else null end) )
and (t.LcValue<= (case when RightInterZone =']' then toData else null end) or t.LcValue< (case when RightInterZone =')' then toData else null end) )
union all
select t.*,s.ResultID,s.Suggestion from ExamineData t
left join StandardData s on EyesightType='s'
and t.age >=case when FromAgeUnit ='月' then FromAge *1./12 else FromAge end
and t.age <= case when toage is null then t.age else toage end
and (t.RSValue>= (case when LeftInterZone ='[' then FromData else null end) or t.RSValue> (case when LeftInterZone ='(' then FromData else null end) )
and (t.RSValue<= (case when RightInterZone =']' then toData else null end) or t.RSValue< (case when RightInterZone =')' then toData else null end) )
union all
select t.*,s.ResultID,s.Suggestion from ExamineData t
left join StandardData s on EyesightType='s'
and t.age >=case when FromAgeUnit ='月' then FromAge *1./12 else FromAge end
and t.age <= case when toage is null then t.age else toage end
and (t.RcValue>= (case when LeftInterZone ='[' then FromData else null end) or t.RcValue> (case when LeftInterZone ='(' then FromData else null end) )
and (t.RcValue<= (case when RightInterZone =']' then toData else null end) or t.RcValue< (case when RightInterZone =')' then toData else null end) )
) t
where
t.ResultID = (select max(ResultID) from
(select t.*,s.ResultID,s.Suggestion from ExamineData t
left join StandardData s on EyesightType='s'
and t.age >=case when FromAgeUnit ='月' then FromAge *1./12 else FromAge end
and t.age <= case when toage is null then t.age else toage end
and (t.LSValue>= (case when LeftInterZone ='[' then FromData else null end) or t.LSValue> (case when LeftInterZone ='(' then FromData else null end) )
and (t.LSValue<= (case when RightInterZone =']' then toData else null end) or t.LSValue< (case when RightInterZone =')' then toData else null end) )
union all
select t.*,s.ResultID,s.Suggestion from ExamineData t
left join StandardData s on EyesightType='c'
and t.age >=case when FromAgeUnit ='月' then FromAge *1./12 else FromAge end
and t.age <= case when toage is null then t.age else toage end
and (t.LcValue>= (case when LeftInterZone ='[' then FromData else null end) or t.LcValue> (case when LeftInterZone ='(' then FromData else null end) )
and (t.LcValue<= (case when RightInterZone =']' then toData else null end) or t.LcValue< (case when RightInterZone =')' then toData else null end) )
union all
select t.*,s.ResultID,s.Suggestion from ExamineData t
left join StandardData s on EyesightType='s'
and t.age >=case when FromAgeUnit ='月' then FromAge *1./12 else FromAge end
and t.age <= case when toage is null then t.age else toage end
and (t.RSValue>= (case when LeftInterZone ='[' then FromData else null end) or t.RSValue> (case when LeftInterZone ='(' then FromData else null end) )
and (t.RSValue<= (case when RightInterZone =']' then toData else null end) or t.RSValue< (case when RightInterZone =')' then toData else null end) )
union all
select t.*,s.ResultID,s.Suggestion from ExamineData t
left join StandardData s on EyesightType='s'
and t.age >=case when FromAgeUnit ='月' then FromAge *1./12 else FromAge end
and t.age <= case when toage is null then t.age else toage end
and (t.RcValue>= (case when LeftInterZone ='[' then FromData else null end) or t.RcValue> (case when LeftInterZone ='(' then FromData else null end) )
and (t.RcValue<= (case when RightInterZone =']' then toData else null end) or t.RcValue< (case when RightInterZone =')' then toData else null end) )
) b where t.id=b.id)
--结果
/*

(4 行受影响)

(34 行受影响)

(4 行受影响)
ID Age LSValue LCValue RSValue RCValue ResultID Suggestion
----------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- ----------- --------------------
2 3.0 4.00 4.00 4.00 4.00 4 异常
3 5.0 3.50 2.00 3.50 2.00 4 异常
1 0.5 2.50 -1.20 3.00 -1.00 4 到眼科诊治
2 3.0 4.00 4.00 4.00 4.00 4 到眼科诊治
3 5.0 3.50 2.00 3.50 2.00 4 到眼科诊治
4 11.0 1.00 1.00 1.00 1.00 4 到眼科诊治
2 3.0 4.00 4.00 4.00 4.00 4 异常
3 5.0 3.50 2.00 3.50 2.00 4 异常
1 0.5 2.50 -1.20 3.00 -1.00 4 到眼科诊治
2 3.0 4.00 4.00 4.00 4.00 4 异常

(10 行受影响)
*/
永生天地 2010-07-07
  • 打赏
  • 举报
回复
好像数据有些不太对。
这个是union做法,也可以全都用join
--建立测试环境
IF OBJECT_ID('Result') IS NOT NULL DROP TABLE Result
GO
CREATE TABLE Result
(
ResultID int ,
Result varchar(10),
Seriouslevel int
)
GO
INSERT Result
select '1','正常','1' union all
select '2','保健','2' union all
select '3','复查','3' union all
select '4','异常','4'
go

IF OBJECT_ID('StandardData') IS NOT NULL DROP TABLE StandardData
GO
CREATE TABLE StandardData
(
EyesightType varchar(1) ,
FromAge int ,
FromAgeUnit varchar(2) ,
ToAge int ,
ToAgeUnit varchar(2) ,
LeftInterZone varchar(1) ,
FromData numeric(9,2) ,
ToData numeric(9,2) ,
RightInterZone varchar(1) ,
ResultID int ,
Suggestion varchar(20)
)
GO
INSERT StandardData
select 'c','6','月','2','年','[','-9.99','-1',']','4','到眼科诊治' union all
select 'c','2','年','4','年','[','-9.99','-1',']','4','到眼科诊治' union all
select 'c','4','年','6','年','[','-9.99','-1',']','4','到眼科诊治' union all
select 'c','6','年','10','年','[','-9.99','-1',']','4','到眼科诊治' union all
select 'c','10','年',null,null,'[','-9.99','-1',']','4','到眼科诊治' union all
select 'c','6','月','2','年','(','-1','1',')','1','正常' union all
select 'c','2','年','4','年','(','-1','1',')','1','正常' union all
select 'c','4','年','6','年','(','-1','1',')','1','正常' union all
select 'c','6','年','10','年','(','-1','1',')','1','正常' union all
select 'c','10','年',null,null,'(','-1','1',')','1','正常' union all
select 'c','6','月','2','年','[','1','9.99',']','4','到眼科诊治' union all
select 'c','2','年','4','年','[','1','9.99',']','4','到眼科诊治' union all
select 'c','4','年','6','年','[','1','9.99',']','4','到眼科诊治' union all
select 'c','6','年','10','年','[','1','9.99',']','4','到眼科诊治' union all
select 'c','10','年',null,null,'[','1','9.99',']','4','到眼科诊治' union all
select 's','6','月','2','年','[','-9.99','-1',']','4','到眼科诊治' union all
select 's','2','年','4','年','[','-9.99','-1',']','4','到眼科诊治' union all
select 's','4','年','6','年','[','-9.99','-1',']','4','到眼科诊治' union all
select 's','6','年','10','年','[','-9.99','-1',']','4','到眼科诊治' union all
select 's','10','年',null,null,'[','-9.99','-1.5',']','4','到眼科诊治' union all
select 's','6','月','2','年','[','-1','2.5',')','2','眼保健' union all
select 's','2','年','4','年','(','-1','2',')','2','眼保健' union all
select 's','4','年','6','年','(','-1','2',')','2','眼保健' union all
select 's','6','年','10','年','(','-1','1.5',')','2','眼保健' union all
select 's','6','月','2','年','[','2.5','3.5',']','1','正常' union all
select 's','2','年','4','年','[','2','3',')','1','正常' union all
select 's','4','年','6','年','[','2','2.5',')','1','正常' union all
select 's','6','年','10','年','[','1.5','2',')','1','正常' union all
select 's','10','年',null,null,'(','-1.5','1.5',')','1','正常' union all
select 's','6','月','2','年','(','3.5','9.99',']','4','异常' union all
select 's','2','年','4','年','[','3','9.99',']','4','异常' union all
select 's','4','年','6','年','[','2.5','9.99',']','4','异常' union all
select 's','6','年','10','年','[','2','9.99',']','4','异常' union all
select 's','10','年',null,null,'[','1.5','9.99',']','4','异常'
go
IF OBJECT_ID('ExamineData') IS NOT NULL DROP TABLE ExamineData
GO
CREATE TABLE ExamineData
(
ID int,
Age numeric(4,1),
LSValue numeric(9,2),
LCValue numeric(9,2),
RSValue numeric(9,2),
RCValue numeric(9,2)
)
GO
INSERT ExamineData
select 1,0.5,2.5,-1.2,3,-1 union all
select 2,3,4,4,4,4 union all
select 3,5,3.5,2,3.5,2 union all
select 4,11,1,1,1,1
go
ChinaJiaBing 2010-07-07
  • 打赏
  • 举报
回复
select ResultID,Suggestion
from CM_EyesightStandardData
where 5*12>= case when FromAgeUnit = 'y' then FromAge*12 else FromAge end
and 5*12 < case when ToAgeUnit='y' then ToAge*12 else ToAge end
and -2.0 between case when LeftInterZone='[' then FromData else FromData+0.0000001 end
and case when RightInterZone='[' then ToDate else ToDate-0.0000001 end
and HospitalID=41

--try


;with china as
(
select ResultID,Suggestion,fromage=(case when FromAgeUnit = 'y' then FromAge*12 else FromAge end),
toage=(case when ToAgeUnit='y' then ToAge*12 else ToAge end),
leftinterzone=(case when LeftInterZone='[' then FromData else FromData+0.0000001 end),
rightinterzone=(case when RightInterZone='[' then ToDate else ToDate-0.0000001 end) from
CM_EyesightStandardData where HospitalID=41
)
select ResultID,Suggestion from china where fromage<=5*12
and toage>5*12
and ((leftinterzone<=2.0 and rightinterzone>=2.0)
or(leftinterzone>=2.0 and rightinterzone<=2.0))
htl258_Tony 2010-07-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 luxi0194 的回复:]
其实倒不是很难,我就是觉得自己写的太乱,大家可以帮我看看上面的SQL有没有可以改进的地方,我想把LSValue,LCValue,RSValue,RCValue同事传进去,做3次union all来判断。
[/Quote]确实看的累
luxi0194 2010-07-07
  • 打赏
  • 举报
回复
其实倒不是很难,我就是觉得自己写的太乱,大家可以帮我看看上面的SQL有没有可以改进的地方,我想把LSValue,LCValue,RSValue,RCValue同事传进去,做3次union all来判断。
luxi0194 2010-07-07
  • 打赏
  • 举报
回复
之所以+ -0.0000001就是为了处理区间的问题。
luxi0194 2010-07-07
  • 打赏
  • 举报
回复
我写了一SQL,但感觉不好:5,-2.0其实是从外面传入的值:
select ResultID,Suggestion 
from CM_EyesightStandardData
where 5*12>= case when FromAgeUnit = 'y' then FromAge*12 else FromAge end
and 5*12 < case when ToAgeUnit='y' then ToAge*12 else ToAge end
and -2.0 between case when LeftInterZone='[' then FromData else FromData+0.0000001 end
and case when RightInterZone='[' then ToDate else ToDate-0.0000001 end
and HospitalID=41
guguda2008 2010-07-07
  • 打赏
  • 举报
回复
2楼的你不要拈轻怕重,赶紧写
SQL77 2010-07-07
  • 打赏
  • 举报
回复
...太长了看着晕
guguda2008 2010-07-07
  • 打赏
  • 举报
回复
老了,写不动了,交给年轻人

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧