0
我修改了我對任務2的要求。我想知道是否有人可以幫助我。我嘗試從記錄不在任務#1和任務#3中的ITEM列中獲得總和。在SQL服務器或Oracle中使用case語句
非常感謝。
我創建了一個簡單的表格和一些原始數據。我試圖完成以下任務:
a.Sum記錄包含'FIRA'作爲FIRA。
b。記錄中包含'FMUA'作爲FMUA。
c.Sum記錄包含SOTA作爲SOTA。
d.Sum記錄包含「PERA」作爲PERA。
e.Sum記錄包含「SGDA」作爲SGDA。 f。記錄包含'TGDA'作爲TGDA。
h.Sum記錄包含'CRMA'作爲CRMA。總和並非來自任務#1和任務#3 MULTIPLE_CLASSIFICATION記錄。
將上述#1和#2以外的剩餘記錄求和爲NONE_CLASSIFICATION。
我才得以完成任務#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
謝謝。我希望總結上面插入記錄的最後三條記錄。 – joe 2011-03-06 12:58:42