关于查询的一些问题(具体看文章内容)

mdh_zl 2008-04-21 03:07:17
数据库表
开始时间 结束时间 价格
2008-04-01 2008-04-10 15
2008-04-11 2008-04-20 20

查询后得到这样的结果

2008-04-09 15
2008-04-10 15
2008-04-11 20

请问可以实现吗
...全文
102 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
hjj841020 2008-04-23
  • 打赏
  • 举报
回复
我也猜一下:
create table #T(be datetime,en datetime, value int)
insert into #T select '2008-4-1','2008-4-10',15
insert into #T select '2008-4-11','2008-4-20',20

select top 100 id=identity(int,1,1)into #A from syscolumns

select * from
(
select dateadd(d,b.id-1,a.be) date,value from #t a ,#a b
where datediff(d,a.be,a.en)>=b.id-1
)a where date between '2008-04-09' and '2008-04-11'


date value
------------------------------------------------------ -----------
2008-04-09 00:00:00.000 15
2008-04-10 00:00:00.000 15
2008-04-11 00:00:00.000 20

(所影响的行数为 3 行)
-狙击手- 2008-04-22
  • 打赏
  • 举报
回复
我也猜不出为,
楼下go on
dawugui 2008-04-22
  • 打赏
  • 举报
回复
[Quote=引用楼主 mdh_zl 的帖子:]
数据库表
开始时间 结束时间 价格
2008-04-01 2008-04-10 15
2008-04-11 2008-04-20 20

查询后得到这样的结果

2008-04-09 15
2008-04-10 15
2008-04-11 20

请问可以实现吗
[/Quote]
猜不出来.楼下继续.
补课 2008-04-22
  • 打赏
  • 举报
回复
同意
[Quote=引用 8 楼 playwarcraft 的回复:]
SQL code
--猜一下吧,可能是這個意思

create table #T(be datetime,en datetime, value int)
insert into #T select '2008-4-1','2008-4-10',15
insert into #T select '2008-4-11','2008-4-20',20

declare @be datetime,@en datetime
set @be='2008-4-9'
set @en='2008-4-11'

declare @t table ([date] datetime)
insert into @t select @be
while @be<@en
begin
insert into @t select dateadd(day,1,…
[/Quote]
flairsky 2008-04-22
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 Limpire 的回复:]
引用 12 楼 happyflystone 的回复:
我也猜不出为,
楼下go on


我猜到了结果,没有猜到过程,楼下继续
[/Quote]
suntoto 2008-04-22
  • 打赏
  • 举报
回复
如果一日一个记录,可查询出来
Limpire 2008-04-22
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 happyflystone 的回复:]
我也猜不出为,
楼下go on
[/Quote]

我猜到了结果,没有猜到过程,楼下继续
mengfeihui 2008-04-21
  • 打赏
  • 举报
回复
比较同意六楼的观点。。
playwarcraft 2008-04-21
  • 打赏
  • 举报
回复

--猜一下吧,可能是這個意思

create table #T(be datetime,en datetime, value int)
insert into #T select '2008-4-1','2008-4-10',15
insert into #T select '2008-4-11','2008-4-20',20

declare @be datetime,@en datetime
set @be='2008-4-9'
set @en='2008-4-11'

declare @t table ([date] datetime)
insert into @t select @be
while @be<@en
begin
insert into @t select dateadd(day,1,@be)
set @be=dateadd(day,1,@be)
end

select [date], isnull(value,0) as value
from @t A
left join #T
on A.[date] between #T.be and #T.en

/*
date value
------------------------------------------------------ -----------
2008-04-09 00:00:00.000 15
2008-04-10 00:00:00.000 15
2008-04-11 00:00:00.000 20

*/
drop table #t
zccmy22 2008-04-21
  • 打赏
  • 举报
回复
呵呵,不知我说的是不是你想要的。

zccmy22 2008-04-21
  • 打赏
  • 举报
回复
select doat, (select 价格 from tb where 时间>='开始时间' and 时间 <= '结束时间') from tb1
liangCK 2008-04-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 playwarcraft 的回复:]
報告:沒看懂,飄過
[/Quote]
zccmy22 2008-04-21
  • 打赏
  • 举报
回复
可以的,
假设。doat 为时间例。

select doat, case when (doat >= '2008-04-01' and doat <='2008-04-10') then 15 else case when (doat > '2008-04-10' and doat <='2008-04-20') then 20 else 0 end end from tb
mdh_zl 2008-04-21
  • 打赏
  • 举报
回复
比如我查询 9号到11号的价格

得到这样的数据
2008-04-09 15
2008-04-10 15
2008-04-11 20


可以实现吗
dobear_0922 2008-04-21
  • 打赏
  • 举报
回复
这个结果怎么来的?
playwarcraft 2008-04-21
  • 打赏
  • 举报
回复
報告:沒看懂,飄過

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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