在SQL里面运行正常,可是在ACCESS里面总是报语法错误,请问到底那里错了

selectL.*from(select*from[Web_Patient]asop1wherenotexists(select1from[Web_Patient]asi... select L.* from
(select * from [Web_Patient] as op1
where not exists(select 1 from [Web_Patient] as ip1 where ip1.[UserName]=op1.[Username] and ip1.[id]>op1.[id])) as L
join (select * from [Web_Patient] as op2 where
(select count(*) from [Web_Patient] as ip2 where ip2.[UserName]=op2.[Username]
and ip2.[id]>op2.[id])=1) as R on L.[UserName]=R.[UserName]
select L.* from (select * from #Web_Patient as op1 where
not exists(select 1 from #Web_Patient as ip1 where ip1.UserName=op1.Username and ip1.id>op1.id)) as L
join
(select * from #Web_Patient as op2 where
(select count(*) from #Web_Patient as ip2 where ip2.UserName=op2.Username and ip2.id>op2.id)=1) as R
on L.UserName=R.UserName
where (case when L.ALT<=10 then 10-L.ALT
when L.ALT>=50 then L.ALT-50
else 0
end)>
(case when R.ALT<=10 then 10-R.ALT
when R.ALT>=50 then R.ALT-50
else 0
end)
目的:
查询 “最后一次的结果比第一次好”

注解:‘ALT’ 肝脏检查里面的一个数据,比如正常人的 ALT值,应该是 10—50之间;

举例,如果‘王三’ 最近一次的查询的ALT值是65,而他上一次查询的ALT值是55,说明王三的ALT值恶化了。

需求,查询条件是,最近一次和上一次比较ALT恶化的患者数据
展开
 我来答
百度网友7c77daf
2011-03-23 · TA获得超过1603个赞
知道大有可为答主
回答量:744
采纳率:100%
帮助的人:842万
展开全部
这条sql有什么作用?
我试过了,在Sql Server里面执行是没有问题的。
Access对sql的语句支持是有限的。
不支持多层次嵌套的查询。
特别是这一句:
where (select count(*) from Web_Patient as ip2 where p2.UserName=op2.Username and ip2.id>op2.id)=1
你现在一张表Web_Patient ,两个字段(Username,id)
知道查询目的,重写一个就好了。

如不能解决问题,发百度消息给我。
追问
--,不行where (select count(*) from Web_Patient as ip2 where p2.UserName=op2.Username and ip2.id>op2.id)=1 
这句是用来,这一次的值,与上一次值来比较的
追答
才“一张表Web_Patient ,两个字段(Username,id)”你想查询得到什么结果。给另外一种解决方案。Access不支持那么复杂的查询。

明白你的意思了。。刚才分三步改写了一下sql:

create table Web_Patient(id int,ALT int,username varchar(16));

insert into Web_Patient(id,ALT,username) values (1,1,'a');
insert into Web_Patient(id,ALT,username) values (2,2,'b');
insert into Web_Patient(id,ALT,username) values (3,3,'a');
insert into Web_Patient(id,ALT,username) values (4,3,'b');
insert into Web_Patient(id,ALT,username) values (5,7,'a');
insert into Web_Patient(id,ALT,username) values (6,8,'c');

--得到每位病人最后一次诊查报告
select * from Web_Patient where id in (select max(id) as max_id from Web_Patient group by username)

--得到倒数第二次报告
select * from Web_Patient where id in (select max(id) as max_id from Web_Patient where id not in (select max(id) as max_id from Web_Patient group by username) group by username)

--用“用户名”为主键连接两次表,查询状态恶化了的(即查询 最后一次的alt>倒数第二次的alt)
select L1.*,L2.ALT as 上一次报告Alt值 from
(select * from Web_Patient where id in (select max(id) as max_id from Web_Patient group by username)) L1
left join
(select * from Web_Patient where id in (select max(id) as max_id from Web_Patient where id not in (select max(id) as max_id from Web_Patient group by username) group by username)) L2
on L1.username=L2.username
where L1.alt>L2.alt

--加上一个条件,排除最后一次检查恢复正常值水平的报告。。
select L1.*,L2.ALT as 上一次报告Alt值 from
(select * from Web_Patient where id in (select max(id) as max_id from Web_Patient group by username)) L1
left join
(select * from Web_Patient where id in (select max(id) as max_id from Web_Patient where id not in (select max(id) as max_id from Web_Patient group by username) group by username)) L2
on L1.username=L2.username
where L1.alt>L2.alt and L1.alt not between 10 and 50

另外,Access中没有Case When,只有IIF()函数。
chymingyan
2011-03-23 · 超过25用户采纳过TA的回答
知道答主
回答量:120
采纳率:0%
帮助的人:77.4万
展开全部
缺'这个符号
追问
麻烦您说清楚点,什么地方少  '  这个符号了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式