在SQL Server或Oracle中使用case语句

时间:2011-03-06 04:39:42

标签: sql

我修改了对任务#2的要求。我想知道是否有人可以帮助我。我试图从ITEM列获取记录不在任务#1和任务#3中的总和。

非常感谢。

我创建了一个简单的表和一些原始数据。我试图完成以下任务:

  1. a.Sum记录中包含'FIRA'作为FIRA。
    b.Sum记录中包含'FMUA'作为FMUA。
    c.Sum记录包含'SOTA'作为SOTA。
    d.Sum记录中包含'PERA'作为PERA。
    e.Sum记录中包含'SGDA'作为SGDA。
    f.Sum记录包含'TGDA'作为TGDA。
    h.Sum记录中包含'CRMA'作为CRMA。

  2. 汇总非任务#1和任务#3 AS MULTIPLE_CLASSIFICATION的记录。

  3. 将上述#1和#2以外的其余记录汇总为NONE_CLASSIFICATION。

  4. 我能够完成任务#1和#3但无法完成任务#2 下面是我在SQL Server和Oracle中测试的查询。

    CREATE TABLE TEMP
    (
      CLASSIFICATION VARCHAR (100),
      ITEM           INTEGER
    )
    
    insert into temp values ('CRMA', 66);
    insert into temp values ('FIRA', 1288);
    insert into temp values ('FIRA/ATEPT/DR', 3);
    insert into temp values ('DR/SERA/ATEPT/FIRA', 4);
    insert into temp values ('PERA', 1311);
    insert into temp values ('STATE/SERA/PERA/ERS', 1);
    insert into temp values ('null', 136);
    insert into temp values ('PERA/DR/ATEPT', 4);
    insert into temp values ('SOTA', 1);
    insert into temp values ('FERA/SOTA', 1);
    insert into temp values ('SOTA/SATO/DT', 1);
    insert into temp values ('FMUA', 5);
    insert into temp values ('SERA', 8);
    insert into temp values ('SGDA', 3);
    insert into temp values ('TGDA', 1);
    insert into temp values ('TGDA/ATPET', 1);
    insert into temp values ('ATPET', 15);
    insert into temp values ('CRMA/PERA', 2);
    insert into temp values ('SERA/FIRA/ATEPT/SGDA/SGD', 5);
    insert into temp values ('FIRE/FIRA/SERA/DR/SOTA', 4)
    
    -----------------------TASK #1--------------------------
    
    SELECT
    SUM (CASE WHEN CLASSIFICATION LIKE '%FIRA%'
    AND CLASSIFICATION NOT LIKE '%CRMA%'
    AND CLASSIFICATION NOT LIKE '%FMUA%'
    AND CLASSIFICATION NOT LIKE '%SOTA%'  
    AND CLASSIFICATION NOT LIKE '%PERA%'
    AND CLASSIFICATION NOT LIKE '%SGDA%'
    AND CLASSIFICATION NOT LIKE '%TGDA%' THEN ITEM END) AS FIRA,
    
    SUM (CASE WHEN CLASSIFICATION LIKE '%FMUA%'
    AND CLASSIFICATION NOT LIKE '%FIRA%'
    AND CLASSIFICATION NOT LIKE '%CRMA%'
    AND CLASSIFICATION NOT LIKE '%SOTA%'
    AND CLASSIFICATION NOT LIKE '%PERA%'
    AND CLASSIFICATION NOT LIKE '%SGDA%'
    AND CLASSIFICATION NOT LIKE '%TGDA%' THEN ITEM END) AS FMUA,
    
    SUM (CASE WHEN CLASSIFICATION LIKE '%SOTA%'
    AND CLASSIFICATION NOT LIKE '%FIRA%'
    AND CLASSIFICATION NOT LIKE '%CRMA%'
    AND CLASSIFICATION NOT LIKE '%FMUA%'
    AND CLASSIFICATION NOT LIKE '%PERA%'
    AND CLASSIFICATION NOT LIKE '%SGDA%'
    AND CLASSIFICATION NOT LIKE '%TGDA%' THEN ITEM END) AS SOTA,
    
    SUM (CASE WHEN CLASSIFICATION LIKE '%PERA%'
    AND CLASSIFICATION NOT LIKE '%FIRA%'
    AND CLASSIFICATION NOT LIKE '%CRMA%'
    AND CLASSIFICATION NOT LIKE '%FMUA%'
    AND CLASSIFICATION NOT LIKE '%SOTA%'
    AND CLASSIFICATION NOT LIKE '%SGDA%'
    AND CLASSIFICATION NOT LIKE '%TGDA%' THEN ITEM END) AS PERA,
    
    SUM (CASE WHEN CLASSIFICATION LIKE '%SGDA%'
    AND CLASSIFICATION NOT LIKE '%FIRA%'
    AND CLASSIFICATION NOT LIKE '%FMUA%'
    AND CLASSIFICATION NOT LIKE '%SOTA%'
    AND CLASSIFICATION NOT LIKE '%PERA%'
    AND CLASSIFICATION NOT LIKE '%TGDA%'
    AND CLASSIFICATION NOT LIKE '%CRMA%' THEN ITEM END) AS SGDA,
    
    SUM (CASE WHEN CLASSIFICATION LIKE '%TGDA%'
    AND CLASSIFICATION NOT LIKE '%FIRA%'
    AND CLASSIFICATION NOT LIKE '%FMUA%'
    AND CLASSIFICATION NOT LIKE '%SOTA%'
    AND CLASSIFICATION NOT LIKE '%PERA%'
    AND CLASSIFICATION NOT LIKE '%SGDA%'
    AND CLASSIFICATION NOT LIKE '%CRMA%' THEN ITEM END) AS TGDA,
    
    SUM (CASE WHEN CLASSIFICATION LIKE '%CRMA%'
    AND CLASSIFICATION NOT LIKE '%FIRA%'
    AND CLASSIFICATION NOT LIKE '%FMUA%'
    AND CLASSIFICATION NOT LIKE '%SOTA%'
    AND CLASSIFICATION NOT LIKE '%PERA%'
    AND CLASSIFICATION NOT LIKE '%SGDA%'
    AND CLASSIFICATION NOT LIKE '%TGDA%' THEN ITEM END) AS CRMA,
    
    -----------------------TASK #2--------------------------
    SUM (CASE WHEN ( CLASSIFICATION LIKE '%FIRA%'
    OR CLASSIFICATION LIKE '%FMUA%'
    OR CLASSIFICATION LIKE '%SOTA%'
    OR CLASSIFICATION LIKE '%PERA%'
    OR CLASSIFICATION LIKE '%SGDA%'
    OR CLASSIFICATION LIKE '%TGDA%'
    OR CLASSIFICATION LIKE '%CRMA%') THEN ITEM END) AS MULIPLE_CLASSIFICATIONS,
    
    -----------------------TASK #3--------------------------
    SUM (CASE WHEN ( CLASSIFICATION NOT LIKE '%FIRA%'
    AND CLASSIFICATION NOT LIKE '%FMUA%'
    AND CLASSIFICATION NOT LIKE '%SOTA%'
    AND CLASSIFICATION NOT LIKE '%PERA%'
    AND CLASSIFICATION NOT LIKE '%SGDA%'
    AND CLASSIFICATION NOT LIKE '%TGDA%'
    AND CLASSIFICATION NOT LIKE '%CRMA%') THEN ITEM END) AS NONE_CLASSIFICATIONS
    FROM TEMP
    
    FIRA    FMUA    SOTA    PERA    SGDA    TGDA    CRMA    MULIPLE_CLASSIFICATIONS NONE_CLASSIFICATIONS
    
      1295  5     3       1316    3          2       66      2701                      159
    

1 个答案:

答案 0 :(得分:0)

您的任务2包含“FIRA”,其值为1288.由于1288大于您期望的总和,因此您的总和查询应修改为仅包含您正在查找的值的类型对于。请参阅您的插入内容:

insert into temp values ('FIRA', 1288);

和您的查询:

--for task #2
SUM (CASE WHEN ( CLASSIFICATION LIKE '%FIRA%'
OR CLASSIFICATION LIKE '%FMUA%'
OR CLASSIFICATION LIKE '%SOTA%'
OR CLASSIFICATION LIKE '%PERA%'
OR CLASSIFICATION LIKE '%SGDA%'
OR CLASSIFICATION LIKE '%TGDA%'
OR CLASSIFICATION LIKE '%CRMA%') THEN ITEM END) AS MULIPLE_CLASSIFICATIONS,