寫的一個SQL
為給客戶統計機頂盒使用分佈情況所寫的一個SQL語句。雖然技術含量可能不是最高的,但是絕對是最複雜的。要實現這個統計,前提當然還是要對業務有比較深的瞭解。可能有人會質疑語句的效率,經過實際的執行,最快100秒就能出來結果。而且後續的資料出來速度也很快。
主要是前天跟客戶吹了牛逼,說要用一個語句實現統計需求,看來總算牛逼沒破(以下所有語句都是同一個SQL):
select aa.*,
(case when aa.subnum = 1 and aa.biztype = 1 and aa.workitem_type_id = 2 then aa.account_id end) "一臺機頂盒(基本型)",
(case when aa.subnum = 1 and aa.biztype = 1 and aa.workitem_type_id = 0 then aa.account_id end) "一臺機頂盒(廣播型)",
(case when aa.subnum = 1 and aa.biztype in(2,3) and aa.is_iptv = 0 then aa.account_id end) "一臺機頂盒(增強無互動)",
(case when aa.subnum = 1 and aa.biztype in(2,3) and aa.is_iptv = 1 then aa.account_id end) "一臺機頂盒(增強有互動)",
(case when aa.subnum = 2 and aa.biztype in(2,3) and aa.is_iptv = 1 and aa.tsubwtpid = 2
and (select count(1) from ow_subscriber sub
where sub.subscriber_id <> aa.subscriber_id
and sub.status not in(6,7,8)
and sub.biztype = 1
and sub.account_id = aa.account_id) = 1 then aa.account_id end) "兩臺機頂盒(增強有互動/基本)",
(case when aa.subnum = 2 and aa.biztype in(2,3) and aa.is_iptv = 0 and aa.tsubwtpid = 2
and (select count(1) from ow_subscriber sub
where sub.subscriber_id <> aa.subscriber_id
and sub.status not in(6,7,8)
and sub.biztype = 1
and sub.account_id = aa.account_id) = 1 then aa.account_id end) "兩臺機頂盒(增強無互動/基本)",
(case when aa.subnum = 2 and aa.biztype in(2,3) and aa.is_iptv = 1 and aa.tsubwtpid = 0
and (select count(1) from ow_subscriber sub
where sub.subscriber_id <> aa.subscriber_id
and sub.status not in(6,7,8)
and sub.biztype = 1
and sub.account_id = aa.account_id) = 1 then aa.account_id end) "兩臺機頂盒(增強有互動/廣播)",
(case when aa.subnum = 2 and aa.biztype in(2,3) and aa.is_iptv = 0 and aa.tsubwtpid = 0
and (select count(1) from ow_subscriber sub
where sub.subscriber_id <> aa.subscriber_id
and sub.status not in(6,7,8)
and sub.biztype = 1
and sub.account_id = aa.account_id) = 1 then aa.account_id end) "兩臺機頂盒(增強無互動/廣播)",
(case when aa.subnum = 2 and aa.biztype in(2,3) and aa.biznum = 2 and aa.iptvnum = 1 then aa.account_id end) "兩臺機頂盒(增強有/無互動)",
(case when aa.subnum = 2 and aa.biztype in(2,3) and aa.biznum = 2 and aa.iptvnum = 2 then aa.account_id end) "兩臺機頂盒(增強都有互動)",
(case when aa.subnum = 2 and aa.biztype = 1 and aa.workitem_type_id = 2 and aa.tsubwtpid = 0
and (select count(1) from ow_subscriber sub
where sub.subscriber_id <> aa.subscriber_id
and sub.status not in(6,7,8)
and sub.biztype = 1
and sub.account_id = aa.account_id) = 1 then aa.account_id end) "兩臺機頂盒(基本/廣播)",
(case when aa.subnum = 2 then aa.account_id end) "兩臺機頂盒(總數)",
(case when aa.subnum = 3 and aa.biztype in(2,3) and aa.biznum = 1 and aa.is_iptv = 1 then aa.account_id end) "三臺機頂盒(一臺增強有互動)",
(case when aa.subnum = 3 and aa.biztype in(2,3) and aa.biznum = 1 and aa.is_iptv = 0 then aa.account_id end) "三臺機頂盒(一臺增強無互動)",
(case when aa.subnum = 3 and aa.biztype in(2,3) and aa.biznum = 2 and aa.iptvnum = 2 then aa.account_id end) "三臺機頂盒(兩臺增強都有互動)",
(case when aa.subnum = 3 and aa.biztype in(2,3) and aa.biznum = 2 and aa.iptvnum = 1 then aa.account_id end) "三臺機頂盒(兩臺增強有/無互動)",
(case when aa.subnum = 3 then aa.account_id end) "三臺機頂盒(總數)",
(case when aa.subnum >= 4 and aa.biztype in(2,3) and aa.biznum = 1 then aa.account_id end) "四臺機頂盒(一臺增強)",
(case when aa.subnum >= 4 and aa.biztype in(2,3) and aa.biznum >= 2 then aa.account_id end) "四臺機頂盒(兩臺以上增強)",
(case when aa.subnum >= 4 then aa.account_id end) "四臺機頂盒(總數)"
from (
select nn.*, sum(is_iptv) over(partition by nn.account_id, nn.biztype) iptvnum,
lead(nn.workitem_type_id) over(partition by nn.account_id order by nn.biztype desc) tsubwtpid --兩臺機頂盒的情況使用該欄位
from (
select sub.account_id, rec.biztype, sub.subscriber_id, pr.workitem_type_id,
count(sub.subscriber_id) over(partition by sub.account_id) subnum,
count(rec.biztype) over(partition by sub.account_id, rec.biztype) biznum,
(case when (select count(1) from ow_subscription spn, ow_product pr
where pr.service_type in(2,3)
and spn.status not in(6,7,8)
and pr.id = spn.product_id
and spn.subscriber_id = ssp.subscriber_id) = 0 then 0 else 1 end) is_iptv
from ow_subscription ssp, ow_subscriber sub, ow_product pr, ow_resource_category rec
where rec.id = pr.resource_category_id
and pr.id = ssp.product_id
and sub.subscriber_id = ssp.subscriber_id
and ssp.subscription_type = 4
--and ssp.account_id = 244063
and ssp.status not in(6,7,8)) nn) aa
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12932950/viewspace-567184/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 如何寫一對多分頁的SQLSQL
- 今天寫了一個統計執行sql次數的指令碼SQL指令碼
- 一種提升SQL改寫效率的方法SQL
- 編寫一個 SQL 查詢來實現分數排名。SQL
- gluesql/gluesql:一個用Rust編寫的SQL資料庫引擎庫包SQLRust資料庫
- 一個反直覺的sqlSQL
- sql devloper 用法的和SQL 編寫SQLdev
- 編寫 SQL 程式碼時常犯的九個錯誤SQL
- c# 怎樣能寫個sql的解析器C#SQL
- 開發一個不需要重寫成 Hive QL 的大資料 SQL 引擎Hive大資料SQL
- 開發一個不需要重寫成Hive QL的大資料SQL引擎Hive大資料SQL
- 記錄一個sqlSQL
- 手寫一個自己的PromisePromise
- vue寫的一個拖拽容器Vue
- PhpOffice 寫一個漂亮的表格PHP
- 一對多分頁的SQL到底應該怎麼寫?SQL
- 書寫高質量sql的一些建議SQL
- pl/sql developer的一個小問題SQLDeveloper
- 將第一個 sql 語句中的結果作為第二個 sql 的引數值SQL
- 寫一個簡單的 Facade 示例
- 手寫一個簡易的WebpackWeb
- 如何寫一個Vue的外掛Vue
- 一個簡單的BypassUAC編寫
- 寫一個滑鼠跟隨的特效特效
- MySQL的SQL等價改寫MySql
- 手寫一個PromisePromise
- 自己寫一個ProviderIDE
- 寫一個Geometry Shader
- MySQL寫sql的21個好習慣,讓你的工作效率翻倍MySql
- MySQL寫sql的21個好習慣,學習工作效率翻倍MySql
- 用 DB::select 寫原生 SQL 一直報錯!記得開這個開關SQL
- 一個 JSer 的 Dart 學習日誌(六):寫一個向量JSDart
- SQL Server關於AlwaysOn的理解-讀寫分離的誤區(一)SQLServer
- 你與寫的一手好sql的大佬可能就差這一道題!SQL
- 手把手教你寫一個java的orm(一)JavaORM
- 自己寫的簡單的一個部落格
- 寫一個最簡陋的node框架框架
- 自己寫一個vue的loading外掛Vue
- 用 Go 寫一個簡易的 dockerGoDocker