Skip to content

SQLAlchemy/SQL: Filter table, then join to query

I need to filter down some results before joining them. I have three models: ModelA, ModelB, and ModelC. ModelA and ModelB both have a relationship to ModelC and they both have an organization_id, …

If statement with select in Postgresql

I’ve having trouble to do a if with select, e.g: In SQL Server I can do this: But in postgresql I don’t have any idea how handle this. I’ve tring some ways to do this, but unfortunately I have not success. Answer The syntax for an IF in Postgres (or rather PL/pgSQL) is IF … THEN &#8230…

Use select filed result on LEFTJOIN string

I’m trying to use id_program result from the availability table inside a string in LEFTJOIN, is it possible? I tried using this {post.id_program} but its not working. EDIT: http://sqlfiddle.com/#!9/aeb4a7/3 Answer I see two ways.. Using CONCAT(): With REPLACE():

SQL – find all combinations

From a dataset like this, I need to get all possible combinations of cases in the same room so that no case overlaps another. The expected result is I am able to get single line results of what case can be combined with which other case like: And I’ve tried some recursions but I am at a loss nowhere nea…

Counting users based on an event count

I have an events table where I currently have a count of users grouped by the date. I am wanting to make two different counts based on the number of times a user had a specific event (we’ll call it event_a) The first count will count the user if event_a appeared exactly 1 time for the user, the 2nd coun…