求一条sql语句

harryCom 2005-01-29 04:32:01
已知这个table:
StartDate EndDate Cost
2005-01-23 2005-01-29 10
2005-01-30 2005-02-05 14
2005-02-06 2005-02-12 20

想得到的结果:
StartDate EndDate Cost
2005-01-23 2005-01-29 10
2005-01-30 2005-01-31 4
2005-02-01 2005-02-05 10
2005-02-06 2005-02-12 20
...全文
76 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
harryCom 2005-01-29
  • 打赏
  • 举报
回复
非常感谢Softlee81307(孔腎) ,问题已解决
pbsql 2005-01-29
  • 打赏
  • 举报
回复
create table #t(StartDate datetime,EndDate datetime,Cost int)
insert #t
select '2005-01-23', '2005-01-29', 10 union all
select '2005-01-30', '2005-02-05', 14 union all
select '2005-02-06', '2005-02-12', 20

select StartDate,EndDate,Cost
from #t where datediff(month,StartDate,EndDate)=0
union all
select StartDate,
dateadd(month,1,StartDate)-day(dateadd(month,1,StartDate)),
cost*(datediff(day,StartDate,dateadd(day,-day(EndDate),EndDate))+1)/7
from #t where datediff(month,StartDate,EndDate)<>0
union all
select dateadd(month,1,StartDate)-day(dateadd(month,1,StartDate))+1,
EndDate,
cost*day(EndDate)/7
from #t where datediff(month,StartDate,EndDate)<>0
order by StartDate
drop table #t
yigebendan 2005-01-29
  • 打赏
  • 举报
回复
mark
Softlee81307 2005-01-29
  • 打赏
  • 举报
回复
Create Table tk(StartDate DateTime,EndDate DateTime,Cost int)
insert Into tk
select '2005-01-23','2005-01-29',10 union all
select '2005-01-30','2005-02-05',14 union all
select '2005-02-06','2005-02-12',20
-----------------------------------------------------------------------------
-----------------以下是輸出語句---------------------------------------
select * from (
select * from tk where datediff(m,startdate,enddate)=0
union all
select startDate,dateadd(dd,-1,convert(varchar(7),EndDate,120)+'-01'),
(datediff(dd,startdate,dateadd(dd,-1,convert(varchar(7),EndDate,120)+'-01'))+1)*
(cost/datediff(dd,startDate,EndDate)) from tk where datediff(m,startdate,enddate)>0
union all
select convert(varchar(7),EndDate,120)+'-01',enddate,
(datediff(dd,convert(varchar(7),EndDate,120)+'-01',EndDate)+1)*
(cost/datediff(dd,startDate,EndDate))
from tk where datediff(m,startdate,enddate)>0 ) a order by startDate
---------------------結束------------------------------------
--------------結果-----------------------------------

StartDate EndDate Cost
2005-01-23 00:00:00.000 2005-01-29 00:00:00.000 10
2005-01-30 00:00:00.000 2005-01-31 00:00:00.000 4
2005-02-01 00:00:00.000 2005-02-05 00:00:00.000 10
2005-02-06 00:00:00.000 2005-02-12 00:00:00.000 20


Drop Table Tk ---刪除測試
Softlee81307 2005-01-29
  • 打赏
  • 举报
回复
Create Table tk(StartDate DateTime,EndDate DateTime,Cost int)
insert Into tk
select '2005-01-23','2005-01-29',10 union all
select '2005-01-30','2005-02-05',14 union all
select '2005-02-06','2005-02-12',20
-----------------------------------------------------------------------------
-----------------以下是輸出語句---------------------------------------
select * from (
select * from tk where datediff(m,startdate,enddate)=0
union all
select startDate,dateadd(dd,-1,convert(varchar(7),EndDate,120)+'-01'),
(datediff(dd,startdate,dateadd(dd,-1,convert(varchar(7),EndDate,120)+'-01'))+1)*
(cost/datediff(dd,startDate,EndDate)) from tk where datediff(m,startdate,enddate)>0
union all
select convert(varchar(7),EndDate,120)+'-01',enddate,
(datediff(dd,convert(varchar(7),EndDate,120)+'-01',EndDate)+1)*
(cost/datediff(dd,startDate,EndDate))
from tk where datediff(m,startdate,enddate)>0 ) a order by startDate
---------------------結束------------------------------------
--------------結果-----------------------------------

StartDate EndDate Cost
2005-01-23 00:00:00.000 2005-01-29 00:00:00.000 10
2005-02-06 00:00:00.000 2005-02-12 00:00:00.000 20
2005-01-30 00:00:00.000 2005-01-31 00:00:00.000 4
2005-02-01 00:00:00.000 2005-02-05 00:00:00.000 10

Drop Table Tk ---刪除測試
Softlee81307 2005-01-29
  • 打赏
  • 举报
回复
Create Table tk(StartDate DateTime,EndDate DateTime,Cost int)
insert Into tk
select '2005-01-23','2005-01-29',10 union all
select '2005-01-30','2005-02-05',14 union all
select '2005-02-06','2005-02-12',20
-----------------------------------------------------------------------------
-----------------以下是輸出語句---------------------------------------

select * from tk where datediff(m,startdate,enddate)=0
union all
select startDate,dateadd(dd,-1,convert(varchar(7),EndDate,120)+'-01'),
(datediff(dd,startdate,dateadd(dd,-1,convert(varchar(7),EndDate,120)+'-01'))+1)*
(cost/datediff(dd,startDate,EndDate)) from tk where datediff(m,startdate,enddate)>0
union all
select convert(varchar(7),EndDate,120)+'-01',enddate,
(datediff(dd,convert(varchar(7),EndDate,120)+'-01',EndDate)+1)*
(cost/datediff(dd,startDate,EndDate))
from tk where datediff(m,startdate,enddate)>0
---------------------結束------------------------------------
--------------結果-----------------------------------

StartDate EndDate Cost
2005-01-23 00:00:00.000 2005-01-29 00:00:00.000 10
2005-02-06 00:00:00.000 2005-02-12 00:00:00.000 20
2005-01-30 00:00:00.000 2005-01-31 00:00:00.000 4
2005-02-01 00:00:00.000 2005-02-05 00:00:00.000 10

Drop Table Tk ---刪除測試
txlicenhe 2005-01-29
  • 打赏
  • 举报
回复
看不懂。

xingfuniao 2005-01-29
  • 打赏
  • 举报
回复
感觉无规律可循....让高手来吧
sdhdy 2005-01-29
  • 打赏
  • 举报
回复
用游标+临时表实现

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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