Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.1 kB
1
Indexable
Never
    DROP TABLE IF EXISTS #dll
	SELECT 
		cll.agent_inn, 
		cll.agent_name, 
		vttr.client_inn, 
		cll.client_name, 
		vttr.region,
		cll.contract_name, 
		CAST(cll.contract_date_begin AS DATE) AS MIN_DOC_date, 
		vttr.MAX_DOC_date,
		vttr.transaction_date,
		vttr.total_monthly_litres
	INTO #dll
	FROM #vttr vttr 
	LEFT JOIN #cll cll ON cll.client_inn = vttr.client_inn and lower(cll.contract_name)=lower(vttr.contract_name)
	GROUP BY 
		cll.agent_inn, 
		cll.agent_name, 
		vttr.client_inn, 
		cll.client_name, 
		vttr.region,
		cll.contract_name, 
		cll.contract_date_begin, 
		vttr.MAX_DOC_date,
		vttr.transaction_date,
		vttr.total_monthly_litres;


    DROP TABLE IF EXISTS #dll_2
	SELECT 
		agent_inn, 
		agent_name, 
		client_inn, 
		client_name, 
		region,
		contract_name,
		SUM(total_monthly_litres) AS total_litres,
		MIN(MIN_DOC_date) AS MIN_DOC_date, 
		MAX(MAX_DOC_date) AS MAX_DOC_date
	INTO #dll_2
	FROM #dll
	GROUP BY 
		agent_inn, 
		agent_name, 
		client_inn, 
		client_name, 
		region,
		contract_name;
Leave a Comment