sql server分組查詢示例
create table Guest_info
(
GuestID int primary key,
GuestName varchar(50),
birthday datetime
)
create table Or_info
(
Order_id int identity(1,1) primary key,
GuestID int foreign key REFERENCES Guest_info(GuestID),
Isyes int -- 0:notOK 1:Ok 2:Cancel 3:Renege
)
select c.age,
TotalCount=count(*),
notOKCount=sum(case c.Isyes when 0 then 1 else 0 end) ,
cancelCount=sum(case c.Isyes when 2 then 1 else 0 end) ,
RenegeCount=sum(case c.Isyes when 3 then 1 else 0 end) ,
OKCount=sum(case c.Isyes when 1 then 1 else 0 end)
from
(select
age = case
when datediff(yy,cast(a.birthday as datetime),getdate()) >50
then '50以上'
when datediff(yy,cast(a.birthday as datetime),getdate()) between 41 and 50
then '41-50'
when datediff(yy,cast(a.birthday as datetime),getdate()) between 31 and 40
then '31-40'
when datediff(yy,cast(a.birthday as datetime),getdate()) between 20 and 30
then '20-30'
else
'20以下'
end,
b.*
from
Guest_info a inner join
Or_info b
on
a.GuestID = b.GuestID) c
group by c.age
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/66009/viewspace-825236/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- sql分組查詢語句--行內分組(非聚合分組)SQL
- Oracle總結【SQL細節、多表查詢、分組查詢、分頁】OracleSQL
- Sql Server系列:查詢分頁語句SQLServer
- MS SQL Server常見問題 -- 分組 去重複 查詢 統計SQLServer
- 分組查詢
- SQL Server對組合查詢結果排序方法SQLServer排序
- SQL Server中Table字典資料的查詢SQL示例程式碼SQLServer
- SQL Server 查詢分解SQLServer
- mysql-分組查詢-子查詢-連線查詢-組合查詢MySql
- group by分組查詢
- 查詢統計SQL分組求和使用小技巧SQL
- AntDesignBlazor示例——分頁查詢Blazor
- SQL Server遞迴查詢SQLServer遞迴
- SQL SERVER 查詢鎖資訊SQLServer
- SQL server跨庫查詢SQLServer
- Sql Server系列:子查詢SQLServer
- SQL server 查詢語句SQLServer
- SQL Server 分散式查詢SQLServer分散式
- SQL SERVER 動態查詢SQLServer
- SQL Server 跨域查詢SQLServer跨域
- MySQL、Oracle和SQL Server的分頁查詢語句MySqlOracleServer
- [Mysql 查詢語句]——分組查詢group byMySql
- SQL 單表多條記錄分組查詢分頁程式碼SQL
- sql-server高階查詢SQLServer
- SQL Server阻塞查詢語句SQLServer
- Sql Server系列:巢狀查詢SQLServer巢狀
- SQL Server 查詢優化功能SQLServer優化
- SQL Server 2000 的分頁查詢(儲存過程)SQLServer儲存過程
- SQL 如何查詢每個分組都出現的欄位值SQL
- SQL Server 跨資料庫查詢SQLServer資料庫
- Sql Server 的引數化查詢SQLServer
- sql-server不相關子查詢SQLServer
- sql-server相關子查詢SQLServer
- sql server的許可權查詢SQLServer
- Sql Server系列:多表連線查詢SQLServer
- SQL SERVER 跨伺服器查詢SQLServer伺服器
- 查詢當前SQL Server的版本SQLServer
- MS SQL Server查詢優化方法SQLServer優化