【SQL干货】一条sql按季度统计交易数据
-- 【SQL干货】按季度统计交易数据
-- 按季度统计交易数据
-- by the way, "quarter" means 1/4, here equals season.
SELECT CONCAT(LEFT(yyyyMM,4),'Q',CEILING( RIGHT(yyyyMM,2) /3)) AS 'QUARTER', SUM(amt)
FROM (
SELECT 202101 AS yyyyMM, 1 AS amt UNION ALL
SELECT 202102, 1 UNION ALL
SELECT 202103, 1 UNION ALL
SELECT 202104, 1 UNION ALL
SELECT 202105, 1 UNION ALL
SELECT 202106, 1 UNION ALL
SELECT 202107, 1 UNION ALL
SELECT 202108, 1 UNION ALL
SELECT 202109, 1 UNION ALL
SELECT 202110, 1 UNION ALL
SELECT 202111, 1 UNION ALL
SELECT 202112, 1 UNION ALL
SELECT 202012, 1000
) a
GROUP BY CONCAT(LEFT(yyyyMM,4),'Q',CEILING( RIGHT(yyyyMM,2) /3))
当看到一些不好的代码时,会发现我还算优秀;当看到优秀的代码时,也才意识到持续学习的重要!--buguge
本文来自博客园,转载请注明原文链接:https://www.cnblogs.com/buguge/p/15842228.html