Sql 基于父值的向后更新逻辑
我有下表,数据如下 表2\u单元Sql 基于父值的向后更新逻辑,sql,sql-server,tsql,sql-update,subquery,Sql,Sql Server,Tsql,Sql Update,Subquery,我有下表,数据如下 表2\u单元 Cell_id Column_id row_id SwitchCell ------------------------------ 1122 11 22 1 1133 11 33 0 1144 11 44 1 5522 55 22 1 5533 55 33 1 5544 55 44 1 表(u)栏
Cell_id Column_id row_id SwitchCell
------------------------------
1122 11 22 1
1133 11 33 0
1144 11 44 1
5522 55 22 1
5533 55 33 1
5544 55 44 1
表(u)栏
column_id SwitchCol
11 ?
55 ?
表11行
row_id Switchrow
22 ?
33 ?
44 ?
我需要更新Table_列中的SwitchCol和Table_行中的Switchrow
逻辑是
如果对于Table_列中的列_id,Table Table_单元格中与Table_row中的row_id的所有组合为1,则使该列的SwitchCol=1
类似地,对于Table_行中的id,Table_行与Table_单元格中Table_列中的id的所有组合都是1,然后使该行的Switchrow=1
例如:对于列_id=55,它与表_单元格(55225535544)中的表_行Table的组合具有SwitchCell=1,因此55的SwitchCol将为1,
但是11的SwitchCol将是0(因为(112211331144)有1,0,1,在一种情况下,它是关闭的
请在逻辑方面帮助我。查找sql查询我想您只需要
min()
和一个相关的子查询:
update table_column co
set switchcol = (
select min(ce.switchcell)
from table_cell ce
where ce.column_id = co.column_id
)
我正在寻找sql查询:)