I would like to add a column to an SQL table with unknown columns and explode the entries in that table by a set of fixed values for that column. E.g. Turn
unknown col 1
...
unknown col x
1
...
foo
2
...
bar
into
unknown col 1
...
unknown col x
new col
1
...
foo
1
2
...
bar
1
1
...
foo
2
2
...
bar
2
The number of unknown columns is also unknown. I know the query to turn the original table into
unknown col 1
...
unknown col x
new col
1
...
foo
1
2
...
bar
1
I don't know the INSERT query that would turn it in to the desired table further above. The table is on Google BigQuery.
p.s: I can think of workarounds, e.g. multiply the number of rows in the original table by n, where n is the number of values the new column can take, then add the column and set the value based on the row number (which is not trivial to set) for each row. I am looking for a cleaner way.
add a column to an SQL table with unknown columns and explode the entries in that table by a set of fixed values for that column.
Below should do the "trick" - example
with new_col_values as (
select [1, 2, 3, 4] values
)
select t.*, val
from `project.dataset.your_table` t,
new_col_values, unnest(values) val
I would like to combine data from multiple cells into one cell using SAS Data Integration Studio.
My data is divided into three different tables, as follows:
Table 1
Col 1
Col 2
Col 3
City A
City B
City C
Table 2
Col 1
Col 2
Col 3
State A
State B
State C
Table 3
Variable 1
Variable 2
x
y
Desired final table:
Variable 1
Variable 2
States
Cities
x
y
State A, State B, State C
City A, City B, City C
That is, I want to create a final table in which I can join the data from table 1 in just one field and the data from table 2 in another field, separating the respective values with a comma.
I have a table in SQL database and I want to find the location of a cell like a coordinate and vice versa. Here is an example:
0 1 2 3
1 a b c
2 g h i
3 n o j
When I ask for i, I want to get row=2 and column=3. When I ask for a cell of row=2 and column=3, I want to get i.
You need to store your matrix in table specifying the columns and rows like this
create table matrix (
row int,
column int,
value varchar2(20)
);
Then you insert your data like this
insert into matrix values (1, 1, 'a');
insert into matrix values (1, 2, 'b');
//and so on.
And then you can simply find what you need using two queries
select column, row from matrix where value = 'i';
select value from matrix where column = 2 and row = 3;
In Oracle, you would do:
select "3"
from t
where "0" = 2;
Naming columns as numbers is not recommended. Your whole data model is strange for SQL. A better representation would be:
row col val
1 1 a
1 2 b
1 3 c
2 1 g
. . .
Then you could do:
select val
from grid
where row = 2 and col = 3;
Create a primary key column such as 'id' and for example, the related row is 'col'
select col from db where id = 2;
this returns you a specific cell (x,2)
Hi Guys I'm new here and can really do with your help writing a SQL script/function for the following problem.
I have a source table which contains three columns Name, Value, miNum. Example of the data inside this table is:
Name Value miNum
A+B+C 1+2+3 a1
C+D+E 3+4+5 a3
E+F 5+2 a7
Now, I have created a final_table and the columns of that table are same as the source table but with additional columns labelled a-z (29 columns in total).
What I want the script/Function to do is from the source table read each row and populate the corresponding column in final_table.
Example output of final_table
Name Value miNum A B C D E F
A+B+C 1+2+3 a1 1 2 3
C+D+E 3+4+5 a3 3 4 5
E+F 5+2 a7 5 2
new columns will be regularly added to the final_table so it won't make sense to hard code the columns into the SQL code. Is it possible to do all this without hardcoding column names??
please can someone kindly show me how I can achieve all this.
Thanks
Please add rest of the columns based on this schema:
select tst.*,
case when instr(name,'A') > 0 then substr(Value,instr(name,'A'),1) end A,
case when instr(name,'B') > 0 then substr(Value,instr(name,'B'),1) end B,
case when instr(name,'C') > 0 then substr(Value,instr(name,'C'),1) end C,
case when instr(name,'D') > 0 then substr(Value,instr(name,'D'),1) end D,
case when instr(name,'E') > 0 then substr(Value,instr(name,'E'),1) end E,
case when instr(name,'F') > 0 then substr(Value,instr(name,'F'),1) end F
from tst;
Gives result
NAME VALUE MINUM A B C D E F
------ ------ ------ - - - - - -
A+B+C 1+2+3 a1 1 2 3
C+D+E 3+4+5 a3 3 4 5
E+F 5+2 a7 5 2
Note that this approach works only for unique names, i.e. for duplicated names only the first value is shown, e.g.
NAME VALUE MINUM A B C D E F
------ ------ ------ - - - - - -
A+A+A 5+2+1 a11 5
The other restriction is that the keys are only A-Z and the values are single character, if this holds you may use this insertto populate the target table:
Insert into TARGET
(name, value, miNum,A,B,C,D,E,F)
select name, value, miNum,
case when instr(name,'A') > 0 then substr(Value,instr(name,'A'),1) end A,
case when instr(name,'B') > 0 then substr(Value,instr(name,'B'),1) end B,
case when instr(name,'C') > 0 then substr(Value,instr(name,'C'),1) end C,
case when instr(name,'D') > 0 then substr(Value,instr(name,'D'),1) end D,
case when instr(name,'E') > 0 then substr(Value,instr(name,'E'),1) end E,
case when instr(name,'F') > 0 then substr(Value,instr(name,'F'),1) end F
from source;
You will have to extend both insert column list and the query with the column up to Z.
Example:
column A column B
A 1
A 2
B 2
B 2
C 1
C 1
I would somehow like to get the following result:
column A column B
A 1.5
B 2
C 1
(which are averages of 1 and 2, 2 and 2 and 1 and 1)
How do I achieve that?
Thanks
If you're using Excel 2007 or above, you can also use the shorter AVERAGEIF function:
=AVERAGEIF($A$1:$A:$6,D1,$B$1:$B$6)
Less typing, easier to read..
In D1:D3, type A, B, C. Then in E1, put this formula
=SUMIF($A$1:$A$6,D1,$B$1:$B$6)/COUNTIF($A$1:$A$6,D1)
and fill down to E3. If you want to replace the existing data, copy E1:E3 and paste-special-values over itself. Then delete A:C.
Alternatively, you can add headers to your data, say "Letter" and "Number". Then create a Pivot Table from your data. Put Letter in the rows section and Number in the Data section. Change your Data section from SUM to AVERAGE and you'll get the same result.