How to copy specific columns from one table to another in Tarantool - database-migration

I have 2 tables, for example:
table X, columns:
A integer
B string
C number
table X2, columns:
A integer
C number
how to copy contents of X to X2 (excluding the missing columns) in Tarantool?
tried, but didn't work:
INSERT INTO "X2" SELECT "A", "C" FROM "X"
SELECT "A", "C" INTO "X2" FROM "X"
(version used: 2.6.2)

What kind of error do you get? I don't have 2.6.2 at hand, but when I try on 2.7.0 everything works just fine:
tarantool> \set language sql
tarantool> \set delimiter ;
tarantool> CREATE TABLE x (col1 INTEGER PRIMARY KEY, col2 VARCHAR(20), col3 INTEGER);
tarantool> CREATE TABLE x2 (col1 INTEGER PRIMARY KEY, col2 INTEGER);
tarantool> INSERT INTO x VALUES (1, 'a', 10), (2, 'b', 20), (3, 'c', 30);
tarantool> SELECT * FROM x;
---
- metadata:
- name: COL1
type: integer
- name: COL2
type: string
- name: COL3
type: integer
rows:
- [1, 'a', 10]
- [2, 'b', 20]
- [3, 'c', 30]
...
tarantool> SELECT * FROM x2;
---
- metadata:
- name: COL1
type: integer
- name: COL2
type: integer
rows: []
...
tarantool> INSERT INTO x2 SELECT col1, col3 FROM x;
tarantool> SELECT * FROM x2;
---
- metadata:
- name: COL1
type: integer
- name: COL2
type: integer
rows:
- [1, 10]
- [2, 20]
- [3, 30]
...

Related

SSIS Multi conditional If statement

I am a new user in SSIS. I am wondering how I can complete the following below.
I need an if statement that reads IF Column 1 = 1 then 1, elseif column 2 = 2 then 2 elseif column 3 = 3 then 3 else 0. Just need to consolidate 3 columns into 1 column in SSIS.
Current SSIS Formula is only giving me column 1 and Null. I want the new column to show 1, 2,3, 0 etc.
[Column 1] == "1" ? "1" : [column 2] == "2" ? "2" : [Column 3] == "3" ? "3" : "0"
What you have is correct for the supplied data and conditions
Given a minimal reproduction.
Source query
SELECT
*
FROM
(
VALUES
('1', '2', '3', '4', 'ROW_0')
, ('X', '2', '3', '4', 'ROW_1')
, ('X', 'X', '3', '4', 'ROW_2')
, ('X', 'X', 'X', '4', 'ROW_3')
, ('X', 'X', 'X', 'X', 'ROW_4')
)D(Column1, Column2, Column3, Column4, ColNotes);
Derived column formula - same as your except I eliminate the space to avoid typing square brackets
Column1 == "1" ? "1" : Column2 == "2" ? "2" : Column3 == "3" ? "3" : "0"
The only way you'd have a NULL in the derived column was if you had a NULL in the inbound data.
Adding this row in to source query will result in a NULL as the generated value.
, (NULL, 'X', 'X', 'X', 'ROW_5')
If you need to guard against this condition, then you need to lead off with the null check in your expression
(ISNULL(Column1) || ISNULL(Column2) || ISNULL(Column3)) ? "0" : Column1 == "1" ? "1" : Column2 == "2" ? "2" : Column3 == "3" ? "3" : "0"

Julia equivalent to python list multiplication

In python I can quickly concatenate and create lists with repeated elements using the + and * operators. For example:
my_list = [1] * 3 + ['a'] * 4 # == [1, 1, 1, 'a', 'a', 'a', 'a']
Similarly in Julia, I can quickly concatenate and create strings with repeated elements using the * and ^ operators. For example:
my_string = "1"^3 * "a"^4 # == "111aaaa"
My question is whether or not there is a convenient equivalent for lists (arrays) in Julia. If not, then what is the simplest way to define arrays with repeated elements and concatenation?
For the above scenario, a shorter form is fill:
[fill(1,3); fill('a', 4)]
You could also define a Python style operator if you like:
⊕(a::AbstractVector{T}, n::Integer) where T = repeat(a, n)
⊕(a::T, n::Integer) where T = fill(a, n)
The symbol ⊕ can be entered in Julia by typing \oplus and pressing Tab.
Now you can do just as in Python:
julia> [1,2] ⊕ 2
4-element Vector{Int64}:
1
2
1
2
julia> 3 ⊕ 2
2-element Vector{Int64}:
3
3
You can use repeat, e.g.
[repeat([1], 3); repeat(['a'],4)]
produces Any[1, 1, 1, 'a', 'a', 'a', 'a'].

Counting and adding multiple variables from single cell in sheets

I have a sheets document that has cells that users input data into. They know to input the data in a certain format; a 'number' and a 'letter', followed by a space, a SKU number, and then a comma.
I'd like to have a formula that counts the amount of each 'letters' and then adds the 'numbers' for each letter.
There are only five 'letters' users can choose from; M, E, T, W, B.
The data they input isn't restricted to a set order, and there isn't a limit of how much they can input, as long as it follows the aforementioned syntax.
I attached a screenshot of an example of how this should look.
The yellow cell is the user inputted data, and the green cells is data created by formula.
Or here's a link to a live version: link
I tried doing it with COUNTIF but that didn't work. I'm guessing it would be done with an array, but I don't know where to start. If I can see an example of something similar, I could probably do the rest.
yes:
=INDEX(REGEXREPLACE(SPLIT(REGEXREPLACE(FLATTEN(QUERY(TRANSPOSE(QUERY(TRANSPOSE(SORT(TRANSPOSE(QUERY(SPLIT(
FLATTEN(REGEXREPLACE(TRIM(SPLIT(A2:A9, ",")), "\b(\d+(?:\.\d+)?)(.+?)\b(.*)", ROW(A2:A9)&"×$1$2×$1×$2")), "×"),
"select count(Col2),sum(Col3) where Col2 is not null group by Col1 pivot Col4 label count(Col2)''")))),
"offset 1", 0)*1&TRIM(REGEXREPLACE(TRANSPOSE(SORT(FLATTEN(QUERY(SPLIT(
FLATTEN(REGEXREPLACE(TRIM(SPLIT(A2:A9, ",")), "\b(\d+(?:\.\d+)?)(.+?)\b(.*)", ROW(A2:A9)&"×$1$2×$1×$2")), "×"),
"select count(Col2),sum(Col3) where Col2 is not null group by Col1 pivot Col4 limit 0 label count(Col2)''")))),
".*sum", ))),,9^9)), "([^ ]+ [^ ]+) ", "$1×"), "×"), "(\d+(?:\.\d+)?)$", "($1)"))
I've added a new sheet ("Erik Help") with the following solution:
=ArrayFormula(FILTER( SPLIT("B E M T W", " ") & " (" & IFERROR(VLOOKUP(ROW(A1:A) & SPLIT("B E M T W", " "), QUERY(FLATTEN(SPLIT(QUERY(FLATTEN(IFERROR(REPT(ROW(A1:A) & REGEXEXTRACT(SPLIT(REGEXREPLACE(A1:A&",", "\d+,", ""), " ", 0, 1), "\D") & "~", 1*REGEXEXTRACT(SPLIT(REGEXREPLACE(A1:A&",", "\d+,", ""), " ", 0, 1), "\d+")))), "WHERE Col1 <>'' "), "~", 1, 1)), "Select Col1, COUNT(Col1) GROUP BY Col1"), 2, FALSE), 0)&")", A1:A<>""))

Postgres C extended data type definition

When dealing with the following problems, Postgres is a bit tricky to deal with more complex structures. I want to set up a two-dimensional array of structure, but I don't know how to make Postgres C support me to do so? Do anyone have any ideas?
Table
id contents(text) num(double)
1 I love you. {1,3,4,5,6,7,8,10}
2 why do it? {3,4,2,11,12,33,44,15}
3 stopping. {22,33,11,15,14,22,11,55}
4 try it again. {15,12,11,22,55,21,31,11}
Sort the rows of each position of the array to get the fo.lowing structure. The result of the first row below is the first position of the num field column array, and so on.the count 4 refers to returning the first n sorted.
select my_func(contents, num, 4) from table;
expected result:
result
{('stopping.', 22), ('try it again.', 15), ('why do it?', 3), ('I love you.', 1)}
{('stopping.', 33), ('try it again.', 12), ('why do it?', 4), ('I love you.', 3)}
{('stopping.', 11), ('try it again.', 11), ('I love you.', 4), ('why do it?', 2)}
......
......
Thanks in advance.
I'm not sure why you need C extended data type, but the following will give you what you want and can be implemented as plpgsql function.
WITH t1 AS (
SELECT id, contents, unnest (num) AS n FROM table
),
t2 AS (
SELECT id, contents, n,
row_number () OVER (PARTITION BY id ORDER BY id) AS o
FROM t1 ORDER BY o ASC, n DESC, id ASC
),
t3 AS (
SELECT array_agg (ROW (contents, n)) AS a, o
FROM t2 GROUP BY o ORDER BY o
)
SELECT array_agg (a ORDER BY o) FROM t3;
UPDATE: Problem of the above may be undefined order of 'unnest'.
The following gives consistent relation between index and num, but need to write the size of num array explicitly.
WITH RECURSIVE t1 (i, id, contents, num) AS (
SELECT 1, id, contents, num[1] FROM table
UNION ALL
SELECT t1.i + 1, table.id, table.contents, table.num[t1.i + 1]
FROM t1, table
WHERE t1.id = table.id AND t1.i < 8 -- put here size of array
),
t2 (i, d) AS (
SELECT i, array_agg (ROW (contents, num) ORDER BY num DESC)
FROM t1 GROUP BY i
)
SELECT array_agg (d ORDER BY i) FROM t2;

How do I group by into struct in BigQuery?

I have the following data:
player_id level talent_id
1 1 a
1 2 b
1 3 c
2 1 d
2 2 e
And want to group by player_id and have rows as structs, with level values as struct field names:
player_id data
1 {_1 = a, _2 = b, _3 = c}
2 {_1 = d, _2 = e, _3 = null}
the level column is always from the set of {1, 2, 3} but some levels might be missing (null)
What I've got so far is aggregation by player_id and with attached array of results:
talents as (
select
p.player_id,
array_agg(struct(p.level, p.talent_id)) as talents
from source.player_talent p
group by player_id
),
player_id data
1 [{1, a}, {2, b}, {3, c}]
2 {{1, d}, {2, e}]
now I need to map this array to a struct with fixed property names _1, _2, _3
This returns the expected results:
WITH Players AS (
SELECT 1 AS player_id, 1 AS level, 'a' AS talent_id UNION ALL
SELECT 1, 2, 'b' UNION ALL
SELECT 1, 3, 'c' UNION ALL
SELECT 2, 1, 'd' UNION ALL
SELECT 2, 2, 'e'
)
SELECT
player_id,
STRUCT(
MAX(IF(level = 1, talent_id, NULL)) AS _1,
MAX(IF(level = 2, talent_id, NULL)) AS _2,
MAX(IF(level = 3, talent_id, NULL)) AS _3) AS data
FROM Players
GROUP BY player_id
The technique here is known as pivoting (converting rows to columns).