How to create a computed column in PrestoDB with values persisted? - sql

I am working in PrestoDB to query and create tables to configure data in a way that I can draw upon it and work on it in Excel and PowerBI. I am trying to create a persisted calculated column that is simply the quotient of two other existing columns.
A colleague suggested
Create Table B as
Select * , Column A/Column B as Column Q
from Table A
however when I perform
Select *
from Table B
column Q is there but completely empty.
What can I run to permanently add these computed columns so that when I query this data the values are persisted?

I don't think PrestoDB supports computed columns, much less persisted computed columns.
I think what you want is view, though, not a table:
create view v_a
select a.*, ColumnA/ColumnB as ColumnQ
from A a;
Anyone who queries v_b will see ColumnQ with the latest calculation of the ratio.

Related

Creating a big query table A from another table B (400 columns) but add an extra column(ID) to table A and cluster on column ID

I was trying to figure out an easy and fast way to create a table A from another table B which has more than 400 columns. Just a create or replace table statement as below would have worked.
create or replace table A
AS select * from B where 1=2
However, I need to create table A with an extra column as ID and also need to add clustering on this column ID. Altering the table later will also do but I understand that I cannot add clustering once the table is created. I do not want to write a DDL specifying all 400 columns. Can this be achieved in an easy and faster way?
I was also looking at options to create table dynamically by using INFORMATION_SCHEMA.COLUMNS information. However, I am not yet sure of that.

Is it possible to update one table from another table without a join in Vertica?

I have two tables A(i,j,k) and B(m,n).
I want to update the 'm' column of B table by taking sum(j) from table A. Is it possible to do it in Vertica?
Following code can be used for Teradata, but does Vertica have this kind of flexibility?
Update B from (select sum(j) as m from A)a1 set m=a1.m;
The Teradata SQL syntax won't work with Vertica, but the following query should do the same thing :
update B set m = (select sum(j) from A)
Depending on the size of your tables, this may not be an efficient way to update data. Vertical is a WORM (write once read many times) store, and is not optimized for updates or deletes.
An alternate way would be to first temporarily move the data in the target table to another intermediate (but not temporary) table. After that write a join query using the other table to produce the desired result, and finally use export table with that join query. Finally drop the intermediate table. Of course, this is assuming you have partitioned your table in a way suitable for your update logic.

SQL computed column for sum of data in another table

I have two tables:
DiskUsage Table StatisticsTable
Server DiskUsage(GB) Total Disk Usage xxxx
1 10
2 212
3 11
I need to create the "Total Disk Usage" as a column which works out the Sum of the "DiskUsage" column. This would need to be dynamic as more servers will be added overtime to the "DiskUsage" table.
I've done some looking into this - and I believe a Computed Column would be the easiest way to achieve this, but I'm not sure how to a). reference the other tables data or b). dynamically obtain the total of that column.
What is the issue with just running a query?
select sum(diskusage)
from diskusage;
This seems simple enough and unless you have millions of rows, performance should be quite fast.
Create a trigger
CREATE TRIGGER test
ON DiskUsage
after INSERT, UPDATE
AS
BEGIN
UPDATE StatisticsTable
SET TotalDiskUsage = (SELECT Sum(DiskUsage)
FROM DiskUsage)
END
Or as Mentioned by King.code create a view instead of having a table
CREATE VIEW StatisticsTable
AS
SELECT Sum(DiskUsage)
FROM DiskUsage

Copy data from a table and load it into another table

I have a table 'A' having 40 columns. I need to copy the data from 20 specific columns from 'A' , to another table 'B' having those 20 columns. There will be around 3 - 10 million records.
What will be the most efficient way to do this in PLSQL.
"daily table B will be truncated and new data will be inserted into it
from A."
Okay, so the most efficient way to do this is not to do it. Use a materialized view instead; a materialized view log on table A will allow you to capture incremental changes and apply them daily, or at any other window you like. Find out more.
Compared to that approach using handrolled PL/SQL - or even pure SQL - is laughably inefficient.
Do you need to do any sort of conversion on the data or is it just copying data straight from one table to another?
The easiest way to do this is, although you would have to create the indexes separately.
create table B as (select A.c1, A.c2, A.c3..... from A);
If table x already existed, you could just do a
insert into B select A.c1, A.c2.... from A
To speed this up, you would want to drop all the indexes on table x until the insert was done.

delete old values of a table and update the table with results of same query

My question is to simple, but I can't find out a way to delete old values of a table and update same table with results of same query.
UPDATE
The query is an SELECT on Table A, and the results be Table B. And nothing on Table B different of the result of last query on Table A.
I have a very big table, and I need to process the records and create a new table regularly. The old values of this table are not important, only the new ones.
I will appreciate any help.
What about a view? If you only need table B to query on. You said you have a select on table A. Lets say your select is SELECT * FROM TableA WHERE X = Y. Then your statement would be
CREATE VIEW vwTableB AS
SELECT * FROM TableA WHERE X = Y
And then instead of querying tableB you would query vwTableB. Any changes to the data in table A would be reflected in the view so you don't have to keep running a script yourself.
This was the data in vwTableB would be kept updated and you wouldn't have to keep deleting and inserting into the second table.
you can use a temporary table to store results you are working with, if you only need it for one session. it will automatically be dropped when you sign out.
you didn't say what db you are using, but try this
create temp tableB AS select * from tableA