Delete smaller duplicate

In KDB, I have the following table:

q)tab:flip `items`sales`prices!(`nut`bolt`cam`cog`bolt`screw;6 8 0 3 0n 0n;10  20 15 20 0n 0n)
q)tab

items sales prices
------------------
nut   6     10
bolt  8     20
cam   0     15
cog   3     20
bolt
screw

This table has 2 duplicate elements (bolt). However, since the first "bolt" contains more information. I would like to remove the "smaller" bolt.

FINAL RESULT:

items sales prices
------------------
nut   6     10
bolt  8     20
cam   0     15
cog   3     20
screw

As far as I understand, if I used the "excellent" function, it is not deterministic?

+4
source share
2 answers

One way to do this is to fill in by the element, then boltinherit the previous values.

q)update fills sales,fills prices by items from tab
items sales prices
------------------
nut   6     10
bolt  8     20
cam   0     15
cog   3     20
bolt  8     20
screw

This can also be done in functional form, where you can pass the table and columns by:

{![x;();(!). 2#enlist(),y;{x!fills,/:x}cols[x]except y]}[tab;`items]

" " " ", , :

q)select from @[tab;`n;:;sum each null tab] where n=(min;n)fby items
items sales prices n
--------------------
nut   6     10     0
bolt  8     20     0
cam   0     15     0
cog   3     20     0
screw              2

, , .

+4

, .

, " ". , , , :

q)delete from tab where null sales
items sales prices
------------------
nut   6     10    
bolt  8     20    
cam   0     15    
cog   3     20    

q)select from tab where (sales*prices) = (max;sales*prices) fby items
items sales prices
------------------
nut   6     10    
bolt  8     20    
cam   0     15    
cog   3     20    
+2

Source: https://habr.com/ru/post/1695442/


All Articles