我有一个庞大的公司对数据集,他们的资金风险投资家,我想检查货币对中只有一个合伙人由VC资助(如果是,则列OVC=1,否则为0(,两个合伙人都由至少一个VC资助(如果是,则列BVB=1(,或者两个合伙人是否由同一个VC资助(如果是,则列CVC=1(。目前,我的表格有列 Partner11 及其资金 VC11 至 VC17 和 Partner22 及其资金 VC21 至 VC27 的 VC。为了能够在一行中说明标题,我在下表中省略了VC14-VC17和VC24-27。
Partner11 | VC11 | VC12 | VC13 | Partner22 | VC21 | VC22 | VC23 | OVB | BVB | CVC
FirmA | | | | FirmB | | | | 0 | 0 | 0
FirmC | Moa | | | FirmD | | | | 1 | 0 | 0
FirmE | Moa | Fred | Bob | FirmF | | | | 1 | 0 | 0
FirmG | | | | FirmH | Bob | Zoe | Jet | 1 | 0 | 0
FirmE | Moa | | | FirmF | Jet | | | 0 | 1 | 0
FirmG | Moa | Fred | Bob | FirmH | Jet | Zoe | | 0 | 1 | 0
FirmG | Moa | Fred | Bob | FirmH | Bob | Zoe | Jet | 0 | 0 | 1
FirmG | Jet | Bob | Fred | FirmH | Bob | Zoe | Jet | 0 | 0 | 1
如果没有合作伙伴是风险投资支持的,则 OVB、BVB 和 CVC 取值 0(见第 1 行(。
要在 OVB 列中插入 0 或 1,代码应检查该对中的一个伙伴在 partner11 的 VC11-VC17 列中是否至少有一个值,或者(不包括(在 partner22 的 VC21-VC27 列中至少有一个值(请参阅第 2 行到第 4 行(。
要在 BVB 列中插入 0 或 1,代码应检查两个伙伴是否至少在其 VC 列中的一个中都有值(VC11-VC17 中的合作伙伴 11,VC21-VC27 中的合作伙伴 22((请参阅第 5+6 行(。
要在 CVC 列中插入 0 或 1,代码应将合作伙伴的 VC 列的值相互比较,这意味着 VC11 应与 VC21、VC22、VC23、VC24、VC25、VC26 和 VC27 进行比较;VC12 与 VC21、VC22、VC23、VC24、VC25、VC26、VC27 等(请参阅第 7+8 行(。
我将非常感谢解决这个问题的任何帮助。
提前非常感谢你。
嗯,这是我写过的最讨厌的查询。 但是,当你的表没有规范化时,这就是你最终得到的。 让我知道这是否有帮助:
运行以下命令以更新OVB
和BVB
和CVC
(SQL Fiddle(:
UPDATE MyTable
SET OVB =
CASE(
CASE(
(CASE Datalength(VC11) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC12) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC13) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC14) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC15) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC16) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC17) WHEN 0 THEN 0 ELSE 1 END)
) WHEN 0 THEN 0 ELSE 1 END
+
CASE(
(CASE Datalength(VC21) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC22) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC23) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC24) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC25) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC26) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC27) WHEN 0 THEN 0 ELSE 1 END)
) WHEN 0 THEN 0 ELSE 1 END
) WHEN 1 THEN 1 ELSE 0 END
, BVB =
CASE(
CASE(
(CASE Datalength(VC11) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC12) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC13) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC14) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC15) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC16) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC17) WHEN 0 THEN 0 ELSE 1 END)
) WHEN 0 THEN 0 ELSE 1 END
+
CASE(
(CASE Datalength(VC21) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC22) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC23) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC24) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC25) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC26) WHEN 0 THEN 0 ELSE 1 END) +
(CASE Datalength(VC27) WHEN 0 THEN 0 ELSE 1 END)
) WHEN 0 THEN 0 ELSE 1 END
) WHEN 2 THEN 1 ELSE 0 END
, CVC =
CASE WHEN
CASE WHEN
(CASE Datalength(VC11) WHEN 0 THEN 0 ELSE
(CASE WHEN VC11 = VC21 THEN 1 ELSE 0 END) +
(CASE WHEN VC11 = VC22 THEN 1 ELSE 0 END) +
(CASE WHEN VC11 = VC23 THEN 1 ELSE 0 END) +
(CASE WHEN VC11 = VC24 THEN 1 ELSE 0 END) +
(CASE WHEN VC11 = VC25 THEN 1 ELSE 0 END) +
(CASE WHEN VC11 = VC26 THEN 1 ELSE 0 END) +
(CASE WHEN VC11 = VC27 THEN 1 ELSE 0 END) END) >= 1
THEN 1 ELSE 0 END
+
CASE WHEN
(CASE Datalength(VC12) WHEN 0 THEN 0 ELSE
(CASE WHEN VC12 = VC21 THEN 1 ELSE 0 END) +
(CASE WHEN VC12 = VC22 THEN 1 ELSE 0 END) +
(CASE WHEN VC12 = VC23 THEN 1 ELSE 0 END) +
(CASE WHEN VC12 = VC24 THEN 1 ELSE 0 END) +
(CASE WHEN VC12 = VC25 THEN 1 ELSE 0 END) +
(CASE WHEN VC12 = VC26 THEN 1 ELSE 0 END) +
(CASE WHEN VC12 = VC27 THEN 1 ELSE 0 END) END) >= 1
THEN 1 ELSE 0 END
+
CASE WHEN
(CASE Datalength(VC13) WHEN 0 THEN 0 ELSE
(CASE WHEN VC13 = VC21 THEN 1 ELSE 0 END) +
(CASE WHEN VC13 = VC22 THEN 1 ELSE 0 END) +
(CASE WHEN VC13 = VC23 THEN 1 ELSE 0 END) +
(CASE WHEN VC13 = VC24 THEN 1 ELSE 0 END) +
(CASE WHEN VC13 = VC25 THEN 1 ELSE 0 END) +
(CASE WHEN VC13 = VC26 THEN 1 ELSE 0 END) +
(CASE WHEN VC13 = VC27 THEN 1 ELSE 0 END) END) >= 1
THEN 1 ELSE 0 END
+
CASE WHEN
(CASE Datalength(VC14) WHEN 0 THEN 0 ELSE
(CASE WHEN VC14 = VC21 THEN 1 ELSE 0 END) +
(CASE WHEN VC14 = VC22 THEN 1 ELSE 0 END) +
(CASE WHEN VC14 = VC23 THEN 1 ELSE 0 END) +
(CASE WHEN VC14 = VC24 THEN 1 ELSE 0 END) +
(CASE WHEN VC14 = VC25 THEN 1 ELSE 0 END) +
(CASE WHEN VC14 = VC26 THEN 1 ELSE 0 END) +
(CASE WHEN VC14 = VC27 THEN 1 ELSE 0 END) END) >= 1
THEN 1 ELSE 0 END
+
CASE WHEN
(CASE Datalength(VC15) WHEN 0 THEN 0 ELSE
(CASE WHEN VC15 = VC21 THEN 1 ELSE 0 END) +
(CASE WHEN VC15 = VC22 THEN 1 ELSE 0 END) +
(CASE WHEN VC15 = VC23 THEN 1 ELSE 0 END) +
(CASE WHEN VC15 = VC24 THEN 1 ELSE 0 END) +
(CASE WHEN VC15 = VC25 THEN 1 ELSE 0 END) +
(CASE WHEN VC15 = VC26 THEN 1 ELSE 0 END) +
(CASE WHEN VC15 = VC27 THEN 1 ELSE 0 END) END) >= 1
THEN 1 ELSE 0 END
+
CASE WHEN
(CASE Datalength(VC16) WHEN 0 THEN 0 ELSE
(CASE WHEN VC16 = VC21 THEN 1 ELSE 0 END) +
(CASE WHEN VC16 = VC22 THEN 1 ELSE 0 END) +
(CASE WHEN VC16 = VC23 THEN 1 ELSE 0 END) +
(CASE WHEN VC16 = VC24 THEN 1 ELSE 0 END) +
(CASE WHEN VC16 = VC25 THEN 1 ELSE 0 END) +
(CASE WHEN VC16 = VC26 THEN 1 ELSE 0 END) +
(CASE WHEN VC16 = VC27 THEN 1 ELSE 0 END) END) >= 1
THEN 1 ELSE 0 END
+
CASE WHEN
(CASE Datalength(VC17) WHEN 0 THEN 0 ELSE
(CASE WHEN VC17 = VC21 THEN 1 ELSE 0 END) +
(CASE WHEN VC17 = VC22 THEN 1 ELSE 0 END) +
(CASE WHEN VC17 = VC23 THEN 1 ELSE 0 END) +
(CASE WHEN VC17 = VC24 THEN 1 ELSE 0 END) +
(CASE WHEN VC17 = VC25 THEN 1 ELSE 0 END) +
(CASE WHEN VC17 = VC26 THEN 1 ELSE 0 END) +
(CASE WHEN VC17 = VC27 THEN 1 ELSE 0 END) END) >= 1
THEN 1 ELSE 0 END >= 1
THEN 1 ELSE 0 END