如何删除具有重复值的行?

时间:2019-11-07 11:16:18

标签: q kdb

我在kdb中有一个数据表,我想用q删除一行中包含重复值的行。

例如,如果我有下表,其中“年龄”(Age)列中有重复的值:

    Name    Age    Degree
    ---------------------
    Alice   26     Science
    Bob     34     Arts
    Carrie  26     Engineering

如何删除第三行,所以我得到以下内容:

    Name    Age    Degree
    ---------------------
    Alice   26     Science
    Bob     34     Arts

谢谢!

2 个答案:

答案 0 :(得分:5)

你可以做

select from t where i=(first;i)fby Age

答案 1 :(得分:3)

您可以使用以下方法删除任何列中的任何重复项:

q)delete from t where ({not x in 1#x};i) fby Age
Name  Age Degree
-----------------
Alice 26  Science
Bob   34  Arts

也可以使用by子句代替fby来解决,但是在这种情况下,要获得每个年龄段的首次出现,您必须使用reverse

q)0!select by Age from reverse t
Age Name  Degree
-----------------
26  Alice Science
34  Bob   Arts
相关问题