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.
Related
I have two tables in my database, where my second table has one extra column "date" and I want to copy my first table data to second with current date
so I tried this SQL SERVER query
INSERT INTO table2 SELECT * FROM table1, GETDATE();
and I get an error invalid object name 'GETDATE'.
Specify any values you want to include in the SELECT clause:
INSERT INTO TABLE2 (Field1,Field2,Date)
SELECT Field1,Field2,GETDATE()
FROM TABLE1;
The FROM clause is only used to specify the query's data sources: tables, subqueries, table-valued functions.
I have two columns in a SQL table and I need a third column with the sum of the other two.
It's possible to add a calculated column?
SELECT WRAP_DURATION, IS_SERV_TYP_FLAG,
FROM RVM_DM.FACT_INTERACTION_SEGMENT
Yes it is possible. I did this in the following query, of course, if the columns are numeric
SELECT Col1,Col2,SUM(Col1 + Col2) AS Column3
FROM yourTable
GROUP BY Col1,Col2
If you want to combine two columns, use the following query
SELECT Col1,Col2,CONCAT(Col1,' ', Col2) AS Column3,
FROM yourTable
Example
SELECT Col1,Col2,nvl(Col1,0) + nvl(Col2,0) AS Column3
FROM yourTable
Try this
SELECT WRAP_DURATION, IS_SERV_TYP_FLAG, NVL(WRAP_DURATION,0) + NVL(IS_SERV_TYP_FLAG,0) SUM_OF_TWO_COL
FROM RVM_DM.FACT_INTERACTION_SEGMENT
Oracle supports generated columns, so you can just add this to the table:
ALTER TABLE RVM_DM.FACT_INTERACTION_SEGMENT ADD calculated INT
GENERATED ALWAYS AS (WRAP_DURATION + IS_SERV_TYP_FLAG);
This becomes part of the table definition -- and the value is always correct because it is calculated when it is queried.
I will say that it seems odd to add a flag to a duration, but this works for most expressions that use only columns in one row (or constants).
Oracle offered a virtual column you do with this.
syntax of a virtual column:
column_name [data_type] [GENERATED ALWAYS] AS (expression) [VIRTUAL]
In this syntax:
First, specify the name ( column_name) of the virtual column.
Second, specify the virtual column’s data type. If you omit the data type, the virtual column will take the data type of the result of the expression.
Third, specify an expression in parentheses after the AS keyword. The values of the virtual column will derive from the expression.
1) Creating a table with a virtual column example
create table RVM_DM.FACT_INTERACTION_SEGMENT (
WRAP_DURATION number,
IS_SERV_TYP_FLAG number,
CALC_SUM number generated always as (NVL(WRAP_DURATION,0) + NVL(IS_SERV_TYP_FLAG,0)) virtual
);
2) Adding a virtual column to an existing table example
ALTER TABLE RVM_DM.FACT_INTERACTION_SEGMENT
ADD (
CALC_SUM AS (NVL(WRAP_DURATION,0) + NVL(IS_SERV_TYP_FLAG,0))
);
I have a table 'zzz' with GpsLatitude and GpsLongitude columns. I want to create a single GPS column (PointWKT) from GpsLatitude and GpsLongitude columns and add it to the existing table 'zzz'.
Query for creating a single column PointWKT:
SELECT
ST_GeogPoint(GpsLongitude, GpsLatitude) AS PointWKT,
FROM
`xxx.yyy.zzz`
I would like to add this query result column PointWKT to an existing table 'zzz'. Correct answer:
SELECT DateTime, SerialNumber, TotalWorkingHours, ...
ST_GeogPoint(GpsLongitude, GpsLatitude) AS PointWKT,
FROM
`login-eko.telematics_latest_K2020.tractor_reports_p` BoundaryWKT
This answer was provided by #Kuki.
You just use ST_GeogPoint(GpsLongitude, GpsLatitude) AS PointWKT
within you select statement, as follows:
SELECT DateTime, SerialNumber, TotalWorkingHours, ...
ST_GeogPoint(GpsLongitude, GpsLatitude) AS PointWKT,
FROM
`project.dataset.table` BoundaryWKT
I am selecting all column of my table (eg 40 columns) and i want to rename only one column (e.g col20 ). how can i rename this column in my select query while selecting all column using select *. I don't want to write the name of all column. one more thing i also don't want to change the order of column in my table
Short answer is, you can't.
Either you have to select all the columns individually, using
select col1, col2, col3, ..., col20 as NewCol, ...., col40 from table
or use
select * from table
You can't have both.
Another option is there which you can use like below, but this will add an extra column in the output.
select t.*, t.col20 as NewCol from table t
i have one table test it has 10 column with 20 rows.
I need to move this data to archive_test table which has 11 column (10 same as test table plus one column is archive date).
when i tried to insert like below its shows error because number of column mismatch.
insert into archive_test
select * from test;
Please suggest the better way to do this.Thanks!
Well, obviously you need to supply values for all the columns, and although you can avoid doing so you should also explicitly state whic value is going to be inserted into which column. If you have an extra column in the target table you either:
Do not mention it
Specify a default value as part of its column definition in the table
Have a trigger to populate it
Specify a value for that column.
eg.
insert into table archive_test (col1, col2, col3 ... col11)
select col1,
col2,
col3,
...
sysdate
from test;
assuming that archive_date is the last column:
INSERT INTO archive_test
SELECT test.*, sysdate
FROM test