Find the current and previous designation of an Employee in q/kdb+.

huangapple go评论59阅读模式
英文:

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#&#39;&#39;`eid xgroup`eid`lastupdate xdesc select from employeeTAB where 1&lt;(count;i)fby eid

答案2

得分: 1

以下是已翻译的内容:

要实现所期望的结果的一种方法是:

  1. 获取eid的参考表和最近的两个lastupdate
  2. 通过eidlastupdate将参考表与其他数据连接

像下面这样:

(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

  1. Get reference table of eids and two most recent lastupdates
  2. Join reference table with other data by eid and lastupdate

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&lt;count x)&amp;x&amp;not 2&lt;sums x};lastupdate&lt;=.z.D)fby eid
eid    name    titleID title                  lastupdate
--------------------------------------------------------
&quot;1234&quot; &quot;Jhon&quot;  &quot;09561&quot; &quot;SalesManager&quot;         2023.06.26
&quot;1234&quot; &quot;Jhon&quot;  &quot;08123&quot; &quot;Salesexecutive&quot;       2023.06.25
&quot;9835&quot; &quot;Chris&quot; &quot;07812&quot; &quot;SeniorSalesExecutive&quot; 2023.06.26
&quot;9835&quot; &quot;Chris&quot; &quot;05234&quot; &quot;Salesexecutive&quot;       2023.06.25

q)select from EmployeeTAB where({(1&lt;count x)&amp;x&amp;not 2&lt;sums x};lastupdate&lt;=2023.06.25)fby eid
eid    name    titleID title            lastupdate
--------------------------------------------------
&quot;1234&quot; &quot;Jhon&quot;  &quot;08123&quot; &quot;Salesexecutive&quot; 2023.06.25
&quot;1234&quot; &quot;Jhon&quot;  &quot;08123&quot; &quot;Salesexecutive&quot; 2023.06.20
&quot;9835&quot; &quot;Chris&quot; &quot;05234&quot; &quot;Salesexecutive&quot; 2023.06.25
&quot;9835&quot; &quot;Chris&quot; &quot;05234&quot; &quot;Salesexecutive&quot; 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&lt;=tradingDate,lastupdate=(max;lastupdate) fby eid) lj 1! select eid,prevTitleID:titleID,prevTitle:title,preLastupdate:lastupdate from employeeTAB where lastupdate&lt;=tradingDate,lastupdate=(max;lastupdate) fby eid

select from res where titleID&lt;&gt;prevTitle, not prevtitle like &quot;&quot;

huangapple
  • 本文由 发表于 2023年6月26日 14:46:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554146.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定