First, do NOT use string concatenation to build SQL queries. You open yourself up to SQL Injection attacks by doing that.
Second, you're storing dates as strings. That's a huge problem because it's not culturally sensitive and sorting of dates by string value does not match the sorting of dates by actual date value.
Lastly, the casing you're using is hurting my eyes, making it difficult to read. SQL keywords are normally capitalized while column names and other identifiers are not.
Also, I don't believe you can create column aliases with a dotted notation. I'm taking about the AS clauses you have in that statement. Table aliases should be short so they don't drown out your column names thereby making your SQL easier to read.
select MAINFILE.facno,max(fdate) as MAINTRAN.fdate
should be
SELECT mf.facno, MAX(mf.fdate) AS fdate
Try your query in an SQL query tool before trying to do it in your code. You'll find it's quicker and easier to debug in the tool than it is in your code.