select example_a from example where time<=2009-11-01 and time >= 2009-01-01 在这个SQL语句里能否直接得到第一行example_a数据和最后一行example_a数据呢? 我现在的解决方案是TOP 1,然后在倒序TOP 1,这样的话影响效率。
select example_a from example t where [time] <='2009-11-01' and [time] >= '2009-01-01' and (not exists(select 1 from example where [time] <='2009-11-01' and [time] >= '2009-01-01' and [time]>t.[time]) or not exists(select 1 from example where [time] <='2009-11-01' and [time] >= '2009-01-01' and [time]<t.[time]) )
执行了1分多钟了还在执行。这个效率还没有TOP 1,然后在倒序TOP 1高啊。问题处在哪里?
晕。问题出在我转换了日期格式。难怪这么慢。。 select example_a from example t where [time] <='2009-11-01' and [time] >= '2009-01-01' and (not exists(select 1 from example where [time] <='2009-11-01' and [time] >= '2009-01-01' and [time]>t.[time]) or not exists(select 1 from example where [time] <='2009-11-01' and [time] >= '2009-01-01' and [time]<t.[time]) ) 能否把2行显示变成1行2列显示啊
用max(id)和min(id)呢?
from example t
where [time] <='2009-11-01' and [time] >= '2009-01-01'
and
(not exists(select 1 from example where [time] <='2009-11-01' and [time] >= '2009-01-01' and [time]>t.[time])
or
not exists(select 1 from example where [time] <='2009-11-01' and [time] >= '2009-01-01' and [time]<t.[time])
)
select example_a
from example t
where [time] <='2009-11-01' and [time] >= '2009-01-01'
and
(not exists(select 1 from example where [time] <='2009-11-01' and [time] >= '2009-01-01' and [time]>t.[time])
or
not exists(select 1 from example where [time] <='2009-11-01' and [time] >= '2009-01-01' and [time]<t.[time])
)
能否把2行显示变成1行2列显示啊