SELECT * FROM (
	SELECT t0.FBILLNO fbillno, t0.FBILLTYPEID fbilltypeid, t0.FDATE fdate
		, t0.FDOCUMENTSTATUS fdocumentstatus, t0.FFOREORGID fforeorgid_id
		, t1.FCUSTID fcustid_id, t1.FMATERIALID fmaterialid_id, t1.FUNITID funitid_id
		, t1.FQTY fqty, t1.FSTARTDATE fstartdate, t1.FENDDATE fenddate, t1.FMTONO fmtono
		, t1.FRESERVETYPE freservetype, t1.FPRIORITY fpriority, t1.FWRITEOFFQTY fwriteoffqty
		, t1.FORDERQTY forderqty, t1.FSELPRDQTY fselprdqty, t1_L.FDESCRIPTION fremark
		, t0.FCREATORID fcreatorid_id, t0.FCREATEDATE fcreatedate, t0.FAPPROVERID fapproverid_id
		, t0.FAPPROVEDATE fapprovedate, t0.FCANCELSTATUS fcancelstatus, t0.FFOREORGID fforeorgid
		, t0.FFORMID fformid, t0.FID fid, t1.FENTRYID t1_fentryid, t1.FSeq t1_fseq
		, ROW_NUMBER() OVER( ORDER BY t0.FCREATEDATE DESC, t0.FBILLNO DESC, t1.FSeq ASC) fidentityid 
	FROM T_PLN_FORECAST t0 
		LEFT OUTER JOIN T_PLN_FORECASTENTRY t1 ON t0.FID = t1.FID 
		LEFT OUTER JOIN T_PLN_FORECASTENTRY_L t1_L ON (t1.FENTRYID = t1_L.FENTRYID AND t1_L.FLocaleId = 2052) 
	WHERE ((((t1.FSTOCKORGID = 1 OR (t1.FSTOCKORGID = 0 AND t0.FFOREORGID IN (0, 1))) 
		AND (t1.FBFLOWID = '' OR t1.FBFLOWID = ' ' OR (t1.FBFLOWID IS NULL))) 
		AND ((t0.FDOCUMENTSTATUS = 'C' AND t1.FPRODUCTTYPE = '1') 
		AND ( Exists (
			SELECT 1 FROM T_PLN_FORECAST et0
				INNER JOIN T_PLN_FORECASTENTRY et0e on et0.FID = et0e.FID
				INNER JOIN T_PRD_MOENTRY et1e on et1e.FSALEORDERENTRYSEQ = et0e.FSEQ AND et1e.FSALEORDERNO = et0.FBILLNO
			WHERE et0e.FPARENTROWID = t1.FROWID
			))
		--AND (t1.FENTRYID != 0)
		)) AND t0.FFORMID = 'PLN_FORECAST')) tlist 
WHERE ((fidentityid >= 1) AND (fidentityid <= 200))