SELECT lo.areasymbol, lo.areaname, m.musym, m.mukey, m.muname, mo.areaovacres,
c.compname, c.comppct_r, coecoclass.ecoclassname, coecoclass.ecoclassid
INTO #MLRAECO
FROM legend
INNER JOIN laoverlap AS lo ON legend.lkey=lo.lkey AND legend.areasymbol<>'US'
INNER JOIN muaoverlap AS mo ON mo.lareaovkey = lo.lareaovkey AND lo.areatypename='mlra'
INNER JOIN mapunit AS m ON m.mukey = mo.mukey
INNER JOIN component AS c ON m.mukey=c.mukey AND compkind = 'series' AND
c.cokey = (SELECT TOP 1 component.cokey FROM component WHERE component.mukey=m.mukey ORDER BY component.comppct_r DESC)
LEFT OUTER JOIN coecoclass ON c.cokey = coecoclass.cokey AND ecoclasstypename LIKE 'NRCS%'
WHERE legend.areasymbol LIKE
'MO017'
ORDER BY lo.areasymbol, m.muname
SELECT areasymbol AS Area_symbol, areaname AS Area_name, muname AS Map_unit_name, SUM(areaovacres) AS MLRA_acres,
compname AS Component_name, ecoclassid AS Ecosite_ID, ecoclassname AS Ecosite_name
FROM #MLRAECO
GROUP BY areasymbol, areaname, muname, compname, ecoclassid, ecoclassname
ORDER BY areasymbol, muname
DROP TABLE #MLRAECO