如何编写以下两条语句中的一条更好的sql以提高性能

发布于 2025-01-12 23:54:28 字数 1161 浏览 0 评论 0原文

如果匹配有多个 ID,请选择带有 max(date) 的 id。 如果上述最长日期有多个 ID,请选择 max(ID) 转至表 1 获取最新 IDS

第 1 步:识别具有最长日期的 ID

CREATE MULTISET VOLATILE TABLE TEST_VT 
AS 
(                                       
SELECT
TABLE1.ID ,
TABLE1.KEY1,
TABLE1.STRT_DT,
TABLE1.TERM_DT,
Rank() OVER (PARTITION BY  TABLE1.KEY1 ORDER BY TABLE1.STRT_DT DESC , TABLE1.ID DESC) RNK
FROM TABLE2          
INNER JOIN TABLE1         
ON TABLE2.KEY1=TABLE1.KEY1    
WHERE  TABLE1.TERM_DT='8888-12-31'
QUALIFY RNK=1
GROUP BY 
1,2,3,4
)
WITH DATA PRIMARY INDEX(ID,KEY1) ON COMMIT PRESERVE ROWS;

转至表 1 获取与上述步骤中的 IDS 关联的最新 KEY1S 第 2 步:如果存在多个最大日期,请从上一步中的所有匹配记录中取出最大 ID

SELECT                                      
TABLE1.ID ,
TABLE1.KEY1,
TABLE1.STRT_DT,
TABLE1.TERM_DT,                             
RANK() OVER (PARTITION BY TABLE1.KEY1 ORDER BY TABLE1.STRT_DT DESC , TABLE1.ID DESC) AS RNK                 
FROM  TABLE1    
INNER JOIN TEST_VT                                      
ON TEST_VT.ID=TABLE1.ID 
INNER JOIN TABLE3                   
ON TABLE3.KEY1=TABLE1.KEY1    
WHERE  TABLE1.TERM_DT='8888-12-31'  
QUALIFY RNK=1 
GROUP BY    
1,2,3,4

If more than one ID for a match select id with max(date) .
if more than one id for above max dates select max(ID)
Go to TABLE1 to get high-dated IDS

Step 1 : identifying IDs which are having max dates

CREATE MULTISET VOLATILE TABLE TEST_VT 
AS 
(                                       
SELECT
TABLE1.ID ,
TABLE1.KEY1,
TABLE1.STRT_DT,
TABLE1.TERM_DT,
Rank() OVER (PARTITION BY  TABLE1.KEY1 ORDER BY TABLE1.STRT_DT DESC , TABLE1.ID DESC) RNK
FROM TABLE2          
INNER JOIN TABLE1         
ON TABLE2.KEY1=TABLE1.KEY1    
WHERE  TABLE1.TERM_DT='8888-12-31'
QUALIFY RNK=1
GROUP BY 
1,2,3,4
)
WITH DATA PRIMARY INDEX(ID,KEY1) ON COMMIT PRESERVE ROWS;

Go to TABLE1 to get high-dated KEY1S associated with IDS from ABOVE STEP
Step 2 : If there is more than one max date take max ID out of all matched records in above step

SELECT                                      
TABLE1.ID ,
TABLE1.KEY1,
TABLE1.STRT_DT,
TABLE1.TERM_DT,                             
RANK() OVER (PARTITION BY TABLE1.KEY1 ORDER BY TABLE1.STRT_DT DESC , TABLE1.ID DESC) AS RNK                 
FROM  TABLE1    
INNER JOIN TEST_VT                                      
ON TEST_VT.ID=TABLE1.ID 
INNER JOIN TABLE3                   
ON TABLE3.KEY1=TABLE1.KEY1    
WHERE  TABLE1.TERM_DT='8888-12-31'  
QUALIFY RNK=1 
GROUP BY    
1,2,3,4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文