目錄html
@(總覽介紹)sql
一下全部的操做都吧下面的這張表作爲操做對象數據庫
create table CUSTOMER_GROUP_MEMBER_LIST ( CUSTOMER_GROUP_ID NUMBER(8) not null, MEMBER_MSISDN NUMBER(15) not null, IS_VALID NUMBER(1) default 9 not null, INVALID_CAUSE NUMBER(1), CREATE_TIME DATE, CREATE_OPER_ID VARCHAR2(64), constraint PK_GROUP_MEMBER_LIST primary key (CUSTOMER_GROUP_ID, MEMBER_MSISDN) )
預覽信息
oracle
SELECT COUNT(*) FROM CUSTOMER_GROUP_MEMBER_LIST 或者可使用 SELECT COUNT(1) FROM CUSTOMER_GROUP_MEMBER_LIST 在或者 SELECT COUNT(IS_VALID) FROM CUSTOMER_GROUP_MEMBER_LIST (某個字段)
要求:
統計IS_VALID 爲1的數據總數
統計IS_VALID 爲0的數據總數
統計IS_VALID爲0且INVALID_CAUSE爲1的數據
統計IS_VALID爲0且INVALID_CAUSE爲2的數據
統計IS_VALID爲0且INVALID_CAUSE爲3的數據函數
SELECT * FROM (SELECT COUNT(1) COUNTONE FROM CUSTOMER_GROUP_MEMBER_LIST WHERE IS_VALID = 0) CASEONE, (SELECT COUNT(1) COUNTTWO FROM CUSTOMER_GROUP_MEMBER_LIST WHERE IS_VALID = 1) CASETWO, (SELECT COUNT(1) COUNTTHREE FROM CUSTOMER_GROUP_MEMBER_LIST WHERE IS_VALID = 0 AND INVALID_CAUSE = 1) CASETHREE, (SELECT COUNT(1) COUNTTOUR FROM CUSTOMER_GROUP_MEMBER_LIST WHERE IS_VALID = 0 AND INVALID_CAUSE = 2) CASEFOUR, (SELECT COUNT(1) COUNTFIVE FROM CUSTOMER_GROUP_MEMBER_LIST WHERE IS_VALID = 0 AND INVALID_CAUSE = 3) CASEFIVE
展現效果
code
--簡單Case函數 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其餘' END --Case搜索函數 CASE WHEN sex = '1' THEN '男' WHEN sex = '2' THEN '女' ELSE '其餘' END
SELECT COUNT(CASE IS_VALID WHEN 0 THEN 1 ELSE NULL END) COUNTONE, COUNT(CASE IS_VALID WHEN 1 THEN 1 ELSE NULL END) COUNTTWO, COUNT(CASE IS_VALID WHEN 0 THEN CASE INVALID_CAUSE WHEN 1 THEN 1 ELSE NULL END ELSE NULL END) COUNTTHREE, COUNT(CASE IS_VALID WHEN 0 THEN CASE INVALID_CAUSE WHEN 2 THEN 1 ELSE NULL END ELSE NULL END) COUNTTOUR, COUNT(CASE IS_VALID WHEN 0 THEN CASE INVALID_CAUSE WHEN 3 THEN 1 ELSE NULL END ELSE NULL END) COUNTFIVE FROM CUSTOMER_GROUP_MEMBER_LIST
demo decode(sign(score-70),1,'良好',0,'良好',-1,
所以進一步操做htm
SELECT COUNT(DECODE(CGML.IS_VALID, 1, 1, NULL)) VALID_COUNT, COUNT(DECODE(CGML.IS_VALID, 0, 1, NULL)) INVALID_COUNT, COUNT(DECODE(CGML.IS_VALID || CGML.INVALID_CAUSE, 01 , 1, NULL)) FEMOBILE_COUNT, COUNT(DECODE(CGML.IS_VALID || CGML.INVALID_CAUSE, 02 , 1, NULL)) INCREET_COUNT, COUNT(DECODE(CGML.IS_VALID || CGML.INVALID_CAUSE, 03 , 1, NULL)) NOTEXIST_COUNT FROM CUSTOMER_GROUP_MEMBER_LIST CGML
查看結果,然而結果並不對 心涼了
- 問題出現的緣由
對象
null和數字的拼接以後的數據進行判斷出問題了 01 並非按照字符的方式進行判斷的
所以我這裏對於篩選的結果再次進行了驗證
更加直觀的展現結果
blog
SELECT COUNT(1) TOTAL, COUNT(DECODE(CGML.IS_VALID, 1, 1, NULL)) VALID_COUNT, COUNT(DECODE(CGML.IS_VALID, 0, 1, NULL)) INVALID_COUNT, COUNT(DECODE(TO_CHAR(CGML.IS_VALID || CGML.INVALID_CAUSE), '01' , 1, NULL)) FEMOBILE_COUNT, COUNT(DECODE(TO_CHAR(CGML.IS_VALID || CGML.INVALID_CAUSE), '02' , 1, NULL)) INCREET_COUNT, COUNT(DECODE(TO_CHAR(CGML.IS_VALID || CGML.INVALID_CAUSE), '03' , 1, NULL)) NOTEXIST_COUNT FROM CUSTOMER_GROUP_MEMBER_LIST CGML