Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

CREATING A VIEW

$
0
0

Hi All,

I have the following query which I received helps from our experts here in the forum.  I now want to create a view from the existing query:

DECLARE @temp table
(
	CompanyID varchar(10),	
	CompanyName varchar(25),
	Route varchar(10),
	Vehicle int, 
	WorkDate datetime, 
	StartOdometer int,
	EndOdometer int
)

INSERT INTO @temp
(
	CompanyID,
	CompanyName,
	Route,
	Vehicle, 
	WorkDate, 
	StartOdometer,
	EndOdometer
 )

SELECT 
	c.CompanyID,
	c.CompanyName,
	dre.Route,
	dre.Vehicle,
	dre.WorkDate, 
	drev.StartOdometer,
	drev.EndOdometer

FROM DailyRouteEntry dre 

JOIN	dbo.DailyRouteEntryVehicle drev (NOLOCK) ON drev.DailyRouteEntryUID = dre.DailyRouteEntryUID
JOIN	dbo.Vehicle v (NOLOCK) ON v.Vehicle = drev.Vehicle
JOIN	dbo.Route r (NOLOCK) ON r.Route = dre.Route
JOIN	dbo.Company c (NOLOCK) ON c.CompanyID = r.CompanyID

WHERE dre.WorkDate BETWEEN '07-01-2013' AND '07-30-2013' and r.LineOfBusiness IN ('C', 'Commercial', 'Front Load', 'FrontLoad')

;

with cte as

( 

SELECT *, row_number() over (partition by Vehicle order by workdate, vehicle)  rn 
 

FROM @temp

)

SELECT

	a.CompanyID,
	a.CompanyName,	
	a.Vehicle, 
	a.Route,
	a.WorkDate,
	a.StartOdometer,
	a.EndOdometer,
	b.EndOdometer PreviousEndOdometer,
	a.rn Rownumber

 FROM cte a full join cte b on a.rn - 1 = b.rn  and  a.Vehicle=b.Vehicle  where a.[vehicle] is not null

Can I create a view from above query?

Thank you again for the help!


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>