sql TRI历史数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql TRI历史数据相关的知识,希望对你有一定的参考价值。
--=================================================
-- Creator: Chris Kendrick
-- Created Date: 10/29/18
-- Modified Date: NA
-- Purpose: Map TRI Data to TL Shipments
-- Modified Reason: NA
--=================================================
/*
**************************************************************************************************************
DISCLAIMER
**************************************************************************************************************
+ DEPENDING ON HOW TO MAP TO EQUIPMENT YOU WILL NEED TO EDIT SCRIPT
+ BELOW EXAMPLE ASSUMES DRY VAN JUST
Advice:
+ if you are going to try and use this data, please contact Chris Kendrick in TLSolutions (he is more than happy to help)
+ there are multiple levels of TRI data granularity and particular use cases for each one - make sure you know what you want
+ Talk to Chris Kendrick - he just wants to make sure you dont accidentally use this information correctly
Pre-Reqs:
+ know what TRI is and understand how they structure their data
useful link: https://www.dat.com/blog/post/freight-rates-faq
+ know how to map TL loads to equipments
+ read acces to the TLPricing database in the lab
TRI Tables:
+ TLPricing.dbo.TRI_History - this is monthly snapshots of data
+ TLPricing.rfp.TRI_365 - this is past 365 (most users should NOT use this data)
+ there is another table of TRI data for benchmarking - talk to Megan Danczyk if you want to use this
*/
--=================================================================
--cte to grab basic load level stuff
--restrict to 51 loads created on 9.1.2018
--=================================================================
With CTE AS (
select
L.LoadGuid
,CreatedDate
,DATEFROMPARTS(YEAR(L.createdDate),MONTH(L.CreatedDate),1) as CleanCreatedDate --used in the join to TRI data tables
,O.City as oCity
,O.StateCode as oState
,O.Zip as oZip
,OM.TRI_Market as oMarket
,D.City as dCity
,D.StateCode as dState
,D.Zip as dZip
,DM.TRI_Market as dMarket
,L.Distance
,IIF( L.Distance <=50,'1-50',
IIF(L.Distance <=100,'51-100',
IIF(L.Distance <=150,'101-150',
IIF(L.Distance <=200,'151-200',
IIF(L.Distance <=250,'201-250',
'251+'))))) as MileageBucket
,'V' as Equipment
from EchoOptimizer.dbo.tblLoads as L
left join EchoOptimizer.dbo.tblAddress as O on L.OriginAddressId = O.AddressId
left join TLPricing.rfp.[3digit_analysis] as OM on LEFT(O.zip,3) = OM.[3digit]
left join EchoOptimizer.dbo.tblAddress as D on L.DestinationAddressId = D.AddressId
left join TLPricing.rfp.[3digit_analysis] as DM on LEFT(D.zip,3) = DM.[3digit]
where L.Mode = 'TL'
and L.[status] = 'Delivered'
and CAST(L.CreatedDate as date) = '9.1.2018'
)
select
cte.*
,cte.oMarket + ' - ' + cte.dMarket + ': ' + cte.MileageBucket as MMMB
,CAST(IIF(cte.mileageBucket = '251+', cte.Distance * H.Low_LineHaul_Rate,PC_Miler_Practical_Mileage*Low_LineHaul_Rate) as decimal(7,2))as TRI_Low_Flat_LH
,CAST(IIF(cte.mileageBucket = '251+', cte.Distance * H.Average_LineHaul_Rate,PC_Miler_Practical_Mileage*Average_LineHaul_Rate) as decimal(7,2)) as TRI_Avg_Flat_LH
,CAST(IIF(cte.mileageBucket = '251+', cte.Distance * H.High_LineHaul_Rate,PC_Miler_Practical_Mileage*High_LineHaul_Rate) as decimal(7,2)) as TRI_High_Flat_LH
,H.Reports as TRI_Monthly_Reports
,H.Companies as TRI_Monthly_Companies
from cte
left join TLPricing.dbo.TRI_History as H
ON cte.oMarket + ' - ' + cte.dMarket + ': ' + cte.MileageBucket = H.MarketToMarketMileageBucket
and cte.CleanCreatedDate = H.Dt
and cte.Equipment = H.Equipment
/*
TLPricing.dbo.TRI_history has 3 equipment codes:
V = Dry Van
R = Reefer
F = Flatbed
*/
;
以上是关于sql TRI历史数据的主要内容,如果未能解决你的问题,请参考以下文章
sql 语句多层嵌套查询 使用别名 字段无效,如何解决(有图)