我有一张表格,其中包含有关代表当事人的律师事务所的数据:
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
我想要一家律师事务所,比如说身份证号为918的律师事务所,作为主力,并确定哪些律师事务所在这些案件中与主要律师事务所在同一/相反的一方行事。换句话说,我想离开:
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
如何使用SQL或SQLAlchemy执行此操作?是否可以选择一个列,其值基于特定行的该列的值?例如。对于case_id 2001300896,id为918的律师事务所代表原告,因此对于具有该case_id的所有行,锚定方类型为原告。
答案 0 :(得分:1)
这可以使用子查询来获取所有律师事务所的案例,然后再加入使用case_id,如下所示:
function SetGridHeaderStyles() {
//Insert a header row above the grid header
var headerRow = "<tr><th colspan='7' style='background-color: transparent'></th><th style='background-color: #636363; border-bottom:3px solid #fff; font-weight:bold;' colspan='9'>Overall Statistics</th><th style='background-color: #636363; border-bottom:3px solid #fff; font-weight:bold;' colspan='9'>Office Statistics</th><th style='background-color: #636363; border-bottom:3px solid #fff; font-weight:bold;' colspan='9'>Nursing Home Statistics</th><th colspan='2' style='background-color: transparent'></th></tr>";
var existingHeader = $("#Grid").html();
$("#Grid thead tr").before(headerRow);
$('.gridWithExtRw tr:eq(1) th').resizable({
handles: "e",
resize: function(event) {
$('.gridWithExtRw th').parent('tr').find('.ui-resizable-handle').css({
'height': $(this).height() + 21 + "px"
});
}
});
答案 1 :(得分:0)
选择918律师事务所的案例并加入表格中选择:
SELECT t.case_id,
t.law_firm_id,
t.party_type,
s.party_type AS anchored_party_type
FROM (
SELECT case_id, party_type
FROM table
WHERE law_firm_id = 918) s
JOIN table t ON t.case_id = s.case_id
SQLAlchemy Python的翻译将取决于您使用的是Core还是ORM。如果是Core并给出了表变量cases
:
In [8]: sq = select([cases.c.case_id, cases.c.party_type]).\
...: where(cases.c.law_firm_id == 918).\
...: alias()
In [9]: stmt = select([cases.c.case_id,
...: cases.c.law_firm_id,
...: cases.c.party_type,
...: sq.c.party_type.label('anchored_party_type')]).\
...: select_from(sq.join(cases, sq.c.case_id == cases.c.case_id))
...:
In [10]: print(stmt)
SELECT cases.case_id, cases.law_firm_id, cases.party_type, anon_1.party_type AS anchored_party_type
FROM (SELECT cases.case_id AS case_id, cases.party_type AS party_type
FROM cases
WHERE cases.law_firm_id = %(law_firm_id_1)s) AS anon_1 JOIN cases ON anon_1.case_id = cases.case_id