一些比较难的sql问题:3(二)

2014-11-24 14:10:00 · 作者: · 浏览: 1
erial,Result,LabTestDate from PatientLabTestResults
where ProbeMaterial='Ca'
and LabTestDate between convert(datetime,2013/1/1) and
convert(datetime,2013/6/24) order by LabTestDate desc
这样后就查不出结果了,其实只是去除了2013/6/24的'号而已,这个大家能理解是什么问题吗?
[sql]
if OBJECT_ID('t') is not null
drop table t
go
create table t(d datetime)
insert into t
select cast('2013-05-01' as datetime) as d
union all
select cast('2013-05-10' as datetime)
/*
1.
通过查询计划能看出,SQL Server把下面的查询转化成了:
select * from t where d >= convert(datetime,2013/5/1,0) and d <= convert(datetime,'2013/6/24',0)
也就是查询条件:
d >= convert(datetime,2013/5/1,0) and d <= convert(datetime,'2013/6/24',0)
进一步转化:
d >= '1901-02-07 00:00:00.000' and d <= 2013-06-24 00:00:00.000
这样就能查询出结果集。
*/
select *
from t
where d between convert(datetime,2013/5/1) and convert(datetime,'2013/6/24')
/*
2.
通过查询计划能看出,SQL Server把下面的查询转化成了:
select * from t where d >= convert(datetime,2013/5/1,0) and d <= convert(datetime,2013/6/24,0)
也就是查询条件:
d >= convert(datetime,2013/5/1,0) and d <= convert(datetime,2013/6/24,0)
进一步转化:
d >= '1901-02-07 00:00:00.000' and d <= '1900-01-14 00:00:00.000'
由于SQL Server 把2013/6/24中的斜杠,当成了除号,也就是按除法计算了,
比如:2013/6/24 就等于13,那么由于datetime默认值是默认值: 1900-01-01 00:00:00,
那么加上13后,就是1900-01-14 00:00:00.000,这样后就查不出结果了.
*/
select *
from t
where d between convert(datetime,2013/5/1) and convert(datetime,2013/6/24)
其实就是,
2013/5/1 就是一个除法运算,结果为402。
2013/6/24 做除法运算后,就是13。
由于datetime数据类型的默认值为:'1900-01-01 00:00:00',
所以上面的convert(datetime,2013/5/1)就是'1900-01-01 00:00:00' 再加上402,
就是'1901-02-07 00:00:00.000',
而convert(datetime,2013/6/24)就是是'1900-01-01 00:00:00' 再加上 13,
就是'1900-01-14 00:00:00.000',
所以就会查不出结果来。
所以,上面的2013/5/1 要写成 '2013/5/1',一定要加上引号。
4、查询出一段数据后判断记录里面的最大id,是否大于值a 查询语句如下:
[sql]
select top 200 id, ClassId,Name,Price,BoxContain,BoxLength,BoxWidth,BoxHeight,CName,EName,CPack,PhotoFolder,EPack,0.46*(round(((convert(decimal(10,2),BoxLength)*convert(decimal(10,2),BoxWidth) * convert(decimal(10,2),BoxHeight))/1000000),3))*35.32/BoxContain+Price/6*1.08 from ProductData where ClassId=101 and BoxContain >0 and BoxContain is not null and round(((convert(decimal(10,2),BoxLength)*convert(decimal(10,2),BoxWidth) * convert(decimal(10,2),BoxHeight))/1000000),3) >=0 And round(((convert(decimal(10,2),BoxLength)*convert(decimal(10,2),BoxWidth) * convert(decimal(10,2),BoxHeight))/1000000),3) <= 10000 and (0.46*(round(((convert(decimal(10,2),BoxLength)*convert(decimal(10,2),BoxWidth) * convert(decimal(10,2),BoxHeight))/1000000),3))*35.32/BoxContain+Price/6*1.08) >=10 and (0.46*(round(((convert(decimal(10,2),BoxLength)*convert(decimal(10,2),BoxWidth) * convert(decimal(10,2),BoxHeight))/1000000),3))*35.32/BoxContain+Price/6*1.08) <=1000 and EPack='Window Box' order by id asc
我的解法,适用于SQL Server 2000:
[sql]
select *
from
(
select top 200 id, ClassId,Name,Price,BoxContain,BoxLength,BoxWidth,BoxHeight,CName,EName,CPack,PhotoFolder,EPack,
0.46*(round(((convert(decimal(10,2),BoxLength)*convert(decimal(10,2),BoxWidth) * convert(decimal(10,2),BoxHeight))/1000000),3))*35.32/BoxContain+Price/6*1.08
from ProductData
where ClassId=101 and BoxContain >0 and BoxContain is not null
and round(((convert(decimal(10,2),BoxLength)*convert(decimal(10,2),BoxWidth) * convert(decimal(10,2),BoxHeight))/1000000),3) >=0
And round(((