如何解决数据扩展
我有一个表格,其中包含长达 40 个月的销售数据。然而,并非所有记录都可以达到 40 个月,有些已经成熟到 20 个月。无论如何我可以创建额外的记录以达到 40 个月吗?我的表中有一个主键,可用于识别哪些记录没有 60 个月的数据。我正在使用 qlikview 并通过加载语句加载数据,谢谢
解决方法
这个想法是
- 确定缺少的日期(针对每个客户/产品/或您的任何数据)
- 为缺失的日期生成数据
- 将生成的数据连接回原始表
查看下面的脚本示例(在脚本本身中添加一些注释)
如果您需要在示例中使用 qvf,请查看 this repo
RawData:
Load * Inline [
ClientId,Date,Value
1,01/01/2021,1
1,02/01/2021,2
1,03/01/2021,3
2,04/01/2021,4
2,05/01/2021,5
3,06/01/2021,6
3,07/01/2021,7
3,08/01/2021,8
];
// Get min and max possible dates from the "transactions" table
Dates:
Load
Min(Date) as MinDate,Max(Date) as MaxDate
Resident
RawData
;
// set the min and max dates as variables
let vMinDate = num(peek('MinDate'));
let vMaxDate = num(peek('MaxDate'));
// we dont need this table anymore
Drop Table Dates;
// generate table with all possible values
// between the min and max dates
TempCalendar:
Load
Date($(vMinDate) + recno () - 1,'DD/MM/YYYY' ) as TempDate
Autogenerate
(num($(vMaxDate)) - num($(vMinDate)) + 1)
;
// join the clientId and the dates
// this will create all possible combinations
// between clientId and dates
CliendDates_Temp:
Load distinct
ClientId
Resident
RawData
;
join
Load
TempDate
Resident
TempCalendar
;
// we dont need this table anymore
Drop Table TempCalendar;
// join the transactions data with the table containing all
// client <-> dates link
// flag the client <-> dates records (RawDataSource fiels)
CliendDates_Temp1:
Load
ClientId,TempDate as Date
Resident
CliendDates_Temp
;
join
Load distinct
ClientId,1 as RawDataSource
Resident
RawData
;
// we dont need this table anymore
Drop Table CliendDates_Temp;
// load only the records that do not exists in the "transactions" table
// add 0 as Value for them#
// concatenate the result to the transactions table
Concatenate (RawData)
Load
ClientId,0 as Value
Resident
CliendDates_Temp1
Where
RawDataSource <> 1
;
// we dont need this table anymore
Drop Table CliendDates_Temp1;
/*
At this point the original/transactions table will containt the
initial data + all the "gap" dates for each client but with Value = 0
ClientId,Value
1,1
1,2
1,3
1,0
1,0
...
/*
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。