buguge - Keep it simple,stupid

知识就是力量,但更重要的,是运用知识的能力why buguge?

导航

【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))

 

 

posted on 2022-01-25 11:18  buguge  阅读(701)  评论(0编辑  收藏  举报