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

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

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

问题

我有一个表格,示例如下:

  1. n: 30; // 表格中的行数
  2. 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);
  3. tab:update group_name_symbol:` from tab where sym=`AAPL;
  4. tab:update group_name_string:` from tab where sym=`GOOGL;

现在我想要向列group_name_symbol和group_name_string中添加一些固定文本。
并且我希望在需要时不断向该列追加新的固定文本。因此,对于不同的固定文本,需要新的更新语句。

因此,当我运行单独的更新查询时,最终输出将如下所示:

  1. update group_name_symbol:`CSMT from tab where ranks=92 // 追加而不是覆盖
  2. update group_name_string:`THN from tab where ranks=19 // 追加而不是覆盖
  3. update group_name_string:`KRL from tab where ranks=19 // 追加而不是覆盖

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

英文:

I have a table say example below

  1. n: 30; // Number of rows in the table
  2. 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);
  3. tab:update group_name_symbol:` from tab where sym=`AAPL;
  4. tab:update group_name_string:` from tab where sym=`GOOGL;

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

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

  1. update group_name_symbol:`CSMT from tab where ranks=92 //append and not override
  2. update group_name_string:`THN from tab where ranks=19 //append and not override
  3. update group_name_string:`KRL from tab where ranks=19 //append and not override

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

答案1

得分: 1

将您的符号原子列转换为嵌套符号列,首先需要执行以下操作:

  1. update enlist each group_name_symbol from `tab

然后您可以执行以下操作:

  1. q)update group_name_symbol:(group_name_symbol,'`CSMT) from tab where ranks=87
  2. date ranks sym group_name_symbol group_name_string price recv_tim..
  3. -----------------------------------------------------------------------------
  4. 2023.06.16 87 GOOGL `US_HOLIDAY`CSMT ` 55.25425 11:32:21..
  5. 2023.06.06 63 GOOGL ,`US_HOLIDAY ` 19.68259 10:38:48..

对于您的字符串列,您应该这样设置空白:

  1. tab:update group_name_string:count[i]#enlist"" from tab where sym=`GOOGL;

然后您可以类似地追加:

  1. q)update enlist each group_name_string from `tab
  2. q)update group_name_string:(group_name_string,:enlist"THN") from tab where ranks=62
  3. date ranks sym group_name_symbol group_name_string price recv..
  4. -----------------------------------------------------------------------------
  5. 2023.06.16 87 GOOGL US_HOLIDAY ,"" 55.25425 11:3..
  6. 2023.06.06 63 GOOGL US_HOLIDAY ,"" 19.68259 10:3..
  7. 2023.06.20 62 MSFT SPCL_CAL_DATE ("QTR_3rd_FRI";"THN") 56.43337 12:4..
  8. 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:

  1. update enlist each group_name_symbol from `tab

then you could do:

  1. q)update group_name_symbol:(group_name_symbol,'`CSMT) from tab where ranks=87
  2. date ranks sym group_name_symbol group_name_string price recv_tim..
  3. -----------------------------------------------------------------------------..
  4. 2023.06.16 87 GOOGL `US_HOLIDAY`CSMT ` 55.25425 11:32:21..
  5. 2023.06.06 63 GOOGL ,`US_HOLIDAY ` 19.68259 10:38:48..

For your string column you should be setting blanks like so:

  1. tab:update group_name_string:count[i]#enlist"" from tab where sym=`GOOGL;

Then you can similarly append

  1. q)update enlist each group_name_string from `tab
  2. q)update group_name_string:(group_name_string,\:enlist"THN") from tab where ranks=62
  3. date ranks sym group_name_symbol group_name_string price recv..
  4. -----------------------------------------------------------------------------..
  5. 2023.06.16 87 GOOGL US_HOLIDAY ,"" 55.25425 11:3..
  6. 2023.06.06 63 GOOGL US_HOLIDAY ,"" 19.68259 10:3..
  7. 2023.06.20 62 MSFT SPCL_CAL_DATE ("QTR_3rd_FRI";"THN") 56.43337 12:4..
  8. 2023.05.26 42 GOOGL US_HOLIDAY ,"" 7.079031 11:5..

答案2

得分: 1

确保你的列从一开始就是列表的列表,否则你会得到错误。

在定义时添加enlist each

  1. q)n: 30; // 表中的行数
  2. q)tab:([] date: .z.d - n?30; ranks: n?100; sym: n?`AAPL`MSFT`GOOGL;
  3. group_name_symbol:enlist each n?`US_HOLIDAY`SPCL_CAL_DATE ;
  4. group_name_string:enlist each n?("FIRST_3rd_FRI";"QTR_3rd_FRI";"FIRST_6rd_FRI") ;
  5. price: "f"$n?100.5; recv_time: n?10:00:00.000 + n?10000000;
  6. is_active: "b"$n?1 0);

同样,在清空行时需要小心,确保有正确的空列表:

  1. q)tab:update group_name_symbol:count[i]#enlist `$() from tab where sym=`AAPL;
  2. q)tab:update group_name_string:count[i]#() from tab where sym=`GOOGL;
  1. q)5 sublist tab
  2. date ranks sym group_name_symbol group_name_string price recv_time is_active
  3. ------------------------------------------------------------------------------------------
  4. 2023.06.13 76 GOOGL ,`US_HOLIDAY () 7.504439 12:35:24.923 1
  5. 2023.05.30 13 MSFT ,`US_HOLIDAY ,"FIRST_3rd_FRI" 38.08953 11:54:05.027 0
  6. 2023.05.27 87 MSFT ,`SPCL_CAL_DATE ,"FIRST_3rd_FRI" 21.29572 11:54:05.027 1
  7. 2023.05.26 11 AAPL `symbol$() ,"FIRST_3rd_FRI" 33.82582 10:23:06.750 0
  8. 2023.06.13 66 AAPL `symbol$() ,"FIRST_6rd_FRI" 52.37666 10:36:25.423 0
  1. q)meta tab
  2. c | t f a
  3. -----------------| -----
  4. date | d
  5. ranks | j
  6. sym | s
  7. group_name_symbol| S // 符号的列表从s --> S
  8. group_name_string| // 字符串的列表从C --> 空
  9. price | f
  10. recv_time | t
  11. is_active | b

在qsql查询中,, 需要包含在 () 中才能用作追加:

  1. q)tab:update group_name_symbol:group_name_symbol(,)\:`CSMT from tab where ranks=11
  2. q)tab:update group_name_string:group_name_string(,)\:enlist "THN" from tab where ranks=76
  3. q)tab:update group_name_string:group_name_string(,)\:enlist "KRL" from tab where ranks=76

结果:

  1. q)5 sublist tab
  2. date ranks sym group_name_symbol group_name_string price recv_time is_active
  3. ------------------------------------------------------------------------------------------
  4. 2023.06.13 76 GOOGL ,`US_HOLIDAY ("THN";"KRL") 7.504439 12:35:24.923 1
  5. 2023.05.30 13 MSFT ,`US_HOLIDAY ,"FIRST_3rd_FRI" 38.08953 11:54:05.027 0
  6. 2023.05.27 87 MSFT ,`SPCL_CAL_DATE ,"FIRST_3rd_FRI" 21.29572 11:54:05.027 1
  7. 2023.05.26 11 AAPL ,`CSMT ,"FIRST_3rd_FRI" 33.82582 10:23:06.750 0
  8. 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:

  1. q)n: 30; // Number of rows in the table
  2. q)tab:([] date: .z.d - n?30; ranks: n?100; sym: n?`AAPL`MSFT`GOOGL;
  3. group_name_symbol:enlist each n?`US_HOLIDAY`SPCL_CAL_DATE ;
  4. group_name_string:enlist each n?("FIRST_3rd_FRI";"QTR_3rd_FRI";"FIRST_6rd_FRI") ;
  5. price: "f"$n?100.5; recv_time: n?10:00:00.000 + n?10000000;
  6. is_active: "b"$n?1 0);

Again when emptying rows care needed to have correct empty lists:

  1. q)tab:update group_name_symbol:count[i]#enlist `$() from tab where sym=`AAPL;
  2. q)tab:update group_name_string:count[i]#() from tab where sym=`GOOGL;
  1. q)5 sublist tab
  2. date ranks sym group_name_symbol group_name_string price recv_time is_active
  3. ------------------------------------------------------------------------------------------
  4. 2023.06.13 76 GOOGL ,`US_HOLIDAY () 7.504439 12:35:24.923 1
  5. 2023.05.30 13 MSFT ,`US_HOLIDAY ,"FIRST_3rd_FRI" 38.08953 11:54:05.027 0
  6. 2023.05.27 87 MSFT ,`SPCL_CAL_DATE ,"FIRST_3rd_FRI" 21.29572 11:54:05.027 1
  7. 2023.05.26 11 AAPL `symbol$() ,"FIRST_3rd_FRI" 33.82582 10:23:06.750 0
  8. 2023.06.13 66 AAPL `symbol$() ,"FIRST_6rd_FRI" 52.37666 10:36:25.423 0
  1. q)meta tab
  2. c | t f a
  3. -----------------| -----
  4. date | d
  5. ranks | j
  6. sym | s
  7. group_name_symbol| S // Lists of symbols goes from s --> S
  8. group_name_string| // Lists of strings goes from C --> blank
  9. price | f
  10. recv_time | t
  11. 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:

  1. q)tab:update group_name_symbol:group_name_symbol(,)\:`CSMT from tab where ranks=11
  2. q)tab:update group_name_string:group_name_string(,)\:enlist "THN" from tab where ranks=76
  3. q)tab:update group_name_string:group_name_string(,)\:enlist "KRL" from tab where ranks=76

The result:

  1. q)5 sublist tab
  2. date ranks sym group_name_symbol group_name_string price recv_time is_active
  3. ------------------------------------------------------------------------------------------
  4. 2023.06.13 76 GOOGL ,`US_HOLIDAY ("THN";"KRL") 7.504439 12:35:24.923 1
  5. 2023.05.30 13 MSFT ,`US_HOLIDAY ,"FIRST_3rd_FRI" 38.08953 11:54:05.027 0
  6. 2023.05.27 87 MSFT ,`SPCL_CAL_DATE ,"FIRST_3rd_FRI" 21.29572 11:54:05.027 1
  7. 2023.05.26 11 AAPL ,`CSMT ,"FIRST_3rd_FRI" 33.82582 10:23:06.750 0
  8. 2023.06.13 66 AAPL `symbol$() ,"FIRST_6rd_FRI" 52.37666 10:36:25.423 0

huangapple
  • 本文由 发表于 2023年6月22日 01:52:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525958.html
匿名

发表评论

匿名网友

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

确定