ORACLE SQL 获取连续28天有销售的店铺
最近公司一SSRS报表需求改变,同比店铺的选取为连续28天有销售的店铺,思考良久,发现可以利用ROW_NUMBER() OVER(PARTITION BY COLUMN01 ORDER BY COLUMN02)来实现,ROW_NUMBER()从1开始,按照COLUMN01分组以及COLUMN02排序.
1:先看数据源,要求取出上面连续28天内销售金额大于0的XF_STORECODE :
2: 分解一下,由图中看出,只要取XF_STORECODE,COUNT(1)>28就可以了.
3: 完整代码:
4: 文本代码
1 WITH STORE_COMP AS 2 (SELECT T.XF_STORECODE, T.XF_TXDATE, SUM(T.XF_AMTSOLD) XF_AMTSOLD 3 FROM XF_VIPITEMDM T, XF_ITEMMAS T1 4 WHERE T.XF_PLU = T1.XF_PLU 5 AND SUBSTR(T.XF_STORECODE, 3, 2) IN (‘NK‘, ‘SC‘, ‘NS‘, ‘MB‘) 6 AND T1.XF_GROUP2 = ‘NIKE‘ 7 AND T1.XF_GROUP9 <> ‘特许装备‘ 8 AND T.XF_TXDATE >= TO_DATE(‘20140501‘, ‘yyyymmdd‘) 9 AND T.XF_TXDATE <= TO_DATE(‘20140531‘, ‘yyyymmdd‘) 10 GROUP BY T.XF_STORECODE, T.XF_TXDATE 11 HAVING SUM(T.XF_AMTSOLD) > 0) 12 SELECT XF_STORECODE 13 FROM (SELECT XF_STORECODE, 14 XF_TXDATE, 15 ROW_NUMBER() OVER(PARTITION BY XF_STORECODE ORDER BY XF_TXDATE) ROW_NUM, 16 TRUNC(XF_TXDATE - ROW_NUMBER() 17 OVER(PARTITION BY XF_STORECODE ORDER BY XF_TXDATE)) ROWDIFF 18 FROM STORE_COMP) 19 GROUP BY XF_STORECODE, ROWDIFF 20 HAVING COUNT (1) >= 28
5: 如有更好的方法,欢迎赐教,谢谢!
6: 本人欲建一个运动行业的网站,欢迎有网页制作技术的朋友利用业余时间一起合作. 有兴趣的可以和我联系QQ 568625626.
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。