将 Table T1 列更新为两个查询的商,每个查询都利用 T1 中的 ID

Update Table T1 column to be the quotient of two queries each leveraging ID from T1

我正在尝试将列更新为两个查询的商,这两个查询为 T1 中的每个 ID 计算另外两个表中的记录。

这似乎应该有效(但无效):

Update T1 Set COLUMN1 = (select count(*) from T2 where T2.ID = T1.ID) / (select count(*) from T3 where T3.ID = T1.ID)

编辑以添加数据样本和预期输出:

T1 就像:

ID COLUMN1
0
1

T2和T3都是这样,ID可以重复:

ID UID
0 00
1 01
1 02
1 03

T1 的预期输出应该是:

ID COLUMN1
0 quotient of count of records in t2 and t3 where id is 0
1 quotient of count of records in t2 and t3 where id is 1

也许这行得通?

Update T1 
Set COLUMN1 = 1.0 * (select count(*) from T2 where T2.ID = T1.ID) / 
(select count(*) from T3 where T3.ID = T1.ID)

乘以 1.0 强制结果为浮点数。

不知道要在 T3 上模拟什么数据 table,根据提到的问题,它会像 T2(假定相同)并构建查询。看看

WITH t1 AS (
    SELECT
        *
    FROM
    VALUES
        (0, ''),(1, '') v(ID, COLUMN1)
),
t2 as (
    SELECT
        *
    FROM
    VALUES
        (0, 00),(1, 01),(1, 02),(1, 03) v(ID, UID)
),
t3 as (
    SELECT
        *
    FROM
    VALUES
        (0, 00),(1, 01),(1, 02) v(ID, UID)
),
t1_t2_count as (
    select
        count(*) as cnt
    from
        T1,
        T2
    where
        T2.ID = T1.ID
),
t1_t3_count as (
    select
        count(*) as cnt
    from
        T1,
        T3
    where
        T3.ID = T1.ID
)
 Select
   t1_t2_count.cnt t1t2_cnt,
   t1_t3_count.cnt as t1t3_cnt,
   div0(t1t2_cnt,t1t3_cnt) result  
from
    t1_t2_count,t1_t3_count;