Untitled

mail@pastecode.io avatar
unknown
plain_text
20 days ago
1.6 kB
1
Indexable
Never
DROP TABLE IF EXISTS #KP_CommonClientsOkved; -- подтягиваем оквэды к таблице с клиентами, которые есть и у нас, и у дилеров
SELECT ccl.agent_inn, ccl.client_inn, ccl.client_name, okved.[ИНДЕКС]
INTO #KP_CommonClientsOkved
FROM #CommonClients ccl
LEFT JOIN [SANDBOX_DB_TEAM_21].[dbo].[Med_Okved] okved ON ccl.client_inn=okved.[ИНН]
GROUP BY ccl.client_inn, ccl.client_name, ccl.agent_inn, okved.[ИНДЕКС]

SELECT * FROM #KP_CommonClientsOkved

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	DROP TABLE IF EXISTS #vttr
	SELECT 
		t.client_inn, 
		t.agent_inn, --upd: t.agent_inn добавил
		t.contract_name, 
		t.region, 
		t.MAX_DOC_date,
		t.hash_key
	INTO #vttr
	FROM #vtt t
	GROUP BY  t.client_inn, t.agent_inn, t.contract_name, t.region, t.MAX_DOC_date, t.hash_key

SELECT * FROM #vttr

	 DROP TABLE IF EXISTS #reg --существующие клиенты наши
	 SELECT s.KONTRAGENT_INN, s.KONTRAGENT_NAIMENOVANIE, s.DOGOVORKONTRAGENTA_DATA_DOGOVORA, s.DOGOVORKONTRAGENTA_NOMER_DOGOVORA, r.c_description AS OTDELENIE, MAX_DOC_DATE_KK
	 INTO #reg
	 FROM #wwe s
	 LEFT JOIN  [asku_rp_kk_cdd].[cdd].[counterparty_contract] cc --контракт
	 ON s.DOGOVORKONTRAGENTA_ID_ASKU = cc.id_rref 
	 LEFT JOIN  [asku_kp_cdd].[cdd].[organization_department] r --регион объекта управления
	 ON cc.organization_department_rref=r.id_rref
	
	 SELECT TOP 5 * FROM #reg
Leave a Comment