/* Given the following database schema: */
CREATE TABLE sqlite_sequence(name,seq)

CREATE TABLE "essays"
(
    projectid         TEXT,
    teacher_acctid    TEXT,
    title             TEXT,
    short_description TEXT,
    need_statement    TEXT,
    essay             TEXT
)

CREATE TABLE "projects"
(
    projectid                              TEXT not null
        primary key,
    teacher_acctid                         TEXT,
    schoolid                               TEXT,
    school_ncesid                          TEXT,
    school_latitude                        REAL,
    school_longitude                       REAL,
    school_city                            TEXT,
    school_state                           TEXT,
    school_zip                             INTEGER,
    school_metro                           TEXT,
    school_district                        TEXT,
    school_county                          TEXT,
    school_charter                         TEXT,
    school_magnet                          TEXT,
    school_year_round                      TEXT,
    school_nlns                            TEXT,
    school_kipp                            TEXT,
    school_charter_ready_promise           TEXT,
    teacher_prefix                         TEXT,
    teacher_teach_for_america              TEXT,
    teacher_ny_teaching_fellow             TEXT,
    primary_focus_subject                  TEXT,
    primary_focus_area                     TEXT,
    secondary_focus_subject                TEXT,
    secondary_focus_area                   TEXT,
    resource_type                          TEXT,
    poverty_level                          TEXT,
    grade_level                            TEXT,
    fulfillment_labor_materials            REAL,
    total_price_excluding_optional_support REAL,
    total_price_including_optional_support REAL,
    students_reached                       INTEGER,
    eligible_double_your_impact_match      TEXT,
    eligible_almost_home_match             TEXT,
    date_posted                            DATE
)

CREATE TABLE donations
(
    donationid                               TEXT not null
            primary key,
    projectid                               TEXT,
    donor_acctid                             TEXT,
    donor_city                               TEXT,
    donor_state                              TEXT,
    donor_zip                                TEXT,
    is_teacher_acct                          TEXT,
    donation_timestamp                       DATETIME,
    donation_to_project                      REAL,
    donation_optional_support                REAL,
    donation_total                           REAL,
    dollar_amount                            TEXT,
    donation_included_optional_support       TEXT,
    payment_method                           TEXT,
    payment_included_acct_credit             TEXT,
    payment_included_campaign_gift_card      TEXT,
    payment_included_web_purchased_gift_card TEXT,
    payment_was_promo_matched                TEXT,
    via_giving_page                          TEXT,
    for_honoree                              TEXT,
    donation_message                         TEXT,
    foreign key (projectid) references projects(projectid)
)

CREATE TABLE resources
(
    resourceid            TEXT not null
            primary key,
    projectid             TEXT,
    vendorid              INTEGER,
    vendor_name           TEXT,
    project_resource_type TEXT,
    item_name             TEXT,
    item_number           TEXT,
    item_unit_price       REAL,
    item_quantity         INTEGER,
    foreign key (projectid) references projects(projectid)
)

/* Answer the following: What percentage of donations are given via a giving or campaign page? List the primary area of those donations. given via a giving or campaign page refers to via_giving_page = 't'; percentage refers to DIVIDE(count(case when via_giving_page = 't' then donationid else null end),count(donationid))*100; primary area of donation refers to primary_focus_area */
SELECT 
SELECT CAST(SUM(CASE WHEN T1.via_giving_page = 't' THEN 1 ELSE 0 END) AS REAL) * 100 / COUNT(donation_total), ( SELECT T2.primary_focus_area FROM donations AS T1 INNER JOIN projects AS T2 ON T1.projectid = T2.projectid WHERE T1.via_giving_page = 't' GROUP BY T2.primary_focus_area ORDER BY SUM(T1.donation_total) DESC LIMIT 1 ) result FROM donations AS T1 INNER JOIN projects AS T2 ON T1.projectid = T2.projectid