If you ever want to conditionally count the number of times a particular condition occurs in SQL, you can do it in Oracle using the case andcount functions. Here's a simple example which counts the number of males/females stored in PS_PERSONAL_DATA. select count(case when SEX = 'M' then 1 end) as MALES, count(case when SEX = 'F' then 1 end) asFEMALESfrom PS_PERSONAL_DATA
All that is happening is that the case statement returns a 1 for every instance where the gender is M or F and the count, counts each returned value of 1 giving a summary like this:
The conditions in the case statement can be a lot more complex and can include sub-queries. |
相关文章
- General Ledger Useful SQL Scripts
- (转) java 通过 jdbc 链接 ms sql server 中出现 "no suitable driver for ..."
- java Mybatis框架动态SQL
- [20180918]文件格式与sql_id.txt
- sql server数据库操作
- SQL Server存储过程邮件发送以表格方式发送
- 在论坛中出现的比较难的sql问题:29(row_number函数 组内某列的值连续出现3次标记出来)
- (4.20)sql server性能指标、性能计数器
- SQL Server中如何设置对列的权限
- SQL STUFF函数 拼接字符串 多列 合并成一列 转