英文:
how do we append some constant text to a symbol and string column? Finally we should have a list of symbols and a list of string
问题
我有一个表格,示例如下:
n: 30; // 表格中的行数
tab:([] date: .z.d - n?30; ranks: n?100; sym: n?`AAPL`MSFT`GOOGL; group_name_symbol:n?`US_HOLIDAY`SPCL_CAL_DATE ; group_name_string:n?("FIRST_3rd_FRI";"QTR_3rd_FRI";"FIRST_6rd_FRI") ;price: "f"$n?100.5; recv_time: n?10:00:00.000 + n?10000000; is_active: "b"$n?1 0);
tab:update group_name_symbol:` from tab where sym=`AAPL;
tab:update group_name_string:` from tab where sym=`GOOGL;
现在我想要向列group_name_symbol和group_name_string中添加一些固定文本。
并且我希望在需要时不断向该列追加新的固定文本。因此,对于不同的固定文本,需要新的更新语句。
因此,当我运行单独的更新查询时,最终输出将如下所示:
update group_name_symbol:`CSMT from tab where ranks=92 // 追加而不是覆盖
update group_name_string:`THN from tab where ranks=19 // 追加而不是覆盖
update group_name_string:`KRL from tab where ranks=19 // 追加而不是覆盖
英文:
I have a table say example below
n: 30; // Number of rows in the table
tab:([] date: .z.d - n?30; ranks: n?100; sym: n?`AAPL`MSFT`GOOGL; group_name_symbol:n?`US_HOLIDAY`SPCL_CAL_DATE ; group_name_string:n?("FIRST_3rd_FRI";"QTR_3rd_FRI";"FIRST_6rd_FRI") ;price: "f"$n?100.5; recv_time: n?10:00:00.000 + n?10000000; is_active: "b"$n?1 0);
tab:update group_name_symbol:` from tab where sym=`AAPL;
tab:update group_name_string:` from tab where sym=`GOOGL;
Now I want append some fixed text to the column group_name_symbol and group_name_string.
And I want to keep appending new fixed text evetime to the column whenever required. So new update statement for different fixed text.
so the final output will look like when i run separate update query
update group_name_symbol:`CSMT from tab where ranks=92 //append and not override
update group_name_string:`THN from tab where ranks=19 //append and not override
update group_name_string:`KRL from tab where ranks=19 //append and not override
答案1
得分: 1
将您的符号原子列转换为嵌套符号列,首先需要执行以下操作:
update enlist each group_name_symbol from `tab
然后您可以执行以下操作:
q)update group_name_symbol:(group_name_symbol,'`CSMT) from tab where ranks=87
date ranks sym group_name_symbol group_name_string price recv_tim..
-----------------------------------------------------------------------------
2023.06.16 87 GOOGL `US_HOLIDAY`CSMT ` 55.25425 11:32:21..
2023.06.06 63 GOOGL ,`US_HOLIDAY ` 19.68259 10:38:48..
对于您的字符串列,您应该这样设置空白:
tab:update group_name_string:count[i]#enlist"" from tab where sym=`GOOGL;
然后您可以类似地追加:
q)update enlist each group_name_string from `tab
q)update group_name_string:(group_name_string,:enlist"THN") from tab where ranks=62
date ranks sym group_name_symbol group_name_string price recv..
-----------------------------------------------------------------------------
2023.06.16 87 GOOGL US_HOLIDAY ,"" 55.25425 11:3..
2023.06.06 63 GOOGL US_HOLIDAY ,"" 19.68259 10:3..
2023.06.20 62 MSFT SPCL_CAL_DATE ("QTR_3rd_FRI";"THN") 56.43337 12:4..
2023.05.26 42 GOOGL US_HOLIDAY ,"" 7.079031 11:5..
英文:
To convert your symbol-atoms column into a nested-symbols column you would first have to do:
update enlist each group_name_symbol from `tab
then you could do:
q)update group_name_symbol:(group_name_symbol,'`CSMT) from tab where ranks=87
date ranks sym group_name_symbol group_name_string price recv_tim..
-----------------------------------------------------------------------------..
2023.06.16 87 GOOGL `US_HOLIDAY`CSMT ` 55.25425 11:32:21..
2023.06.06 63 GOOGL ,`US_HOLIDAY ` 19.68259 10:38:48..
For your string column you should be setting blanks like so:
tab:update group_name_string:count[i]#enlist"" from tab where sym=`GOOGL;
Then you can similarly append
q)update enlist each group_name_string from `tab
q)update group_name_string:(group_name_string,\:enlist"THN") from tab where ranks=62
date ranks sym group_name_symbol group_name_string price recv..
-----------------------------------------------------------------------------..
2023.06.16 87 GOOGL US_HOLIDAY ,"" 55.25425 11:3..
2023.06.06 63 GOOGL US_HOLIDAY ,"" 19.68259 10:3..
2023.06.20 62 MSFT SPCL_CAL_DATE ("QTR_3rd_FRI";"THN") 56.43337 12:4..
2023.05.26 42 GOOGL US_HOLIDAY ,"" 7.079031 11:5..
答案2
得分: 1
确保你的列从一开始就是列表的列表,否则你会得到错误。
在定义时添加enlist each
:
q)n: 30; // 表中的行数
q)tab:([] date: .z.d - n?30; ranks: n?100; sym: n?`AAPL`MSFT`GOOGL;
group_name_symbol:enlist each n?`US_HOLIDAY`SPCL_CAL_DATE ;
group_name_string:enlist each n?("FIRST_3rd_FRI";"QTR_3rd_FRI";"FIRST_6rd_FRI") ;
price: "f"$n?100.5; recv_time: n?10:00:00.000 + n?10000000;
is_active: "b"$n?1 0);
同样,在清空行时需要小心,确保有正确的空列表:
q)tab:update group_name_symbol:count[i]#enlist `$() from tab where sym=`AAPL;
q)tab:update group_name_string:count[i]#() from tab where sym=`GOOGL;
q)5 sublist tab
date ranks sym group_name_symbol group_name_string price recv_time is_active
------------------------------------------------------------------------------------------
2023.06.13 76 GOOGL ,`US_HOLIDAY () 7.504439 12:35:24.923 1
2023.05.30 13 MSFT ,`US_HOLIDAY ,"FIRST_3rd_FRI" 38.08953 11:54:05.027 0
2023.05.27 87 MSFT ,`SPCL_CAL_DATE ,"FIRST_3rd_FRI" 21.29572 11:54:05.027 1
2023.05.26 11 AAPL `symbol$() ,"FIRST_3rd_FRI" 33.82582 10:23:06.750 0
2023.06.13 66 AAPL `symbol$() ,"FIRST_6rd_FRI" 52.37666 10:36:25.423 0
q)meta tab
c | t f a
-----------------| -----
date | d
ranks | j
sym | s
group_name_symbol| S // 符号的列表从s --> S
group_name_string| // 字符串的列表从C --> 空
price | f
recv_time | t
is_active | b
在qsql查询中,,
需要包含在 ()
中才能用作追加:
q)tab:update group_name_symbol:group_name_symbol(,)\:`CSMT from tab where ranks=11
q)tab:update group_name_string:group_name_string(,)\:enlist "THN" from tab where ranks=76
q)tab:update group_name_string:group_name_string(,)\:enlist "KRL" from tab where ranks=76
结果:
q)5 sublist tab
date ranks sym group_name_symbol group_name_string price recv_time is_active
------------------------------------------------------------------------------------------
2023.06.13 76 GOOGL ,`US_HOLIDAY ("THN";"KRL") 7.504439 12:35:24.923 1
2023.05.30 13 MSFT ,`US_HOLIDAY ,"FIRST_3rd_FRI" 38.08953 11:54:05.027 0
2023.05.27 87 MSFT ,`SPCL_CAL_DATE ,"FIRST_3rd_FRI" 21.29572 11:54:05.027 1
2023.05.26 11 AAPL ,`CSMT ,"FIRST_3rd_FRI" 33.82582 10:23:06.750 0
2023.06.13 66 AAPL `symbol$() ,"FIRST_6rd_FRI" 52.37666 10:36:25.423 0
英文:
You need to ensure you columns are lists of lists from the start or you will get errors
enlist each
added when defining:
q)n: 30; // Number of rows in the table
q)tab:([] date: .z.d - n?30; ranks: n?100; sym: n?`AAPL`MSFT`GOOGL;
group_name_symbol:enlist each n?`US_HOLIDAY`SPCL_CAL_DATE ;
group_name_string:enlist each n?("FIRST_3rd_FRI";"QTR_3rd_FRI";"FIRST_6rd_FRI") ;
price: "f"$n?100.5; recv_time: n?10:00:00.000 + n?10000000;
is_active: "b"$n?1 0);
Again when emptying rows care needed to have correct empty lists:
q)tab:update group_name_symbol:count[i]#enlist `$() from tab where sym=`AAPL;
q)tab:update group_name_string:count[i]#() from tab where sym=`GOOGL;
q)5 sublist tab
date ranks sym group_name_symbol group_name_string price recv_time is_active
------------------------------------------------------------------------------------------
2023.06.13 76 GOOGL ,`US_HOLIDAY () 7.504439 12:35:24.923 1
2023.05.30 13 MSFT ,`US_HOLIDAY ,"FIRST_3rd_FRI" 38.08953 11:54:05.027 0
2023.05.27 87 MSFT ,`SPCL_CAL_DATE ,"FIRST_3rd_FRI" 21.29572 11:54:05.027 1
2023.05.26 11 AAPL `symbol$() ,"FIRST_3rd_FRI" 33.82582 10:23:06.750 0
2023.06.13 66 AAPL `symbol$() ,"FIRST_6rd_FRI" 52.37666 10:36:25.423 0
q)meta tab
c | t f a
-----------------| -----
date | d
ranks | j
sym | s
group_name_symbol| S // Lists of symbols goes from s --> S
group_name_string| // Lists of strings goes from C --> blank
price | f
recv_time | t
is_active | b
https://code.kx.com/q/ref/maps/#each-left-and-each-right
,
needs to be wrapped in ()
to use it as append in a qsql query:
q)tab:update group_name_symbol:group_name_symbol(,)\:`CSMT from tab where ranks=11
q)tab:update group_name_string:group_name_string(,)\:enlist "THN" from tab where ranks=76
q)tab:update group_name_string:group_name_string(,)\:enlist "KRL" from tab where ranks=76
The result:
q)5 sublist tab
date ranks sym group_name_symbol group_name_string price recv_time is_active
------------------------------------------------------------------------------------------
2023.06.13 76 GOOGL ,`US_HOLIDAY ("THN";"KRL") 7.504439 12:35:24.923 1
2023.05.30 13 MSFT ,`US_HOLIDAY ,"FIRST_3rd_FRI" 38.08953 11:54:05.027 0
2023.05.27 87 MSFT ,`SPCL_CAL_DATE ,"FIRST_3rd_FRI" 21.29572 11:54:05.027 1
2023.05.26 11 AAPL ,`CSMT ,"FIRST_3rd_FRI" 33.82582 10:23:06.750 0
2023.06.13 66 AAPL `symbol$() ,"FIRST_6rd_FRI" 52.37666 10:36:25.423 0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论