sql 循环表,获取表中某一字段作为查询语句的条件之一

DECLARE @timeSpan TABLE
( id int,
intime DATETIME ,
outtime DATETIME )
DECLARE @distance TABLE
( x float, y float )


INSERT INTO @timeSpan
( id,intime,outtime)

SELECT ROW_NUMBER() over (order by att.inTime) 'rowindex',att.inTime,att.outTime
FROM attendance att
inner join Vehicle as veh
on att.employeeid=veh.ID
WHERE intime BETWEEN '2014/11/17 0:00:00' AND '2014/11/17 10:14:00' and veh.vehicleDepartmentId=3 and veh.vehicleType=1 and veh.id=-297 and att.occday=20141117
order by att.inTime

declare @count int
declare @currentCount int
declare @intime datetime
declare @outtime datetime
select @count = COUNT(*) from @timeSpan
set @currentCount =0
while @count >@currentCount
begin
set @currentCount =@currentCount+1
select @intime =intime, @outtime =outtime from @timeSpan where @currentCount =id
INSERT INTO @distance
( x,y)
select x,y from (select wp.*,wv.x,wv.y FROM (
SELECT workSiteId,employeeId FROM workSitePass where employeeid='-297' and sTime BETWEEN @intime and @outtime
) AS wp INNER JOIN workSite as wv
on wp.workSiteId=wv.number
inner join Vehicle as ev on wp.employeeId=ev.id
left join deviceLocMap as dlm on dlm.number=wv.number ) as t1


end

select * from @distance

转载于:https://www.cnblogs.com/cztZone/p/4103180.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值