我有一个表,其中包含客户的销售数据,如下所示;
|CustomerCode|SalesDate|Profit
|123455666 |2018-06 |120
|123455666 |2018-06 |100
|123455666 |2018-05 |10
|123455666 |2018-04 |60
|666452342 |2018-06 |900
|666452342 |2018-05 |1000
|666452342 |2018-05 |900
|666452342 |2018-06 |800
我想要一个表,显示公司三个月的利润。预期表;
|CustomerCode|P_This_Month|P_1_Month_Ago|P_2_Month_Ago
|123455666 |220 |10 |60
|666452342 |900 |1900 |800
做到这一点的最佳方法是什么?目前,我正在使用JOIN运算符。但是,即使它适用于第一次联接,也不适用于第二次联接(获得了意外的高值)。
我该怎么做?
SELECT
This_Month.*,
SUM(_1_Month_Ago.UCSALES) sales_1_month_ago,
SUM(_2_Months_Ago.UCSALES) sales_2_months_ago
FROM
SalesTable This_Month
LEFT JOIN
SalesTable _1_Month_Ago ON This_Month.CustomerCode = _1_Month_Ago AND DATE_SUB(This_Month.SalesDate, INTERVAL 1 MONTH) = _1_Month_Ago.SalesDate
LEFT JOIN
SalesTable _2_Months_Ago ON This_Month.CustomerCode = _2_Months_Ago AND DATE_SUB(This_Month.SalesDate, INTERVAL 1 MONTH) = _2_Months_Ago.SalesDate
GROUP BY
This_Month.CustomerCode
答案 0 :(得分:2)
您可以通过条件聚合来做到这一点。
select
CustomerCode,
sum(case when sales_date=current_date then profit else 0 end) P_This_Month,
sum(case when sales_date=date_sub(current_date, INTERVAL 1 month) then profit else 0 end) P_1_Month,
sum(case when sales_date=date_sub(current_date, INTERVAL 2 month) then profit else 0 end) P_2_Month,
from <tableName>
group by CustomerCode
答案 1 :(得分:1)
您可以在从日期中提取月份的情况下使用案例
with cte as
(
select EXTRACT(month FROM SalesDate) as monthofday,CustomerCode,sum(Profit) as total
from tbale
) select CustomerCode,
sum(case when monthofday=4 then total else 0 end) as Forththmonthtotal,
sum(case when monthofday=5 then total else 0 end) as fifththmonthtotal,
--you can apply for 12 month this logic
from cte
group by CustomerCode
答案 2 :(得分:0)
以下是用于BigQuery标准SQL
此解决方案避免进行自我联接,并且通用性足以覆盖多个月的数据
#standardSQL
WITH months AS (
SELECT FORMAT_DATE('%Y-%m', month) month
FROM (
SELECT
MIN(PARSE_DATE('%Y-%m', SalesDate)) min_month,
MAX(PARSE_DATE('%Y-%m', SalesDate)) max_month
FROM `project.dataset.table`
), UNNEST(GENERATE_DATE_ARRAY(min_month, max_month, INTERVAL 1 MONTH)) month
)
SELECT CustomerCode, month,
SUM(IF(diff = 0, P_This_Month, 0)) P_This_Month,
SUM(IF(diff = 1, P_This_Month, 0)) P_1_Month_Ago,
SUM(IF(diff = 2, P_This_Month, 0)) P_2_Month_Ago
FROM (
SELECT CustomerCode, P_This_Month, PARSE_DATE('%Y-%m', month) month,
DATE_DIFF(PARSE_DATE('%Y-%m', month), PARSE_DATE('%Y-%m', SalesDate), MONTH) diff
FROM months m CROSS JOIN (
SELECT CustomerCode, SalesDate, SUM(Profit) P_This_Month
FROM `project.dataset.table`
GROUP BY CustomerCode, SalesDate
) t
)
WHERE diff BETWEEN 0 AND 2
GROUP BY CustomerCode, month
您可以使用问题中的示例数据来测试,玩游戏,如下例所示
#standardSQL
WITH `project.dataset.table` AS (
SELECT 123455666 CustomerCode, '2018-06' SalesDate, 120 Profit UNION ALL
SELECT 123455666, '2018-06', 100 UNION ALL
SELECT 123455666, '2018-05', 10 UNION ALL
SELECT 123455666, '2018-04', 60 UNION ALL
SELECT 666452342, '2018-06', 900 UNION ALL
SELECT 666452342, '2018-05', 1000 UNION ALL
SELECT 666452342, '2018-05', 900 UNION ALL
SELECT 666452342, '2018-04', 800
), months AS (
SELECT FORMAT_DATE('%Y-%m', month) month
FROM (
SELECT
MIN(PARSE_DATE('%Y-%m', SalesDate)) min_month,
MAX(PARSE_DATE('%Y-%m', SalesDate)) max_month
FROM `project.dataset.table`
), UNNEST(GENERATE_DATE_ARRAY(min_month, max_month, INTERVAL 1 MONTH)) month
)
SELECT CustomerCode, month,
SUM(IF(diff = 0, P_This_Month, 0)) P_This_Month,
SUM(IF(diff = 1, P_This_Month, 0)) P_1_Month_Ago,
SUM(IF(diff = 2, P_This_Month, 0)) P_2_Month_Ago
FROM (
SELECT CustomerCode, P_This_Month, PARSE_DATE('%Y-%m', month) month,
DATE_DIFF(PARSE_DATE('%Y-%m', month), PARSE_DATE('%Y-%m', SalesDate), MONTH) diff
FROM months m CROSS JOIN (
SELECT CustomerCode, SalesDate, SUM(Profit) P_This_Month
FROM `project.dataset.table`
GROUP BY CustomerCode, SalesDate
) t
)
WHERE diff BETWEEN 0 AND 2
GROUP BY CustomerCode, month
-- ORDER BY CustomerCode, month
有结果
Row CustomerCode month P_This_Month P_1_Month_Ago P_2_Month_Ago
1 123455666 2018-04-01 60 0 0
2 123455666 2018-05-01 10 60 0
3 123455666 2018-06-01 220 10 60
4 666452342 2018-04-01 800 0 0
5 666452342 2018-05-01 1900 800 0
6 666452342 2018-06-01 900 1900 800