SELECT  DATEDIFF(Day,ISNULL(T3.F_BGP_MAINTDATE,T3.FINSTOCKDATE),GETDATE())                                                   AS DIFFDay
       ,T2.F_BGP_DAY
       ,ISNULL(T7.F_BGP_DAY,- 1)                                                                                             AS FNearDAY
       ,T2.F_BGP_DAY - DATEDIFF(Day,ISNULL(T3.F_BGP_MAINTDATE,GETDATE()),GETDATE())                                          AS FEnableDay
       ,T2.FNUMBER
       ,T0.FSTOCKORGID
       ,T1.FMATERIALID
       ,T1.FNUMBER                                                                                                           AS FMaterialNum
       ,T1.F_BGP_ISCOLDCHAIN
       ,T1.F_BGP_ISSPECIAL
       ,T1.F_BGP_BIGCLASS
       ,T6.FNAME                                                                                                             AS F_BGP_BIGCLASS_Name
       ,CONVERT(varchar(10),T1.F_BGP_FIRSTDATE,21)                                                                           AS F_BGP_FirstDate
       ,(CASE WHEN T1.F_BGP_FirstDate IS NULL THEN 0 WHEN DATEDIFF(Year,T1.F_BGP_FirstDate,GetDate()) > 1 THEN 0 ELSE 1 END) AS DIFFYear
       ,(CASE WHEN T1.F_BGP_FirstDate IS NULL THEN 0 ELSE DATEDIFF(Year,T1.F_BGP_FirstDate,GetDate()) END) AS DIFFYear
       ,T1.F_BGP_MAINTAINCLASS
       ,'0'                                                                                                                  AS F_BGP_MainteState
       ,T0.FSTOCKUNITID
       ,T0.FBASEUNITID
       ,T0.FQTY
       ,T0.FBaseQty
       ,CONVERT(varchar( 10),isnull(T0.FPRODUCEDATE,T3.FPRODUCEDATE),21)                                                     AS FPRODUCEDATE
       ,CONVERT(varchar(10),isnull(T0.FEXPIRYDATE,T3.FEXPIRYDATE),21)                                                        AS FExpiryDate
       ,T0.FLOT
       ,T0.FSTOCKID
       ,T0.FSTOCKLOCID
       ,T5.FEXPUNIT
       ,T5.FEXPPERIOD
       ,CASE WHEN T5.FEXPUNIT = 'M' THEN T5.FExpPeriod
             WHEN T5.FEXPUNIT = 'Y' THEN T5.FExpPeriod * 12
             WHEN T5.FEXPUNIT = 'D' THEN T5.FExpPeriod / 30  ELSE 0 END                                                      AS FExpMonth
       ,ISNULL(T3.F_BGP_MAINTDATE + CAST(T2.F_BGP_DAY AS int),GETDATE())                                                     AS F_BGP_SHOULDMAINTEDATE
       ,T3.FINSTOCKDATE
       ,DATEDIFF(Month,T3.FINSTOCKDATE,GETDATE())                                                                           AS FInStockMonth
       ,T0.FOWNERTYPEID
       ,T0.FOWNERID
       ,T0.FKEEPERTYPEID
       ,T0.FKEEPERID
       ,T0.FSTOCKSTATUSID
       ,FAuxPropId
       ,FSecQty
       ,FSecUnitId
       ,t10.FInStockDate FIRFInStockDate
       ,F_BGP_FirstStatus
       ,(Case WHEN T10.FInStockDate is null THEN 0 WHEN DateDiff(Year,T10.FInStockDate,GetDate()) > 1 THEN 0 else 1 end) FDIFFYear
       ,F_BGP_INVENTORYID
FROM (
		SELECT  fid F_BGP_INVENTORYID
			   ,FSTOCKORGID
			   ,FMATERIALID
			   ,FBASEUNITID
			   ,FSTOCKUNITID
			   ,FLOT
			   ,FSTOCKID
			   ,FSTOCKLOCID
			   ,FPRODUCEDATE
			   ,FEXPIRYDATE
			   ,FOWNERTYPEID
			   ,FOWNERID
			   ,FKEEPERTYPEID
			   ,FKEEPERID
			   ,FSTOCKSTATUSID
			   ,SUM(FQTY)     AS FQTY
			   ,SUM(FBASEQTY) AS FBaseQty
			   ,FAuxPropId
			   ,SUM(FSecQty)as FSecQty
			   ,FSecUnitId
		FROM dbo.T_STK_INVENTORY
		WHERE not exists(
		SELECT  distinct F_BGP_INVENTORYID
		FROM T_BGP_MaintePlanB mb where F_BGP_BaseQty > F_BGP_BaseMaintQty AND F_BGP_INVENTORYID = T_STK_INVENTORY.fid )
		GROUP BY  FSTOCKORGID
				 ,FMATERIALID
				 ,FBASEUNITID
				 ,FSTOCKUNITID
				 ,FLOT
				 ,FSTOCKID
				 ,FSTOCKLOCID
				 ,FPRODUCEDATE
				 ,FEXPIRYDATE
				 ,FOWNERTYPEID
				 ,FOWNERID
				 ,FKEEPERTYPEID
				 ,FKEEPERID
				 ,FSTOCKSTATUSID
				 ,FAuxPropId
				 ,FSecUnitId
				 ,fid
	) AS T0
	INNER JOIN dbo.T_BD_MATERIAL AS T1 ON T0.FMATERIALID = T1.FMASTERID AND T0.FSTOCKORGID = T1.FUSEORGID
	LEFT JOIN
	(
		SELECT  FMATERIALID
			   ,MIN(FInStockDate)FInStockDate
		FROM T_BD_LotMaster
		GROUP BY  FMATERIALID
	)T10 ON T1.FMATERIALID = T10.FMaterialID
	INNER JOIN dbo.T_BGP_MaintainType AS T2 ON T1.F_BGP_MAINTAINCLASS = T2.FID
	INNER JOIN dbo.T_BD_LOTMASTER AS T3 ON T0.FLOT = T3.FLOTID
	INNER JOIN dbo.T_BD_MATERIALSTOCK AS T5 ON T0.FMATERIALID = T5.FMATERIALID
	LEFT OUTER JOIN dbo.T_BGP_BigClass_L AS T6 ON T1.F_BGP_BIGCLASS = T6.FID
	LEFT OUTER JOIN dbo.T_BGP_EffectType AS T7 ON T1.F_BGP_EFFECTTYPE = T7.FID
WHERE (T0.FBaseQty > 0) 