Skip to content
Advertisement

How to pull the count of occurences from 2 SQL tables

I am using python on a SQlite3 DB i created. I have the DB created and currently just using command line to try and get the sql statement correct.

I have 2 tables.

When I setup table two, I added this statement in the creation of my messages table, but I have no clue what, if anything, it does:

What I am trying to do is return a list containing the name and message count during 2020. I have used this statement to get the TOTAL number of posts in 2020, and it works:

But I am struggling with figuring out if I should Join the tables, or if there is a way to pull just the info I need. The statement I want would look something like this:

Advertisement

Answer

One option uses a correlated subquery:

This query would take advantage of an index on messages(user_id, date). You could also join and aggregate. If you want to allow users that have no messages, a left join is a appropriate:

Note that it is more efficient to filter the date column against literal dates than applying a function on it (which precludes the use of an index).

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement