How could I write a get_categories_by_year() function?

I would do this with SQL query. Doing it in PHP will be a little bit slow, I guess.

This function should do the job:

function get_categories_from_year($year) {
    $sql = "select term.* 
        from {$wpdb->terms} term
            inner join {$wpdb->term_taxonomy} tax on (term.term_id = tax.term_id)
            inner join {$wpdb->term_relationships} relation on (tax.term_taxonomy_id = relation.term_taxonomy_id)
            inner join {$wpdb->posts} post on (relation.object_id = post.ID)
        where tax.taxonomy = 'category' and YEAR(post.post_date) = %d";
    return $wpdb->get_results( $wpdb->prepare($sql, $year) );
}

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)