sql - How to lag() the previous data of specific category only?
问题描述
Sample date:
Date Type Revenue
2019/01/01 A 100
2019/02/01 A 200
2019/03/01 A 200
2019/01/01 B 100
2019/02/01 B 50
2019/01/01 C 500
2019/02/01 C 600
I want to know how many percentage the revenue increase or decrease comparing to the last month. Here is my sample code
select to_date(date, 'yyyy-mm') as date, type, sum(revenue) as revenue
lag(revenue, 1) over (order by service_type, date) as last_month
round(((revenue - last_month::float)/last_month)::float*100,2) as percentage_growth
from #a
group by 1,2
order by 2,1;
The result of my code
Date Type Revenue Last_month Percentage_growth
2019/01/01 A 100 None None
2019/02/01 A 200 100 100%
2019/03/01 A 200 200 100%
2019/01/01 B 100 200 -100% --Here is the matter
2019/02/01 B 50 100 -50%
2019/01/01 C 500 50 900% --Here is the matter
2019/02/01 C 600 500 20%
As you already seen, in the new type, lag()
function still fetch the previous revenue of different type.
How to deal with it many thanks.
解决方案
您似乎正在联合使用聚合函数和窗口函数。鉴于您想返回原始表,您应该在这里只使用窗口函数。这是您的查询的工作版本:
WITH cte AS (
SELECT *,
LAG(revenue, 1) OVER (ORDER BY Type, Date) AS last_month
FROM a
)
SELECT *,
100.0 * (revenue - COALESCE(last_month, revenue)) / COALESCE(last_month, revenue) AS pct_growth
FROM cte;
演示
推荐阅读
- python-3.x - 计算文件中给定索引下单词的频率
- swift - iOS 12 SDK 通用函数返回 Optional.some(nil)
- python-3.x - 如何使用 Beautiful Soup 从具有特定类的标签中获取字符串
- angularjs - Video.js onclick 事件将屏幕扩展到全屏模式并取消静音
- c# - 通过触发 Xamarin 表单修改按钮背景
- c# - 无法加载文件或程序集“GalaSoft.MvvmLight.Platform,PublicKeyToken=null”或其依赖项之一
- sql - 将数据类型 Int 转换为 Varchar SQL Server 2016
- java - 如何用替换/正则表达式替换字符串中的两个字符?
- hibernate - 如何通过 Hibernate 在生成的 sql 中为别名添加引号?
- reactjs - 我的 mapStateToProps 没有将道具传递给组件