create table test_table(
IDNUM NUMBER,
CAT VARCHAR2(200),
PRICE NUMBER
);
INSERT INTO test_table VALUES(1,'',10);
INSERT INTO test_table VALUES(2,'cat1',10);
INSERT INTO test_table VALUES(3,'cat1',3);
INSERT INTO test_table VALUES(4,'cat2',11);
INSERT INTO test_table VALUES(5,'cat2',10);
INSERT INTO test_table VALUES(6,'',10);
select
COALESCE(CAT,'unknown'),
SUM(PRICE)from test_table GROUP BY CAT;
result:
COALESCE(CAT,'UNKNOWN') SUM(PRICE)
unknown 20
cat1 13
cat2 21
注意:COALESCE 与 ISNULL相比,COALESCE 类似case,可以多个input,ISNULL只有2个parameter
COALESCE不能完成类型的转换,否则报错