0

My struggle is not with creating a table, I can create a table. The problem is to populate columns based off of calculations of other tables.

I have looked at How to create all tables defined in models using peewee and this is not helping me do summations and count etc..

I have a hypothetical database (database.db) and created these two tables:

Table 1 (from class User)

id name
1  Jamie
2  Sam
3  Mary

Table 2 (from class Sessions)

id SessionId
1  4121
1  4333
1  4333
3  5432

I simply want to create a new table using peewee:

id  name   sessionCount TopSession # <- (Session that appears most for the given user)
1   Jamie  3            4333
2   Sam    0            NaN
3   Mary   1            5432
4 ...

Each entry in Table1 and Table2 was created using User.create(...) or Sessions.create(...)

The new table should look at the data that is in the database.db (ie Table1 and Table2) and perform the calculations.

This would be simple in Pandas, but I cant seem to find a query that can do this. Please help

Llewellyn Hattingh
  • 306
  • 1
  • 5
  • 16

1 Answers1

0

I found it...

query = Sessions.select(fn.COUNT(Sessions.id)).where(Sessions.id==1)
count = query.scalar()
print(count)
>>> 3

# Or:
query = Sessions.select().where(Sessions.id == 1).count()
3

For anyone out there : )

coleifer
  • 24,887
  • 6
  • 60
  • 75
Llewellyn Hattingh
  • 306
  • 1
  • 5
  • 16