banxitan

          統(tǒng)計(jì)

          留言簿(2)

          閱讀排行榜

          評論排行榜

          產(chǎn)品時(shí)間段(StartDate,EndDate)SQL查詢

          表 a 字段,id,開始時(shí)間,結(jié)束時(shí)間,價(jià)格
          1,2008-01-01,2008-09-10,220.0
          2,2008-09-11,2008-09-30,280.0
          3,2008-10-01,2008-10-10,320.0
          3,2008-10-11,2008-12-31,350.0

          輸入一個(gè)開始時(shí)間,一個(gè)結(jié)束時(shí)間,返回這段時(shí)間內(nèi)每天的價(jià)格,最好返回每天的日期和對應(yīng)的價(jià)格
          例輸入開始時(shí)間2008-09-09,結(jié)束時(shí)間2008-09-13
          返回
          2008-09-09,220
          2008-09-10,220
          2008-09-11,280
          2008-09-12,280
          2008-09-13,280

          方案一:采用存儲(chǔ)過程的方案!
          create table T([id] int,[開始時(shí)間] datetime,[結(jié)束時(shí)間] datetime,[價(jià)格] numeric(4,1))
          insert into T
          select 1,'2008-01-01','2008-09-10',220.0 union all
          select 2,'2008-09-11','2008-09-30',280.0 union all
          select 3,'2008-10-01','2008-10-10',320.0 union all
          select 3,'2008-10-13','2008-12-31',350.0

          select * from T
          go
          --Code
          create procedure GetTimePrice(@begintime datetime,@endtime datetime)
          as
          declare @tmptime datetime
          declare @tmp table (dt datetime,price numeric(4,1))--結(jié)果表
          begin
          set @tmptime=@begintime
          while @tmptime<=@endtime
          begin

          insert into @tmp
          select @tmptime,價(jià)格  from t where @tmptime between 開始時(shí)間 and 結(jié)束時(shí)間
          set @tmptime=DATEADD(dd,1,@tmptime)
          end
          select * from @tmp
          end
          go

          exec GetTimePrice '2008-09-09','2008-09-14'--執(zhí)行
          --
          Drop
          drop table T
          drop procedure GetTimePrice


          方案二:采用Case方案
          set nocount on
          create table T([id] int,[開始時(shí)間] datetime,[結(jié)束時(shí)間] datetime,[價(jià)格] numeric(4,1))
          insert into T
          select 1,'2008-01-01','2008-09-10',220.0 union all
          select 2,'2008-09-11','2008-09-30',280.0 union all
          select 3,'2008-10-01','2008-10-10',320.0 union all
          select 3,'2008-10-11','2008-12-31',350.0

          declare @bgnTime datetime set @bgnTime = '2008-09-09'
          declare @endTime datetime set @endTime = '2008-09-13'
          select id
              ,
          case when [開始時(shí)間]<@bgnTime then @bgnTime else [開始時(shí)間] end as [開始時(shí)間] -- 讓輸出結(jié)果更貼近參數(shù)表現(xiàn)出來
              ,case when [結(jié)束時(shí)間]>@endTime then @endTime else [結(jié)束時(shí)間] end as [結(jié)束時(shí)間] -- 讓輸出結(jié)果更貼近參數(shù)表現(xiàn)出來
              ,[價(jià)格]
          from T
          where [開始時(shí)間]<@endTime
          and [結(jié)束時(shí)間]>@bgnTime
          -- id,開始時(shí)間,結(jié)束時(shí)間
          --
           1,2008-09-09 00:00:00.000,2008-09-10 00:00:00.000
          --
           2,2008-09-11 00:00:00.000,2008-09-13 00:00:00.000

          drop table T

          id          開始時(shí)間                                                   結(jié)束時(shí)間                                                   價(jià)格     
          ----------- ------------------------------------------------------ ------------------------------------------------------ ------ 
          1           2008-09-09 00:00:00.000                                2008-09-10 00:00:00.000                                220.0
          2           2008-09-11 00:00:00.000                                2008-09-13 00:00:00.000                                280.0

          posted on 2009-04-23 20:50 MikyTan 閱讀(799) 評論(0)  編輯  收藏 所屬分類: SQL

          主站蜘蛛池模板: 绥芬河市| 乌拉特中旗| 定西市| 青铜峡市| 彩票| 揭西县| 旌德县| 耒阳市| 西盟| 进贤县| 封丘县| 和硕县| 奉化市| 孝感市| 哈密市| 墨玉县| 铜川市| 中卫市| 普安县| 和田市| 察隅县| 镶黄旗| 阿拉善右旗| 定远县| 沁源县| 安塞县| 蓬溪县| 平塘县| 宁明县| 翁牛特旗| 黄陵县| 鄂尔多斯市| 尉氏县| 蛟河市| 岳阳县| 囊谦县| 伊金霍洛旗| 泸水县| 青州市| 平江县| 来凤县|