0

我有 8 位列,A1,A2,a3,a4,b1,b2,b3,b4。所有 8 个都是完全独立的,并且基于这些,应该填充另一个字段。

我想用文本 A、B 或 AB 更新这个其他字段,具体取决于 8 列中的哪一列设置为 1。

这里有几个例子;- 所有 8 个字段都设置为 1,然后填充 AB,
- 如果 A3 和 B1 设置为 1,则填充 AB,
- 如果 A1 和 A3 设置为 1,则填充 A,
- 如果 B4 和 B2 设置为 1然后用 B 填充。

因此,对于 A1 到 B4 的任何组合,应设置该字段

以下是我尝试过的,但不完整,但会给出一个想法;

更新

来自adrianm的正确答案

UPDATE m
SET ref = ASet + BSet 
FROM contactMaster m
inner join contact c on 
m.contactid = c.contactid
 CROSS APPLY (
     SELECT CASE WHEN (c.A1 | c.A2 | c.A3 | c.A4) = 1 THEN 'C' ELSE '' END AS ASet
           ,CASE WHEN (c.B1 | c.B2 | c.B3 | c.B4) = 1 THEN 'D' ELSE '' END AS BSet
 ) AS CA1 
where ref is null
4

2 回答 2

2
UPDATE ContactMaster
    SET ref = ASet + BSet 
FROM ContactMaster
     INNER JOIN Contact
          ON ContactMaster.ContactId = Contact.ContactId
     CROSS APPLY (
         SELECT CASE WHEN (Contact.A1 | Contact.A2 | Contact.A3 | Contact.A4) = 1 THEN 'A' ELSE '' END AS ASet
               ,CASE WHEN (Contact.B1 | Contact.B2 | Contact.B3 | Contact.B4) = 1 THEN 'B' ELSE '' END AS BSet
     ) AS CA1 
WHERE ContactMaster.ref IS NULL
于 2013-10-17T10:20:06.657 回答
0
IF (Select Count(*) from table where A1=1 AND A2 =1 AND a3 =1 AND a4 =1 AND b1 =1 AND b2 =1 AND b3 =1 AND b4=1 )>0
    BEGIN

    UPDATE MyTable 
    SET ColumnValue ='AB' 
    where A1=1 AND A2 =1 AND a3 =1 AND a4 =1 AND b1 =1 AND b2 =1 AND b3 =1 AND b4=1
    END

   ELSE IF (Select Count(*) from table where A1 =1 and A3 =1 )>0

    BEGIN
     Update MyTable set columnValue ='A'
     where A1 =1 and A3 =1
    END   
于 2013-10-17T10:10:01.107 回答