I have two columns in a table (ex. column1, column2), one INT and the other VARCHAR types. I need to combine both in another column (ex. column3) and I don't want to do it manually. Is there a way to fill this third column with a combine of the other two column with a specific format using some SQLl query?
Example:
column1 column2 column3
8 munson munson, 8
23 gatine gatine, 23
63 carbon carbon, 63
Thanks,
If you want to do it on the fly (Just query the 3 new columns)
You can do:
Select column1,colum2, CONCAT_WS(column2,', ',CAST(column1 as TEXT)) as column3 from table;
If you are trying to modify the original table to add a new column you can do something like:
UPDATE
table
SET
column3 = CONCAT_WS(column2,', ',CAST(column1 as TEXT))
The previous snippet should work for postgresql. Other engines will have different syntax for updating a column.
Related
I have a table with one column and 46 rows and I want to create another column that contains the average of the first column in all 46 rows
e.g.
This is the table:
CREATE TABLE table2
SELECT column1
FROM table1
I want to add another column that contains for each row (46 rows) the value of AVG(column1)
How do can it be done?
You wouldn't use create table to add a column. In this case, a query using a window function is sufficient:
SELECT t1.*, AVG(column1) OVER () as avg_column1
FROM table1 t1;
This is Standard SQL and should work in any database.
I would like to generate a new ID number for every new combination of column 1 and column 2.
For example:
ID | column 1 | column 2
1 | peter | blue
2 | mark | red
1 | peter | blue
As there will be new rows added over time, with new values, this should be able to auto-update.
I tried DENSE_RANK(), which seemed to work. But it gave an error when I put it as a statement in a calculated column, so I guess this is not possible? (Still very new to SQL).
Thanks for any help!
Error message: #1901 - Function or expression 'dense_rank()' cannot be
used in the GENERATED ALWAYS AS clause of `ProductIdentifier
EDIT:
What I basically want is to link a row to another table based on the 2 columns. I could also concatenate the two columns of course, but I read somewhere that doing this with string will be slower. It will ultimately be a big table with currently 200.000+ rows and growing to millions. Is this something I could/should do?
I don't understand. If you want column1/column2 to be unique, then they should be in their own table:
create table t12 (
t12_id int auto_increment primary key,
column1 int,
column2 int,
unique (column1, column2)
);
This gives you the unique value for the pair that you seem to want.
You can't use window functions in a generated column, as you found out. You can, however, compute the information on the fly in a view:
create view myview as
select id, column1, column2, dense_rank() over(order by column1, column2) rn
from mytable
Then you can query the view instead of the table, like:
select * from myview;
I am work on a project which has to add one column to the exist table.
It is like this:
The OLD TBL Layout
OldTbl(
column1 number(1) not null,
column2 number(1) not null
);
SQL TO Create the New TBL
create table NewTbl(
column1 number(1) not null,
column2 number(1) not null,
**column3 number(1)**
);
When I try to insert the data by the SQL below,
on one oracle server,it was successful executed,
but on another oracle server, I got "ORA-00947 error: not enough values"
insert into NewTbl select
column1,
column2
from OldTbl;
Is there any oracle option may cause this kind of difference in oracle?
ORA-00947: not enough values
this is the error you received, which means, your table actually has more number of columns than you specified in the INSERT.
Perhaps, you didn't add the column in either of the servers.
There is also a different syntax for INSERT, which is more readable. Here, you mention the column names as well. So, when such a SQL is issued, unless a NOT NULL column is missed out, the INSERT still work, having null updated in missed columns.
INSERT INTO TABLE1
(COLUMN1,
COLUMN2)
SELECT
COLUMN1,
COLUMN2
FROM
TABLE2
insert into NewTbl select
column1,
column2
from OldTbl;
The above query is wrong, because your new table has three columns, however, your select has only two columns listed. Had the number and the order of the columns been same, then you could have achieved it.
If the number of the columns, and the order of the columns are different, then you must list down the column names in the correct order explicitly.
I would prefer CTAS(create table as select) here, it would be faster than the insert.
CREATE TABLE new_tbl AS
SELECT column1, column2, 1 FROM old_tbl;
You could use NOLOGGING and PARALLEL to increase the performance.
CREATE TABLE new_tbl NOLOGGING PARALLEL 4 AS
SELECT column1, column2, 1 FROM old_tbl;
This will create the new table will 3 columns, the first two columns will have data from the old table, and the third column will have value as 1 for all rows. You could keep any value for the third column as per your choice. I kept it as 1 because you wanted the third column as data type NUMBER(1).
how to insert a row back in same table with a column count increase by +1
insert into Columns
select columns
where count of column 3 increase by 1
Do you mean something like this:
insert into YourTable (column1, column2, column3, column4)
select column4, column1, column2, column3 from YourTable
In that case data will be copied into YourTable and data from first column will be in second column, from second in third ... data from the last column will be in first.
If you just want to take a column and increase the value of one column by one, I don't see why you'd take it out and insert it again when you can just UPDATE the data:
UPDATE sometable
SET somecolumn = somecolumn + 1
WHERE someothercolumn = somevalue
Based on your comment, maybe you just want an identity column.
If you create a table like this:
CREATE TABLE myTable (
id INT GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)
)
This should make it so every time you insert a row into myTable, the new row has an id which is one greater than any other in the table.
Presumably, you can ALTER a table to add an identity column too.
I used DB2 syntax because that's what this is tagged as. If you're using another database, the syntax will be much simpler.
I need a calculated field based on each record in sql server 2000.
for example there is a table like this:
col1 col2 calcfield
-------------------
1 2 col1+col2
3 5 col1*col2
I need a query to calculate the last field per record eg:
1 2 3
3 5 15
Actually This is a system that calculates a number for some persons.There are some parameters stored in fields of a table and there is another field that stores how to calculate the number from those parameters(that is the formula). for each person there are different parameters and a different formula. I want to design a query that can extract parameters and the calculated column directly from the table
Is there any way to do this and if there is what is the best and fastest way....
best regards
You just do the math and alias it. Por exemplo:
SELECT
field1,
field2,
field1 + field2 AS 'CalcField'
FROM table
If you need to do different calculations depending on the record, use a CASE statement:
SELECT
field1,
field2,
CASE
WHEN (some condition) THEN field1 + field2
WHEN (some other condition) THEN field1 * field2
ELSE (some default value or calculation)
END AS 'CalcField'
FROM table