求教一个----------------关于该交房租查询的SQL

cowbobe 2014-06-30 03:58:00
没分了,大家帮看看也行,谢了.

是这样,我想做一个SQL出来查询应该交房租的SQL语句,在SQL2000下.

比如,当cowbo的enddate是每月01/05,那他就要在每月这天前交房租.

表Contract是合同表(住户名,合同开始,结束,合同号),:

ID clientName startdate enddate ContractID
----------------------------------------------------------------------------------------------------------
1 cowbo 2014-01-05 2015-01-05 合同号1021210
2 xiu 2014-03-15 2015-03-15 合同号1021211


表Pay是付房租的(发生日期,合同号,金额)

ID indate ContractID cost
---------------------------------------------------------------------------



我想做一个SQL语句查询分别查出:
1.在每个月不同的人在enddate之前5天的未交房租记录.(也就是该交房的住户)
2.查出不同的人过了每月该交房租但未交的记录?(逾期记录)


...全文
138 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
唐诗三百首 2014-06-30
  • 打赏
  • 举报
回复

-- 1.在每个月不同的人在enddate之前5天的未交房租记录.(也就是该交房的住户)
select * 
 from [Contract] a
 where not exists
 (select 1 
  from [Pay] b 
  where b.ContractID=a.ContractID 
  and left(convert(varchar,b.indate,112),6)=left(convert(varchar,getdate(),112),6))
 and convert(varchar,getdate(),112)>=
     convert(varchar,dateadd(d,-5,left(convert(varchar,getdate(),23),8)+right(convert(varchar,a.enddate,112),2)),112)

-- 2.查出不同的人过了每月该交房租但未交的记录?(逾期记录)
select * 
 from [Contract] a
 where not exists
 (select 1 
  from [Pay] b 
  where b.ContractID=a.ContractID 
  and left(convert(varchar,b.indate,112),6)=left(convert(varchar,getdate(),112),6))
 and convert(varchar,getdate(),112)>=
     convert(varchar,left(convert(varchar,getdate(),23),8)+right(convert(varchar,a.enddate,112),2),112)

34,594

社区成员

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

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