I have a table containing data about law firms representing parties in cases:
case_id | law_firm_id | party_type
------------+-------------+------------
2001300896 | 918 | Plaintiff
2001300896 | 1927 | Plaintiff
2001300896 | 1934 | Plaintiff
2001300896 | 91653 | Defendant
2001300896 | 245649 | Plaintiff
2001300896 | 1534016 | Defendant
2001311137 | 918 | Defendant
2001311137 | 50823 | Plaintiff
2001311137 | 257164 | Defendant
2001311137 | 8055087 | Defendant
I want 1 law firm, say the one with id 918, to serve as the anchor and figure out which law firms are acting on the same / opposite side as the anchor law firm in these cases. In other words, I want to get out:
case_id | law_firm_id | party_type | anchored_party_type
------------+-------------+------------+--------------------
2001300896 | 918 | Plaintiff | Plaintiff
2001300896 | 1927 | Plaintiff | Plaintiff
2001300896 | 1934 | Plaintiff | Plaintiff
2001300896 | 91653 | Defendant | Plaintiff
2001300896 | 245649 | Plaintiff | Plaintiff
2001300896 | 1534016 | Defendant | Plaintiff
2001311137 | 918 | Defendant | Defendant
2001311137 | 50823 | Plaintiff | Defendant
2001311137 | 257164 | Defendant | Defendant
2001311137 | 8055087 | Defendant | Defendant
How can I do this using SQL or SQLAlchemy? Is it possible to select a column where the value is based on the value for that column for a particular row? E.g. for case_id 2001300896, the law firm with id 918 is representing a Plaintiff, so the anchored party type is Plaintiff for all the rows with that case_id.