`
newleague
  • 浏览: 1479591 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类

相关知识(二)

SQL 
阅读更多

(以下为网络收集,回答正确性有待验证)

1、

怎樣count非零不重复的记录个数?

假设某列有以下数据:
5
0
3
5
6
0

我想数出这一列中不重复的非零值的个数,在这里就是3。怎么做?要直接在sql里面实现,不能用函数啊过程阿啥的。
注意,有时候0不出现的,所以不能用count(distinct number) - 1。
A:
select sum(decode(number, 0, 0, 1)) from ( select distinct number from t);
select COUNT(distinct(number)) from table where nvl(number,0)<>0
select COUNT(distinct(number)) from table where number<>0
decode函数:
例:表table_subject,有subject_name列。要求按照:语、数、外的顺序进行排序。这时,就可以非常轻松的使用Decode完成要求了。

  select * from table_subject order by decode(subject_name, '语文', 1, '数学', 2, , '外语',3)

nul函数:
NVL(EXPR1,EXPR2)
若EXPR1是NULL,则返回EXPR2,否则返回EXPR1.
SELECT NAME,NVL(TO_CHAR(COMM),'NOT APPLICATION') FROM TABLE1;
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics