如果是,则在转换中插入查询列case语句的逻辑

时间:2018-09-05 23:56:36

标签: tsql

我们对ERP / CRM集成进行了转换。我们可以对转换进行硬编码或使用查找表。现在,我们必须注释掉一种方法,因为表监视在下面被注释掉了。但是,我们需要做的是检查硬代码,然后如果在硬代码中找不到转换,那么我们需要在查找表中查找匹配项。下面列出了一个列的示例。我们需要类似在硬代码转换中找不到表的表。 SQL SERVER 2008R

class sol:
    def __init__(self):
        self.val = 0

def fun1(obj, attrib):
    obj[attrib] = 1

newSol = sol()

fun1(newSol, "val")

3 个答案:

答案 0 :(得分:1)

使用此代码(为简化起见缩写):

a = 0 
checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            a++;
        } else {
            a--;

        }
    }
});

checkbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            a++;
        } else {
            a--;

        }

    }
});
if (a == 1){
    CompoundButtonCompat.setButtonTintList(checkbox1, ColorStateList.valueOf(context.getResources().getColor(R.color.red)));
    CompoundButtonCompat.setButtonTintList(chechbox2, ColorStateList.valueOf(context.getResources().getColor(R.color.red)));
}

if (a == 2) {
    CompoundButtonCompat.setButtonTintList(chechbox1, ColorStateList.valueOf(context.getResources().getColor(R.color.orange)));
    CompoundButtonCompat.setButtonTintList(checkbox2, ColorStateList.valueOf(context.getResources().getColor(R.color.orange)));
}

coalesce( case PC.UMAJOR when ... end, -- case returns null if no hit (SELECT TOP 1 ISNULL(CC.CODE_VALUE_KEY, '') FROM Bridge_test.dbo.peoplechanges pc LEFT JOIN [sis_test].[dbo].[CODE_CURRICULUM] CC ON CC.[CODE_VALUE] = PC.[UMAJOR] ) 返回其参数列表中的第一个非空值。如果没有匹配项,则该案例将返回null,在这种情况下,将返回查找表上的选择。

通过使用带有任意数量参数的coalesce(),您可以通过在子查询返回null的情况下提供第三个值来合并来提高安全性(如果COALESCE为null空):

peoplechanges

答案 1 :(得分:0)

您可以尝试创建mapperTable表而不是HardCode CASE WHEN,让代码更简洁。

将来,您只需要维护mapperTable。

CREATE TABLE mapperTalbe(
   UMAJOR varchar(50),
   curriculum varchar(50)
);

INSERT INTO mapperTalbe VALUES ('ACM' , 'ACCT');
INSERT INTO mapperTalbe VALUES ('BSBA' , 'BADM');
INSERT INTO mapperTalbe VALUES ('B&TS' , 'BIBT');
INSERT INTO mapperTalbe VALUES ('BICH' , 'BIOC');
INSERT INTO mapperTalbe VALUES ('BIO' , 'BIOL');
INSERT INTO mapperTalbe VALUES ('BIS' , 'BIS');
INSERT INTO mapperTalbe VALUES ('BCOM' , 'Bu Com');
INSERT INTO mapperTalbe VALUES ('BUS' , 'BUSI');
INSERT INTO mapperTalbe VALUES ('CHEM' , 'CHEM');
INSERT INTO mapperTalbe VALUES ('AASCM' , 'CHRM');
INSERT INTO mapperTalbe VALUES ('CL&A' , 'CLDA');
INSERT INTO mapperTalbe VALUES ('COM' , 'COMM');
INSERT INTO mapperTalbe VALUES ('CRIM' , 'CRIM');
INSERT INTO mapperTalbe VALUES ('CRMJ' , 'CRMJ');
INSERT INTO mapperTalbe VALUES ('CROS' , 'CROSS');
INSERT INTO mapperTalbe VALUES ('DIGC' , 'DIGCIN');
INSERT INTO mapperTalbe VALUES ('MED' , 'EDUC');
INSERT INTO mapperTalbe VALUES ('EDES' , 'ELED');

CREATE TABLE T (UMAJOR varchar(50));

INSERT INTO T VALUES ('EDES');
INSERT INTO T VALUES ('CRIM');
INSERT INTO T VALUES ('CRIM1');

查询1

SELECT t2.UMAJOR
FROM T t1
LEFT JOIN mapperTalbe t2 on t1.UMAJOR = t2.UMAJOR

Results

| UMAJOR |
|--------|
|   CRIM |
|   EDES |
| (null) |

答案 2 :(得分:0)

只需将子查询添加到case子句中的else

,
           --degree = PC.UCURRICULM, --transformation hard coded
           curriculum = CASE PC.UMAJOR    
            WHEN 'ACM' then 'ACCT'
            WHEN 'BSBA' then 'BADM'
            WHEN 'B&TS' then 'BIBT'
            WHEN 'BICH' then 'BIOC'
            WHEN 'BIO' then 'BIOL'
            WHEN 'BIS' then 'BIS'
            WHEN 'BCOM' then 'Bu Com'
            WHEN 'BUS' then 'BUSI'
            WHEN 'CHEM' then 'CHEM'
            WHEN 'AASCM' then 'CHRM'
            WHEN 'CL&A' then 'CLDA'
            WHEN 'COM' then 'COMM'
            WHEN 'CRIM' then 'CRIM'
            WHEN 'CRMJ' then 'CRMJ'
            WHEN 'CROS' then 'CROSS'
            WHEN 'DIGC' then 'DIGCIN'
            WHEN 'MED' then 'EDUC'
            WHEN 'EDES' then 'ELED'
            else -- Transformation lookup on tables.
              ( SELECT TOP 1 ISNULL(CC.CODE_VALUE_KEY, '') FROM Bridge_test.dbo.peoplechanges pc 
                 LEFT JOIN [sis_test].[dbo].[CODE_CURRICULUM] CC ON CC.[CODE_VALUE] = PC.[UMAJOR] )
            end

提示:最好包含一个else子句。即使它返回null,它也表明您已做出关于如何处理“其他”值的明智决定。