Skip to content

Instantly share code, notes, and snippets.

@akhilesh26
Created August 8, 2019 14:24
Show Gist options
  • Save akhilesh26/67aa2aa42181390dfa079bed6f5253e9 to your computer and use it in GitHub Desktop.
Save akhilesh26/67aa2aa42181390dfa079bed6f5253e9 to your computer and use it in GitHub Desktop.
WITH RECURSIVE records AS (
	SELECT shipping_line_id, MIN(total_price)  min_price
	FROM rate_detailed_fcl_freight_charges
	where origin_port_id='58eb6135c0a35f29ced559c0' and destination_port_id='58ef81f3c0a35f0983b17969' and container_type='standard' and container_size='20' and commodity='general'
	GROUP BY shipping_line_id
)
SELECT
	r.shipping_line_id,
	r.min_price,
	frt.freight_forwarder_id,
	MAX(frt.date) DATE
FROM rate_detailed_fcl_freight_charges frt JOIN records r ON 
	frt.shipping_line_id = r.shipping_line_id 
	AND frt.total_price = r.min_price	
GROUP BY r.shipping_line_id, r.min_price, frt.freight_forwarder_id
LIMIT 50;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment