比较多列:如果相同的值1,否则为0

时间:2013-10-07 17:11:28

标签: sql sql-server compare case

我有一个庞大的公司对数据集与他们的资金风险资本家,我想检查这对货币对中只有一个是由VC资助的(如果是列OVC = 1,否则为0),两个合作伙伴都是由至少一个VC(如果是列BVB = 1)或两个合作伙伴是否由同一VC资助(如果是列CVC = 1)。目前,我的表中列有Partner11,其资金来自风险投资VC11至VC17,以及Partner22,其资金风险投资在VC21至VC27。为了能够在一行中说明标题,我在下表中省略了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列中是否至少有一个值,或者在伙伴22的VC21-VC27列中是否包含(不包括)见第2至4行。

要在BVB列中插入0或1,代码应检查两个伙伴是否至少在其VC列之一中具有值(对于VC11-VC17中的partner11,对于VC21-VC27中的partner22)(请参阅行) 5 + 6)。

要在CVC列中插入0或1,代码应该将伙伴的VC列的值相互比较,这意味着VC11应该与VC21,VC22,VC23,VC24,VC25,VC26和VC27进行比较; VC12与VC21,VC22,VC23,VC24,VC25,VC26,VC27等(见第7 + 8行)。

我真的很感激有任何帮助来解决这个问题。

非常感谢你。

1 个答案:

答案 0 :(得分:0)

嗯,这是我写过的最糟糕的查询。但是,当你的桌子没有标准化时,这就是你最终的结果。如果这有助于我,请告诉我:

运行以下内容以更新OVBBVB以及CVCSQL 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