﻿CREATE VIEW V_DB_CONTRACT AS
SELECT t1.FID,t1.FName,t1.FNumber,t1.FDOCUMENTSTATUS,t1.FFORBIDSTATUS
	,ISNULL(t1.预付,0) AS 'FPlanRatePrepay'
	,ISNULL(t1.预付,0) * t1.FCONTRACTAMOUNT AS 'FPlanPrepay'
	,ISNULL(t1.发货,0) AS 'FPlanRateDeliver'
	,ISNULL(t1.发货,0) * t1.FCONTRACTAMOUNT AS 'FPlanDeliver'
	,ISNULL(t1.到货,0) AS 'FPlanRateArrive'
	,ISNULL(t1.到货,0) * t1.FCONTRACTAMOUNT AS 'FPlanArrive'
	,ISNULL(t1.验收,0) AS 'FPlanRateCheck'
	,ISNULL(t1.验收,0) * t1.FCONTRACTAMOUNT AS 'FPlanCheck'
	,ISNULL(t1.质保,0) AS 'FPlanRateQuality'
	,ISNULL(t1.质保,0) * t1.FCONTRACTAMOUNT AS 'FPlanQuality'
FROM(
	SELECT
		t0.FID,t0.FBILLNO AS 'FNumber'
		,t0.FNAME as FName
		,t0.FDOCUMENTSTATUS
		,'A' AS 'FFORBIDSTATUS'
		,t0p.F_CYG_TEXT
		,t0p.FRECADVANCERATE
		,t0f.FCONTRACTAMOUNT
	FROM T_CRM_CONTRACT t0
		INNER JOIN T_CRM_CONTRACTFIN t0f on t0f.FID = t0.FID
		INNER JOIN T_CRM_CONTRACTPLAN t0p on t0.FID = t0p.FID  
	WHERE t0.FDOCUMENTSTATUS = 'C'
 ) t0  
 PIVOT   
 (  
  SUM(t0.FRECADVANCERATE)  
  FOR   
  t0.f_CYG_TEXT IN ([预付],[发货],[到货],[验收],[质保])  
 ) AS t1