有哪些基于两个条件更新列数据的有用方法?

时间:2015-09-28 17:41:31

标签: sql ms-access

我对以下代码有两个问题:

UPDATE HRBI SET HRBI.PayGroupRegionCode = IIF(HRBI.PayGroupRegionCode = 'UNK' AND HRBI.CompensationGradeProfile = 'Japan JPN', 'APJ',
IIF(HRBI.PayGroupRegionCode = 'UNK' AND HRBI.CompensationGradeProfile = 'Taiwan TWN', 'APJ',
IIF(HRBI.PayGroupRegionCode = 'UNK' AND HRBI.CompensationGradeProfile = 'China CNA', 'APJ',
IIF(HRBI.PayGroupRegionCode = 'UNK' AND HRBI.CompensationGradeProfile = 'Australia AUS', 'APJ',
IIF(HRBI.PayGroupRegionCode = 'UNK' AND HRBI.CompensationGradeProfile = 'Singapore SGP', 'APJ',
IIF(HRBI.PayGroupRegionCode = 'UNK' AND HRBI.CompensationGradeProfile = 'Thailand THA', 'APJ',
IIF(HRBI.PayGroupRegionCode = 'UNK' AND HRBI.CompensationGradeProfile = 'Luxembourg LUX','EMEA',
IIF(HRBI.PayGroupRegionCode = 'UNK' AND HRBI.CompensationGradeProfile = 'Romania ROU', 'EMEA',HRBI.PayGroupRegionCode))))))));
  1. 如果没有满足任何条件,false条件是否返回PayGroupRegionCode中的默认值(列中已有的值)?
  2. 还有其他/更好的方式来写这个吗?

2 个答案:

答案 0 :(得分:1)

  1. 考虑Switch而不是嵌套的IIf表达式。还可以使用模式CompensationGradeProfile IN (<list of values>)
  2. UPDATE HRBI
    SET HRBI.PayGroupRegionCode =
        Switch
            (
                HRBI.CompensationGradeProfile IN
                        (
                            'Japan JPN',
                            'Taiwan TWN',
                            'China CNA',
                            'Australia AUS',
                            'Singapore SGP',
                            'Thailand THA'
                        ), 'APJ',
                HRBI.CompensationGradeProfile IN
                        (
                            'Luxembourg LUX',
                            'Romania ROU'
                        ), 'EMEA',
                True, HRBI.PayGroupRegionCode
            )
    WHERE HRBI.PayGroupRegionCode = 'UNK';
    

答案 1 :(得分:1)

由于您要检查PayGroupRegionCode中的值是否已修复("UNK"),因此您可以将其移至查询的WHERE子句。

现在,让我们简化一下:

UPDATE HRBI 
SET PayGroupRegionCode = IIF(CompensationGradeProfile = 'Japan JPN', 'APJ',
IIF(CompensationGradeProfile = 'Taiwan TWN', 'APJ',
IIF(CompensationGradeProfile = 'China CNA', 'APJ',
IIF(CompensationGradeProfile = 'Australia AUS', 'APJ',
IIF(CompensationGradeProfile = 'Singapore SGP', 'APJ',
IIF(CompensationGradeProfile = 'Thailand THA', 'APJ',
IIF(CompensationGradeProfile = 'Luxembourg LUX','EMEA',
IIF(CompensationGradeProfile = 'Romania ROU', 'EMEA', HRBI.PayGroupRegionCode))))))))
where PayGroupRegionCode = 'UNK';

现在,鉴于您只更新了两个值,我认为最简单的方法是使用两个不同的更新查询(更易于阅读和易于运行):

UPDATE HRBI
SET payGroupRegionCode = 'APJ'
WHERE PayGroupRegionCode = 'UNK'
  AND CompensationGradeProfile IN ('Japan JPN', 'Taiwan TWN', 'China CNA', 
                                   'Australia AUS', 'Singapore SGP');
UPDATE HRBI
SET payGroupRegionCode = 'EMEA'
WHERE PayGroupRegionCode = 'UNK'
  AND CompensationGradeProfile IN ('Luxembourg LUX','Romania ROU');

请注意,由于您没有使用多个表,因此可以在查询(查询)的字段列表中省略表名。

替代方案(未经测试)

自从我上次使用Access以来已经有一段时间了,但也许你可以试试这个:

UPDATE HRBI
SET payGroupRegionCode = IIF(
        CompensationGradeProfile IN ('Japan JPN', 'Taiwan TWN', 'China CNA', 
                                     'Australia AUS', 'Singapore SGP'),
        'APJ',
        IIF(
            CompensationGradeProfile IN ('Luxembourg LUX','Romania ROU'), 
            'EMEA', 
            PayGroupRegionCode
        )
    )
WHERE PayGroupRegionCode = 'UNK';