-----------------------------------------------------------------------------------------'
-- Aggiunta vista VGT_DDT											 
-----------------------------------------------------------------------------------------'
	
	IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'dbo.VGT_DDT'))
		DROP VIEW dbo.VGT_DDT
	GO

	SET ANSI_NULLS ON
	GO

	SET QUOTED_IDENTIFIER OFF
	GO
		
	CREATE VIEW [dbo].[VGT_DDT]
	AS 
	SELECT 
		*, (SELECT TOP 1 GT02_CODICEGE 
			FROM GT02_CLIENTIFORNITORI
			WHERE GT02_ID = [GT03_ID_GT02]) AS [GT03_CLIFOR_GE]
			, (SELECT TOP 1 GT02_IND_TIPO_CLIENTE_FORNITORE 
			FROM GT02_CLIENTIFORNITORI
			WHERE GT02_ID = [GT03_ID_GT02]) AS [GT03_INDTIPOCF_GE]				 
			, (select top 1 GT03_rif.GT03_NUMREG_GE 
			from GT03_DOCTESTATA GT03_rif
			where GT03_DOCTESTATA.GT03_IDRIF_GT03 = GT03_rif.GT03_ID) as gt04_numreg_rif
			, (SELECT TOP 1 GT09_CODICEGE 
			FROM GT09_AGENTI
			WHERE GT09_ID = [GT03_ID_GT09]) AS [GT03_AGENTE]
			, (SELECT TOP 1 GT13_CODICEGE  
			FROM GT13_DESTINATARI
			WHERE GT13_ID = [GT03_ID_GT13]) AS [GT03_DESTIN]
			, (SELECT TOP 1 GT11_IDGE 
			FROM GT11_CONTATTI 
			WHERE GT11_ID = [GT03_ID_GT11]) AS [GT03_CONTATTO]		
	FROM GT03_DOCTESTATA 
	INNER JOIN GT04_DOCCORPO 
		ON GT04_ID_GT03 = GT03_ID
	WHERE GT03_SYNCGE = 1
	AND GT03_TIPODOCTU = 5
GO


