问题描述
我想使用不重复计数来应用fby,即
从t中选择,其中1 =(不重复计数;列)fby another_column
我该怎么做?
解决方法
要添加到已有的内容中,您可以使用@ apply来用较少的字符来完成
q)n:100
// Create a table where all entries for sym=`a are size 10
q)show t:update size:10 from ([]sym:n#`a`b`c`d;size:n?200) where sym = `a
sym size
--------
a 10
b 28
c 51
d 64
a 10
b 43
...
// Use count distinct@ to select from the table as per your requirements
q)select from t where 1=(count distinct@;size) fby sym
sym size
--------
a 10
a 10
a 10
a 10
a 10
0N!
是一个很好的运算符,可以在运行中检查这些查询的操作
使用它,我们可以看到count distinct
本身就失败了,因为它试图对返回值1的函数distinct
进行计数
q)select from t where 1=(0N!count distinct; size) fby sym
1
'type
[0] select from t where 1=(0N!count distinct; size) fby sym
但是使用二进位@可以创建方便的投影
q)select from t where 1=(0N!(count distinct@); size) fby sym
#@[?:]
sym size
--------
a 10
a 10
a 10
a 10
...
在这里,我不得不在这里使用方括号来包装以防止出现0N!被count distinct @
投影所吸引。用k讲,这有效地转化为“计算应用于@的第二个参数的唯一运算符的结果”。非常方便进行代码查询
确定是否提供示例表和所需的输出会更容易些,但是以下其中一项可能会有所帮助。
最简单的解决方案是使用匿名函数:
select from t where 1=({count distinct x};c1) fby c2
或者,您可以使用在尼克·普萨里斯(Nick Psaris)的新书《趣味Q》(Fun Q :)中首次使用的语法:
select from t where 1=(count distinct ::;c1) fby c2