Chris.Kendrick of TL Solutions
2/20/2019 - 10:49 PM

Documentation - TRI/DAT/Transcore Data Guide

/*
Created Date: 2.20.2019
Created By: Chris Kendrick
Keywords: TRI, DAT, Transcore, Market, Spot, Shipper Contract
Description: this teaches you the basics of how TRI structures their data, what kind of TRI data echo stores in our DB, and how to get at the data


**************************************************************************************************************
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 Data we have:
   30 day Spot - Dry Van
   30 day spot - Reefer
   30 day shipper contract - Dry Van
   30 day shipper contract - Reefer
   30 day shipper contract - Flatbed
   365 day Spot - Dry Van
   365 day spot - Reefer
   
TRI Tables: 
 + TLPricing.dbo.TRI_history -- 30day spot
 + TLPricing.dbo.TRI_hisory_ShipperContract -- 30day contract
 + TLPricing.rfp.TRI_365 -- this is past 365 (most users outside TLSolutions will NOT use this data)
 >>> ALSO - Megan Danczyk has a benchmarking table of TRI on shorter timeframes - just know it exists, but we dont use it ever


 Data granularity - this is critical to returning the correct type of data when interacting with these tables
    Timeframe: 
        7 day
        15 day
        30 day
        90 day
        365 day
    Geographic Expansion & code: 
        + 3digit - 10
        + market - 20
        + xmarket - 30
        + region - 40
        + national - 50
    MileageBand: 
        '1-50' 
        '51-100' 
        '101-150' 
        '151-200' 
        '201-250' 
        '251+'
    Equipment & TLS Name: 
        'V' - 'Dry Van'
        'R' - 'Reefer'
        'F' - 'Flatbed' (mainly exists in the shipper contract table)
    Rate Type: 
        Spot - cost of loads seviced on the spot board
            >>> TLSolutions uses this for practically everything
        Contract - contracted rates with carriers
            >>> Megan Danzyck has some reports for MT that use this data
        

*/
 
 
--=================================================================
-- Example
--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 --faking it on a Dry Van in this example, depending on equipmet type required you will need to add that logic in
	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 ---MMMB granularity
	and cte.CleanCreatedDate = H.Dt ---date granularity
	and cte.Equipment = H.Equipment ---equipment granularity
		/* 
		TLPricing.dbo.TRI_history has 3 equipment codes - YOU WILL NEED ADD ANOTHER LAYER ONTO THE JOIN TO GET AT THIS
			V = Dry Van
			R = Reefer
			F = Flatbed
		*/