<?php

$SQL = "create view view_permits as

          select permits.*,
                 date_format(permit_entry_date,  '%c/%e/%Y')          as permit_entry_date_verbose,
                 date_format(permit_permit_date, '%c/%e/%Y')          as permit_permit_date_verbose,
                 date_format(permit_created,     '%c/%e/%Y %l:%i %p') as permit_created_verbose,
                 date_format(permit_changed,     '%c/%e/%Y %l:%i %p') as permit_changed_verbose,

                 IF(
                    permit_phone IS NULL OR permit_phone = '',
                    'Not Provided',
                        CONCAT('(', SUBSTRING(permit_phone, 1, 3), ') ',
                                    SUBSTRING(permit_phone, 4, 3), '-',
                                    SUBSTRING(permit_phone, 7, 4)
                 ))                                                   as permit_phone_verbose,

                 concat('$', format(permit_value, 2))                 as permit_value_verbose,
                 format(permit_size, 0)                               as permit_size_verbose,

                 trends.prev_ytd_permits                              as previous_ytd_permits,
                 trends.ytd_permits                                   as ytd_permits,
                 trends.permits_trend,

                 trends.prev_ytd_value                                as previous_ytd_value,
                 trends.ytd_value,

                 counties.county_name                                 as county_name_verbose

            from permits

            join view_company_permit_trends as trends
              on permits.permit_company = trends.permit_company

            join counties
              on permits.permit_county = counties.county_serial";
?>