SQL Looping table, one of the criteria for a field in a table as a query statement

Source: Internet
Author: User

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 an D 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 [Email protected]+1
Select @intime =intime, @outtime =outtime from @timeSpan where @currentCount =id
INSERT into @distance
(x, Y)
Select X, y from (the 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

SQL Looping table, one of the criteria for a field in a table as a query statement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.