滞后 SQL 命令的替代方案
我有一张桌子,里面有一张这样的桌子。
Month-----Book_Type-----sold_in_Dollars
Jan----------A------------ 100
Jan----------B------------ 120
Feb----------A------------ 50
Mar----------A------------ 60
Mar----------B------------ 30
依此类推,
我必须根据最近 2 个月的销售额计算每个月和书籍类型的预期销售额。 因此对于 3 月和类型 A 来说,它将是 (100+50)/2 = 75 对于 3 月和类型 B,该值为 120/1,因为没有 2 月的数据。
我试图使用滞后函数,但它不起作用,因为几行中缺少数据。
对此有什么想法吗?
I have a table which has a table like this.
Month-----Book_Type-----sold_in_Dollars
Jan----------A------------ 100
Jan----------B------------ 120
Feb----------A------------ 50
Mar----------A------------ 60
Mar----------B------------ 30
and so on
I have to calculate the expected sales for each month and book type based on the last 2 months sales.
So for March and type A it would be (100+50)/2 = 75
For March and type B it is 120/1 since no data for Feb is there.
I was trying to use the lag function but it wouldn't work since there is data missing in a few rows.
Any ideas on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于它计划忽略缺失值,因此这应该可行。目前没有数据库来测试它,但会在早上再试一次
这种假设月份有一个日期数据类型并且可以排序......如果它只是一个文本字符串那么你会需要别的东西。
通常,您可以只使用
前 2 行和前 1 行之间的行
,但这将采用之前的两个数据点,如果缺少行,则不一定是前两个月的数据点。你可以用滞后来解决这个问题,但会更复杂一些。
Since it plans to ignore missing values, this should probably work. Don't have a database to test it on at the moment but will give it another go in the morning
This sort of assumes that month has a date datatype and can be sorted on... if it's just a text string then you'll need something else.
Normally you could just use
rows between 2 preceding and 1 preceding
but but this will take the two previous data points and not necessarily the two previous months if there are rows missing.You could work it out with lag but it would be a bit more complicated.
据我所知,您可以为 lag() 指定默认值:(
假设 Month 列并不真正包含 JAN 或 FEB,而是真实的、可订购的日期。)
As far as I know, you can give a default value to lag() :
(Assuming Month column doesn't really contain JAN or FEB but real, orderable dates.)
怎么样(请原谅 sql server 语法,但你明白了):
What about something like (forgive the sql server syntax, but you get the idea):
分区外连接可以帮助创建丢失的数据。创建一组月份,然后按月份将这些值连接到每一行,并为每种图书类型执行一次连接。我在此示例中创建了一月到四月:
A partition outer join can help create the missing data. Create a set of months and join those values to each row by the month and perform the join once for each book type. I created the months January through April in this example: