英文:
Find the current and previous designation of a Employee in q/kdb+
问题
Here is the translation of the provided text:
找到当前和先前的指定员工,通过比较当前日期与先前的更新日期
flip dc:`eid`name`titleID`title`lastupdate!(("1234" ;"1234"; "1234" ;"9835" ;"9835"; "9835"; "0982");("Jhon";"Jhon";"Jhon";"Chris";"Chris";"Chris";"jack");("09561";"08123";"08123" ;"07812"; "05234"; "05234"; "08123");("SalesManager";"Salesexecutive";"Salesexecutive";"SeniorSalesExecutive";"Salesexecutive";"Salesexecutive";"salesmanager");2023.06.26 2023.06.25 2023.06.20 2023.06.26 2023.06.25 2022.06.20 2022.01.19)
员工表格
员工ID | 姓名 | 职务ID | 职务 | 最后更新日期 |
---|---|---|---|---|
"1234" | "Jhon" | "09561" | "销售经理" | 2023.06.26 |
"1234 " | "Jhon" | "08123" | "销售执行官" | 2023.06.25 |
"1234" | "Jhon" | "08123" | "销售执行官" | 2022.06.20 |
"9835" | "Chris" | "07812" | "高级销售执行官" | 2023.06.26 |
"9835" | "Chris" | "05234" | "销售执行官" | 2023.06.25 |
"9835" | "Chris" | "05234" | "销售执行官" | 2022.06.20 |
"0982" | "Jack" | "08123" | "销售经理" | 2022.01.19 |
查询select from EmployeeTAB where lastupdate<td,lastupdate=(max;lastupdate) fby eID;
结果显示最新的更新结果
输出
员工ID | 姓名 | 职务ID | 职务 | 最后更新日期 |
---|---|---|---|---|
"1234" | "Jhon" | "09561" | "销售经理" | 2023.06.26 |
"1234" | "Jhon" | "08123" | "销售执行官" | 2023.06.25 |
"9835" | "Chris" | "07812" | "高级销售执行官" | 2023.06.26 |
"9835" | "Chris" | "05234" | "销售执行官" | 2023.06.25 |
英文:
Find the current and previous designation employees by comparing current date with previous updated
flip dc:`eid`name`titleID`title`lastupdate!(("1234" ;"1234"; "1234" ;"9835" ;"9835"; "9835"; "0982");("Jhon";"Jhon";"Jhon";"Chris";"Chris";"Chris";"jack");("09561";"08123";"08123" ;"07812"; "05234"; "05234"; "08123");("SalesManager";"Salesexecutive";"Salesexecutive";"SeniorSalesExecutive";"Salesexecutive";"Salesexecutive";"salesmanager");2023.06.26 2023.06.25 2023.06.20 2023.06.26 2023.06.25 2022.06.20 2022.01.19)
EmployeeTAB
eid | name | titleID | title | lastupdate |
---|---|---|---|---|
"1234" | "Jhon" | "09561" | "Sales Manager" | 2023.06.26 |
"1234 " | "Jhon" | "08123" | "Sales executive" | 2023.06.25 |
"1234" | "Jhon" | "08123" | "Sales executive" | 2022.06.20 |
"9835" | "Chris" | "07812" | "Senior Sales executive" | 2023.06.26 |
"9835" | "Chris" | "05234" | "Sales executive" | 2023.06.25 |
"9835" | "Chris" | "05234" | "Sales executive" | 2022.06.20 |
"0982" | "Jack" | "08123" | "Sales Manager" | 2022.01.19 |
Query select from EmployeeTAB where lastupdate<td,lastupdate=(max;lastupdate) fby eID;
results the latest updated result
output
eID | name | titleID | title | lastupdate |
---|---|---|---|---|
"1234" | "Jhon" | "09561" | "Sales Manager" | 2023.06.26 |
"1234" | "Jhon" | "08123" | "Sales executive" | 2023.06.25 |
"9835" | "Chris" | "07812" | "Senior Sales executive" | 2023.06.26 |
"9835" | "Chris" | "05234" | "Sales executive" | 2023.06.25 |
答案1
得分: 2
删除仅有一个角色的员工使用 fby
。
将剩余员工按照ID和最新日期排序,并在 eid 上进行分组。
然后只需提取前两个条目并取消分组。
`eid xasc ungroup 2#''`eid xgroup`eid`lastupdate xdesc select from employeeTAB where 1<(count;i)fby eid
英文:
Remove employees with only 1 role using fby
.
Sort remaining by id and latest date and group the table on eid.
Then just take
the first 2 entries and ungroup
`eid xasc ungroup 2#''`eid xgroup`eid`lastupdate xdesc select from employeeTAB where 1<(count;i)fby eid
答案2
得分: 1
以下是已翻译的内容:
要实现所期望的结果的一种方法是:
- 获取
eid
的参考表和最近的两个lastupdate
- 通过
eid
和lastupdate
将参考表与其他数据连接
像下面这样:
(ungroup select -2#asc lastupdate by eid from EmployeeTAB)
ij `eid`lastupdate xkey EmployeeTAB
删除只有一个角色的员工会使查询更加复杂:
delete from distinct (
(ungroup select -2#asc lastupdate by eid from EmployeeTAB)
ij `eid`lastupdate xkey EmployeeTAB
) where 1=(count;eid) fby eid
英文:
One way to achieve desired result is to
- Get reference table of
eid
s and two most recentlastupdate
s - Join reference table with other data by
eid
andlastupdate
Like below
(ungroup select -2#asc lastupdate by eid from EmployeeTAB)
ij `eid`lastupdate xkey EmployeeTAB
Deleting employees, who only had one role makes query more complicated:
delete from distinct (
(ungroup select -2#asc lastupdate by eid from EmployeeTAB)
ij `eid`lastupdate xkey EmployeeTAB
) where 1=(count;eid) fby eid
答案3
得分: 0
以下是代码的翻译部分:
另一种方法(假设数据按照最后更新时间排序,与示例一致):
q) 根据 eid 分组选择 EmployeeTAB where ({(1 < count x) & x & not 2 < sums x}; lastupdate <= .z.D) fby eid
eid name titleID title lastupdate
--------------------------------------------------------
"1234" "Jhon" "09561" "SalesManager" 2023.06.26
"1234" "Jhon" "08123" "Salesexecutive" 2023.06.25
"9835" "Chris" "07812" "SeniorSalesExecutive" 2023.06.26
"9835" "Chris" "05234" "Salesexecutive" 2023.06.25
q) 根据 eid 分组选择 EmployeeTAB where ({(1 < count x) & x & not 2 < sums x}; lastupdate <= 2023.06.25) fby eid
eid name titleID title lastupdate
--------------------------------------------------
"1234" "Jhon" "08123" "Salesexecutive" 2023.06.25
"1234" "Jhon" "08123" "Salesexecutive" 2023.06.20
"9835" "Chris" "05234" "Salesexecutive" 2023.06.25
"9835" "Chris" "05234" "Salesexecutive" 2022.06.20
q)
英文:
Another approach (assumes data is sorted by lastupdate as per the sample):
q)select from EmployeeTAB where({(1<count x)&x&not 2<sums x};lastupdate<=.z.D)fby eid
eid name titleID title lastupdate
--------------------------------------------------------
"1234" "Jhon" "09561" "SalesManager" 2023.06.26
"1234" "Jhon" "08123" "Salesexecutive" 2023.06.25
"9835" "Chris" "07812" "SeniorSalesExecutive" 2023.06.26
"9835" "Chris" "05234" "Salesexecutive" 2023.06.25
q)select from EmployeeTAB where({(1<count x)&x&not 2<sums x};lastupdate<=2023.06.25)fby eid
eid name titleID title lastupdate
--------------------------------------------------
"1234" "Jhon" "08123" "Salesexecutive" 2023.06.25
"1234" "Jhon" "08123" "Salesexecutive" 2023.06.20
"9835" "Chris" "05234" "Salesexecutive" 2023.06.25
"9835" "Chris" "05234" "Salesexecutive" 2022.06.20
q)
答案4
得分: 0
res:(select from employeeTab where lastupdate<=tradingDate,lastupdate=(max;lastupdate) fby eid) lj 1! select eid,prevTitleID:titleID,prevTitle:title,preLastupdate:lastupdate from employeeTAB where lastupdate<=tradingDate,lastupdate=(max;lastupdate) fby eid
select from res where titleID<>prevTitle, not prevtitle like ""
英文:
res:(select from employeeTab where lastupdate<=tradingDate,lastupdate=(max;lastupdate) fby eid) lj 1! select eid,prevTitleID:titleID,prevTitle:title,preLastupdate:lastupdate from employeeTAB where lastupdate<=tradingDate,lastupdate=(max;lastupdate) fby eid
select from res where titleID<>prevTitle, not prevtitle like ""
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论