----这样的SQL怎么写?---

Sale_Boss 2004-09-15 04:37:41
表(kq)
userID userName tDate tTime nRemark
0001 Mike 2004-9-10 1900-1-1 8:43:00 1
0001 Mike 2004-9-10 1900-1-1 8:44:00 1
0002 Wang 2004-9-11 1900-1-1 8:45:00 2
....

给定一个时间段,例如 [2004-9-10,2004-9-15]
现在需要向数据库中插入一些数据,插入的数据必须是

(1)在给定的时间段内,每天只有一条记录的那些人(按UserID,tDate分组,Having count(*) =1的记录)
(2)插入新记录时间即为(1)中记录时间+1。

例:上面的的,0001因为在2004-9-10有2条,则不需增。
0002在2004-9-11好只有一条记录,则需要增加如下一条记录
0002 Wang 2004-9-11 1900-1-1 8:46:00 2

------------------------------------------------------------------------------
如果写成存储过程,以两个日期为参数该怎么写?
...全文
105 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hearyone 2004-09-15
  • 打赏
  • 举报
回复
insert into kq (userID ,userName ,tDate,tTime, nRemark)
select table1.userid,kq.userName,table1.tDate,dateadd(s,1,kq.tTime) ,kq.nRemark
from
(select userID ,tDate from kq
group by userID,tDate
where tdate between insert into kq (userID ,userName ,tDate,tTime, nRemark)
select table1.userid,kq.userName,table1.tDate,dateadd(s,1,kq.tTime) ,kq.nRemark
from
(select userID ,tDate from kq
group by userID,tDate
where tdate between @date1 and @date2 --参数
having count(*)=1)
table1
left join kq on table1.userid=kq.userid and table1.tdate=kq.tdate
having count(*)=1)
table1
left join kq on table1.userid=kq.userid and table1.tdate=kq.tdate
windy2008 2004-09-15
  • 打赏
  • 举报
回复
UP
Sale_Boss 2004-09-15
  • 打赏
  • 举报
回复
3kss
zjcxc 元老 2004-09-15
  • 打赏
  • 举报
回复
create proc p_insert
@开始时间 datetime,
@结束时间 datetime
as
insert kq(userID,userName,tDate,tTime,nRemark)
select a.userID,a.userName,dateadd(day,1,a.tDate),a.tTime,a.nRemark
from kq a,(
select UserID,tDate
from kq
where tDate between @开始时间 and @结束时间
group by UserID,tDate
having count(*)=1
)b where a.UserID=b.UserID and a.tDate=b.tDate
了缘 2004-09-15
  • 打赏
  • 举报
回复
insert into kq (userID ,userName ,tDate,tTime, nRemark)
select table1.userid,kq.userName,table1.tDate,dateadd(s,1,kq.tTime) ,kq.nRemark
from
(select userID ,tDate from kq
group by userID,tDate
where tdate between @date1 and @date2 --参数
having count(*)=1)
table1
left join kq on table1.userid=kq.userid and table1.tdate=kq.tdate

34,576

社区成员

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

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