def lulc_area_sql(grid_name, lulc_name):
    """
    Returns a sql statement that intersects a grid with the LULC table and
    sums the areas

    Args:
        grid_name (string): the PostGIS table name for the grid
        lulc_name (string): the PostGIS table name for the LULC table

    Returns:
        sql (string): a sql statement
    """

    sql = ("SELECT grid.i, "
           "grid.j, "
           "SUM(CASE WHEN pixel = 1 "
           "THEN ST_Area(ST_Intersection(grid.way, lulc.way)) "
           "ELSE 0 END) AS Agriculture, "
           "SUM(CASE WHEN pixel = 2 "
           "THEN ST_Area(ST_Intersection(grid.way, lulc.way)) "
           "ELSE 0 END) AS Bare, "
           "SUM(CASE WHEN pixel = 3 "
           "THEN ST_Area(ST_Intersection(grid.way, lulc.way)) "
           "ELSE 0 END) AS Forest, "
           "SUM(CASE WHEN pixel = 4 "
           "THEN ST_Area(ST_Intersection(grid.way, lulc.way)) "
           "ELSE 0 END) AS Grassland, "
           "SUM(CASE WHEN pixel = 5 "
           "THEN ST_Area(ST_Intersection(grid.way, lulc.way)) "
           "ELSE 0 END) AS Shrubland, "
           "SUM(CASE WHEN pixel = 6 "
           "THEN ST_Area(ST_Intersection(grid.way, lulc.way)) "
           "ELSE 0 END) AS Frozen, "
           "SUM(CASE WHEN pixel = 7 "
           "THEN ST_Area(ST_Intersection(grid.way, lulc.way)) "
           "ELSE 0 END) AS Urban, "
           "SUM(CASE WHEN pixel = 8 "
           "THEN ST_Area(ST_Intersection(grid.way, lulc.way)) "
           "ELSE 0 END) AS Water, "
           "grid.way FROM %s AS grid, "
           "%s AS lulc "
           "WHERE ST_Intersects(grid.way, lulc.way) "
           "GROUP BY grid.i, grid.j, grid.way ORDER BY grid.j, grid.i ASC")

    sql = sql % (grid_name, lulc_name)

    return sql