寫的一個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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 一個JTextPane寫SQL語句的問題SQL
- sql使用cursor寫一個簡單的迴圈<轉>SQL
- 一條SQL的改寫SQL
- 一個有趣的sqlSQL
- 【sql】編寫基本的SQL SELECT語句一SQL
- 寫好SQL的幾個好習慣SQL
- 決定一個SQL執行效率的是執行計劃, 而不是SQL的寫法SQL
- 如何寫一對多分頁的SQLSQL
- 一條SQL語句的書寫SQL
- 今天寫了一個統計執行sql次數的指令碼SQL指令碼
- 【SQL】【遷移】寫了一個簡單的sequence遷移指令碼SQL指令碼
- 改寫一個要跑5小時的SQL成1分鐘SQL
- 如何寫這個sql語句?SQL
- Update 多個關聯表SQL的寫法SQL
- 寫的一個perl程式
- 寫一個新的xtype
- 編寫一個 SQL 查詢來實現分數排名。SQL
- 一個sql的優化SQL優化
- 一種提升SQL改寫效率的方法SQL
- gluesql/gluesql:一個用Rust編寫的SQL資料庫引擎庫包SQLRust資料庫
- PL/SQL Develop的一個Bug[Oracle資料庫中慎用小寫表名]SQLdevOracle資料庫
- 手寫一個自己的PromisePromise
- vue寫的一個拖拽容器Vue
- 一個反直覺的sqlSQL
- 學習筆記----一句SQL語句寫個乘法表筆記SQL
- 開發一個不需要重寫成Hive QL的大資料SQL引擎Hive大資料SQL
- 開發一個不需要重寫成 Hive QL 的大資料 SQL 引擎Hive大資料SQL
- c# 怎樣能寫個sql的解析器C#SQL
- 編寫 SQL 程式碼時常犯的九個錯誤SQL
- ios 高效的寫好一個UI介面(一)iOSUI
- sql devloper 用法的和SQL 編寫SQLdev
- 手寫一個簡易的WebpackWeb
- PhpOffice 寫一個漂亮的表格PHP
- 如何寫一個Vue的外掛Vue
- 如何快速的寫一個單例單例
- 如何寫一個實用的 bind?
- 寫一個更好的Javascript DOM庫JavaScript
- 手寫一個PromisePromise