Views

Learn how to use views in PostgreSQL.

We'll cover the following...

Views allow integrating server-side computations in the definition of a relation. The computing still happens dynamically at query time and is made transparent to the client. When using a view, there’s no problem with cache invalidation because nothing gets cached away.

Press + to interact
create view tweet.message_with_counters
as
select messageid,
message.userid,
message.datetime,
message.message,
count(*) filter(where action = 'rt')
- count(*) filter(where action = 'de-rt')
as rts,
count(*) filter(where action = 'fav')
- count(*) filter(where action = 'de-fav')
as favs,
message.location,
message.lang,
message.url
from tweet.activity
join tweet.message using(messageid)
group by message.messageid, activity.messageid;

Note: This query is already executed. You can verify it by using the next query in the “ ...