The rabbis wanted to keep this book out of the canon because of so many of the verses that give a wrong impression. However, when they saw the final verse of Ecclesiastes, they relented. What does this verse say? "At the end of the matter, after all has been heard, you should fear the Lord and observe His commandments, for this is the totality of man."
In other words, since the book ends on an unambiguous note of piety, it is okay for the masses to read. Really? Does one verse at the end undo all of the problematic verses that precede it?
A commentary, the Ketav Sofer, elucidates one verse which gives us the key to understanding all of these verses. "What advantage does one gain from all of his work that he will labor at under the sun?" He asks our question, that there is certainly benefit for work! No, because at the end of the day, King Solomon tells us, the wise man and the fool, the rich man and the pauper, will meet the same end. After life is over, no one has any advantage.
That is what happens when one labors "under the sun." But if one labors for "over the sun," for the sake of Heaven, there is tremendous benefit. This is hinted at by the use of the future tense, "that he will labor under the sun." It is not commenting on the work he has already done, but his intentions for the future. If his intentions are to collect earthly toys, he will never see that advantage last. But if he labors for the sake of Heaven, for that place which is above the sun, he will most certainly have an advantage.
Rabbi Norman Lamm, the former president of Yeshiva University, expounds on this book beautifully. King Solomon, according to the Aramaic commentator Yonatan ben Uzziel, wrote this book with an element of prophecy. There is a profound difference between wisdom and prophecy. The former can indeed give happiness! Knowledge demonstrably improves our quality and effectiveness in life. These are key elements in happiness, and this cannot be dismissed. King Solomon himself pursued wisdom to the end of his days.
Prophecy, on the other hand, contains the seeds of depression. Why? Because King Solomon saw that everything he had built in his kingdom would be squandered and destroyed in future generations. In his son's time, the kingdom would split in two. Centuries after, the entire nation would be exiled and afflicted. And, yes, every accomplishment one makes in this world eventually disappears after we are gone. Who knows if our great-great-grandchildren will even know the most basic things about us, let alone our talents, our accomplishments, our loves, our dreams.
Wisdom looks at our present and immediate future, while prophecy looks way down the road. Wisdom can give us short-term optimism in happiness. Prophecy shows us how futile everything is. Or so it seems...
Then comes that final verse, that magical conclusion to Ecclesiastes, "At the end of the matter, after all has been heard, you should fear the Lord and observe His commandments, for this is the totality of man." This verse is not just putting a kashrut certificate on a questionable work! It is giving us the whole context! And that is: if everything you do in this world is sanctified by dedication to the fear of the Lord, it is of value and will last forever.
One's great-great-grandchildren may not know the details of their life, but God certainly does! And God is eternal. All of the verses in the book that seem to make light of important values such as wisdom, piety, labor, joy and love, do so when they are dedicated to worldly success, to happiness "under the sun." All of that is vanity indeed. But when they are dedicated to God, when they are sanctified to heaven, which is "above the sun," they become eternal.
As I first contemplated the depressing aspects of this book, I found myself feeling down. I love to get excited about a new project, and here, King Solomon, the wisest man who ever lived, is telling me to get depressed, not excited, because it's all vanity and worthlessness. And then I went to take a nap in my sukkah. It was there that I felt this answer to these questions.
The sukkah is the physical embodiment of the book of Ecclesiastes. Its walls are flimsy, its roof is porous. Nothing about it will last, except that it symbolizes the eternity of the Jewish people. And that is because it is dedicated to God. The Talmud tells us that God's Name is written into the very fiber of the sukkah. And then I cheered up, because I understood that by dedicating all of my exciting projects to fulfilling God's purpose in the world, happiness and love and wisdom and wealth have tremendous value.