您好,登錄后才能下訂單哦!
1.OLD:
SELECT card.c_cust_id, card.TYPE, card.n_all_money FROM card WHERE card.c_cust_id NOT IN (SELECT c_cust_id FROM card WHERE TYPE IN ('11', '12', '13', '14') AND flag = '1') AND card.TYPE IN ('11', '12', '13', '14') AND card.flag = 'F';
2.優化方向
(1).主查詢和子查詢使用的表相同,條件差不多。考慮進行合并。
(2).
使用分析函數找出相同c_cust_id 既card.flag = 'F' 也 flag = '1' 或者只滿足flag = '1' 然后將這部分記錄過濾掉即可。
當分組結果card.flag = 'F' 也 flag = '1' min(flag) over(partition by card.c_cust_id) = '1'
當分組結果flag = '1' min(flag) over(partition by card.c_cust_id) = '1'
當分組結果flag = 'F' min(flag) over(partition by card.c_cust_id) = 'F' (需要)
select card.c_cust_id, card.TYPE, card.n_all_money from (select card.c_cust_id, card.TYPE, card.n_all_money, min(flag) over(partition by card.c_cust_id) from card where card.TYPE IN ('11', '12', '13', '14') and card.flag in ('1','F')) where card.flag = 'F';
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。