IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[V_CHQ_LANC_CPL_STATUS_ANALISE]'))
DROP VIEW [dbo].[V_CHQ_LANC_CPL_STATUS_ANALISE]
GO
--SELECT * FROM V_CHQ_LANC_CPL_STATUS_ANALISE WHERE STS_CHQ <> STS_CHQ_REAL 
CREATE VIEW V_CHQ_LANC_CPL_STATUS_ANALISE AS
SELECT 
	CD_EMP,
	CD_CHQ,
	STS_CHQ,
	ULTIMA_SITUACAO,
	CASE WHEN DEPO>0 THEN
		CASE WHEN DEVOLVIDO > 0 THEN
			CASE WHEN REDEP>0 THEN
				CASE WHEN COBRANCA>0 THEN
					CASE WHEN RECUPERADO>0 THEN
						7
					ELSE
						1	
					END
				ELSE
					9
				END
			ELSE
				CASE WHEN COBRANCA>0 THEN
					CASE WHEN RECUPERADO>0 THEN
						7
					ELSE
						1	
					END
				ELSE
					3
				END
			END
		ELSE
			6
		END
	ELSE
		CASE WHEN DEVOLVIDO > 0 THEN
			CASE WHEN COBRANCA>0 THEN
				CASE WHEN RECUPERADO>0 THEN
					7
				ELSE
					1	
				END
			ELSE
				3
			END
		ELSE
			CASE WHEN COBRANCA>0 THEN
				CASE WHEN RECUPERADO>0 THEN
					7
				ELSE
					1	
				END
			ELSE
				0
			END			
		END
	END AS STS_CHQ_REAL
 FROM	
	V_CHQ_LANC_CPL_SITUACAO_STATUS