SQL Server、Oracle中CASE 與COUNT合用計數
使用CASE及COUNT對不同值進行計數
1. SQL Server中
select TeacherNo,(case WeekNo when 1 then count(TeacherNo) end) XQ1,
(case WeekNo when 2 then count(TeacherNo) end) XQ2,
(case WeekNo when 3 then count(TeacherNo) end) XQ3
from A
group by TeacherNo;
2. Oracle 中
with A as (
select 1 TeacherNo,2 WeekNo from dual
union all
select 1 TeacherNo,3 WeekNo from dual
union all
select 2 TeacherNo,1 WeekNo from dual
union all
select 3 TeacherNo,2 WeekNo from dual
union all
select 1 TeacherNo,2 WeekNo from dual
)
select TeacherNo,count(case WeekNo when 1 then 1 else null end) XQ1,
count(case WeekNo when 2 then 1 else null end) XQ2,
count(case WeekNo when 3 then 1 else null end) XQ3
from A
group by TeacherNo;
1. SQL Server中
select TeacherNo,(case WeekNo when 1 then count(TeacherNo) end) XQ1,
(case WeekNo when 2 then count(TeacherNo) end) XQ2,
(case WeekNo when 3 then count(TeacherNo) end) XQ3
from A
group by TeacherNo;
2. Oracle 中
with A as (
select 1 TeacherNo,2 WeekNo from dual
union all
select 1 TeacherNo,3 WeekNo from dual
union all
select 2 TeacherNo,1 WeekNo from dual
union all
select 3 TeacherNo,2 WeekNo from dual
union all
select 1 TeacherNo,2 WeekNo from dual
)
select TeacherNo,count(case WeekNo when 1 then 1 else null end) XQ1,
count(case WeekNo when 2 then 1 else null end) XQ2,
count(case WeekNo when 3 then 1 else null end) XQ3
from A
group by TeacherNo;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10314474/viewspace-772386/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SQL Server中count(*)和Count(1)的區別SQLServer
- SQL語句case when外用sum與count的區別SQL
- SQL Server CASE WHEN ... THEN ... ELSE ... ENDSQLServer
- Oracle case when改寫SQLOracleSQL
- Oracle group by與case when統一單位後統計數量Oracle
- Oracle 11g新特新--SQL Test Case BuilderOracleSQLUI
- HOME: Count Digits —— 計算字串中數字個數Git字串
- SQL Server 中將字串按數字排序SQLServer字串排序
- SQL Server與雲端計算(下)WCSQLServer
- SQL Server與雲端計算(上)UHSQLServer
- 瞭解GaussDB SQL中CASE表示式SQL
- 【Case】SQL SERVER 屬性OWNER不可用於資料庫xxx。SQLServer資料庫
- SQL CASE 表示式SQL
- SQL Server 審計(Audit)SQLServer
- Oracle\MS SQL Server Update多表關聯更新OracleSQLServer
- MySql/Oracle和SQL Server的分頁查MySqlOracleServer
- SQL Server AlwaysOn的Oracle等價技術SQLServerOracle
- SQL Server 2008中Analysis Services的新特性——深入SQL Server 2008SQLServer
- SQL Server安全審計中的常見疏忽NFSQLServer
- [network][easy case]troubleshoting the connection to a remote serverREMServer
- SQL Server解惑——查詢條件IN中能否使用變數SQLServer變數
- SQL Server ceiling向上取小數SQLServer
- 【SQL Server】常用全域性變數SQLServer變數
- SQL Server中GROUP BY(連結)SQLServer
- 16、MySQL Case-索引key對select count(*)的影響MySql索引
- .sql檔案匯入到sql server中SQLServer
- sql server中的一個坑-len與datalength區別SQLServer
- sql case when, Exist ,group by ,聚合SQL
- SQL Server 更新統計資訊SQLServer
- 一次搞定各種資料庫SQL執行計劃:MySQL、Oracle、SQL Server、PostgreSQL以及SQLite資料庫MySqlOracleServerSQLite
- Sql Server 的引數化查詢SQLServer
- SQL中的case when then else end用法 【詳細】轉載SQL
- Oracle中的sql hintOracleSQL
- oracle 19c sec_case_sensitive_logon引數問題OracleGo
- sql server中巢狀事務*SQLServer巢狀
- SQL Server 中的 ACID 屬性SQLServer
- C++ :引用計數(reference count) 實現C++
- sql serverSQLServer
- MongoDB中如何使用統計count()方法?MongoDB