CREATE TABLE a third table from two tables JOIN statement - sql

I have 2 tables and want to create a third one when ta asin_id and tb asin have the same value,
with the
SELECT * FROM ta JOIN tb ON ta.asin_id = tb.asin;
i can have the view from the third table but i cant create it.
i already tried this:
CREATE TABLE tc AS SELECT * FROM ta JOIN tb ON ta.asin_id = tb.asin;
ERROR: syntax error at or near "tc"
LINE 1: CREATE tc AS SELECT * FROM items JOIN bs_audiotv ON items.as...
i attached a pic from the shell here: https://photos.app.goo.gl/LLjHi2wn9WQhxXkR8

CREATE T is not a right syntax. You have to use CREATE TABLE TABLE_NAME AS
When you are using CTAS you should specify which columns you need one by one. If same column name exists in your tables then you will receive an error again.
CTAS
Here is you script.
CREATE TABLE TC
AS
SELECT ITEMS.ASIN_ID,
ITEMS.TITLE,
ITEMS.BRAND,
ITEMS.DESCRIPTION,
ITEMS.CATEGORIES,
ITEMS.SPECIFICATIONS,
ITEMS.IMAGES,
BS_AUDIOTV.ASIN,
BS_AUDIOTV.LINK,
BS_AUDIOTV.PRICE_L,
BS_AUDIOTV.PRICE_U,
BS_AUDIOTV.PRICE
FROM ITEMS JOIN BS_AUDIOTV ON ITEMS.ASIN_ID = BS_AUDIOTV.ASIN;

Related

How to put join result into a new table

How do I put the result of this join query into a new table on the same schema?
SELECT census2010.district,census2010.total,census2010.male,
census2010.female,census2010.region, districts.area
FROM ghana.census2010 LEFT JOIN ghana.districts
ON census2010.region ILIKE districts.region
Considering the above code, use the CREATE TABLE ___ AS ( )
command. like this
CREATE TABLE ghana.TableName AS (
SELECT census2010.district,census2010.total,census2010.male,
census2010.female,census2010.region, districts.area
FROM ghana.census2010
LEFT JOIN ghana.districts ON census2010.region ILIKE districts.region)
The result should be a table in the same database schema
If you want your join table to evolve after new entries are added or removed from your two starting tables, creating a view is the appropriate choice.
CREATE VIEW view_name AS
SELECT census2010.district, census2010.total, census2010.male,
census2010.female, census2010.region, districts.area
FROM ghana.census2010
LEFT JOIN ghana.districts
ON census2010.region ILIKE districts.region);
Then, you'll be able to run queries like the following ones
SELECT * FROM view_name;
in order to obtain the always up-to-date joined table. You can remove a view like any other table by running:
DROP VIEW view_name;

I'm having issues with the ALTER TABLE function

I'm attempting to use the tables trip, guide and reservation in order to make a normalized table. I'm getting the error "ORA-00933: SQL command not properly ended."
I'm not certain what is wrong with ALTER TABLE.
SELECT trip.TRIP_ID, trip.State, trip.Max_grp_size, trip.type, trip.season, guide.Guide_num, guide.last_name, guide.First_name, guide.address, guide.city, guide.Hire_date, trip_guides.guide_num, reservations.trip_price
FROM trip
JOIN trip_guides ON trip.TRIP_ID = trip_guides.trip_id
JOIN Guide ON trip_guides.guide_num = guide.Guide_num
ALTER TABLE trip ADD (price_trip CHAR)
JOIN reservations ON trip.price_trip = reservations.trip_price
ORDER BY trip.trip_ID;
It sounds like what you want is a "CREATE TABLE ... AS (SELECT...)" type of statement. Essentially creating a NEW table based on the result set of a SQL statement.
This would look like:
CREATE TABLE your_1st_level_normalized_table AS
(
SELECT trip.TRIP_ID,
trip.STATE,
trip.Max_grp_size,
trip.type,
trip.season,
guide.Guide_num,
guide.last_name,
guide.First_name,
guide.address,
guide.city,
guide.Hire_date,
trip_guides.guide_num,
reservations.trip_price,
NULL as PRICE_TRIP /*new field in the new table set to NULL*/
FROM trip
JOIN trip_guides ON trip.TRIP_ID = trip_guides.trip_id
JOIN Guide ON trip_guides.guide_num = guide.Guide_num
JOIN reservations ON trip.price_trip = reservations.trip_price
);
I've removed the ORDER BY since that's not allowed in this statement.

How to create a table with multiple columns

Struggling with a create table statement which is based on this select into statement below:
#MaxAPDRefundAmount money = 13.00
...
select pkd.*, pd.ProviderReference, per.FirstName, per.Surname, #MaxAPDRefundAmount [MaxAPDRefundAmount],commission.Type [Commission] into #StagedData from CTE_PackageData pkd
inner join J2H.dbo.Package pk on pkd.Reference = pk.Reference
inner join J2H.dbo.Product pd on pk.PackageId = pd.PackageId
inner join J2H.dbo.FlightReservation fr on pd.ProductId = fr.ProductId
and fr.FlightBoundID = 1
inner join J2H.dbo.ProductPerson pp on pd.ProductId = pp.ProductID
and pp.StatusId < 7
inner join J2H.dbo.Flight f on fr.FlightId = f.FlightID
inner join J2H.dbo.Person per on pk.PackageId = per.PackageId
and per.PersonId = pp.PersonId
inner join J2H.dbo.PersonType pt on per.PersonTypeId = pt.PersonTypeID
We are changing a select into to just normal insert and select, so need a create table (we are going to create a temp (hash tag table) and not declaring a variable table. Also there is a pkd.* at the start as well so I am confused in knowing which fields to include in the create table. Do I include all the fields in the select statement into the create statement?
Update:
So virtually I know I need to include the data types below but I can just do:
create table #StagedData
(
pkd.*,
pd.ProviderReference,
per.FirstName,
per.Surname,
#MaxAPDRefundAmount [MaxAPDRefundAmount],
commission
)
"Do I include all the fields in the select statement into the create statement?" Well, that depends, if you need them, than yes, if not than no. It's impossible for us to say whether you need them... If you're running this exact query as insert, than yes.
As for the create statement, you can run the query you have, but replace into #StagedData with something like into TEMP_StagedData. In management studio you can let sql server build the create query for you: right-click the newly created TEMP_StagedData table in the object explorer (remember to refresh), script Table as, CREATE To and select New Query Editor Window.
The documentation of the CREATE TABLE statement is pretty straightforward.
No. Clearly, you cannot use pkd.* in a create table statement.
What you can do is run your old SELECT INTO statement as a straight SELECT (remove the INTO #stagedata) part, and look at the columns that get returned by the SELECT.
Then write a CREATE TABLE statement that includes those columns.
To create a table from a SELECT without inserting data, add a WHERE clause that never returns True.
Like this:
SELECT * INTO #TempTable FROM Table WHERE 1=0
Once the table with the columns for your SELECT, you can add additional columns with ALTER TABLE.
ALTER TABLE #TempTable ALL ExtraColumn INT
Then do your INSERT/SELECT.

Postgres - CREATE TABLE FROM SELECT

I have two tables, one contains a large list of IDs and Info regarding those ids.
I have a second table Graph which just has two columns, each column contains the aforementioned id numbers, multiple times. I want to trim the size of my Info table by selecting only those ids that appear in my graph and creating a new smaller Info table. Is there a simple way of doing this?
CREATE TABLE FROM SELECT?
Thanks!
It's as easy as:
create table new_table
as
select t1.col1, t2.col2
from some_table t1
join t2 on t1.id = t2.some_id;
You can use any select statement for that. The column names of the new table are defined by the column aliases used in th query.
More details in the manual: http://www.postgresql.org/docs/current/static/sql-createtableas.html
You can create TEMP table if you need those small table only for that session. you can use below query to do that.
DROP TABLE IF EXISTS temp_table;
CREATE TEMP TABLE temp_table AS
SELECT
i.id as info_id, i.information as information
FROM
info i
INNER JOIN graph g ON i.id = g.id;
Now you can use this temp_table for your next table in the function.
OR
you can also create table like below (if you not want to create it as TEMP):
CREATE TABLE temp_table AS
SELECT
i.id as info_id, i.information as information
FROM
info i
INNER JOIN graph g ON i.id = g.id;

SQL saving to a Temporary table is not working

i have the following sql query
SELECT P.catalogid,
Sum(numitems) numitems,
Sum(ignoreditems) ignoreditems,
P.supplierid,
P.cname,
P.cprice,
P.cstock,
P.ccode,
P.minstock,
P.pother4,
P.processedsucssessfully,
P.notprocessed,
Y.backorder
INTO ##tempt
FROM ##temporderstable P
JOIN supporder Y
ON P.catalogid = Y.catalogid
GROUP BY P.catalogid,
P.supplierid,
P.cname,
P.cprice,
P.cstock,
P.ccode,
P.minstock,
P.pother4,
Y.backorder,
P.processedsucssessfully,
P.notprocessed
i am trying to save the result of join into another temporty table, but when i try to access the new table from another query it says invalid object name, is this the right way to save the result of this query to ##tempt?
Try using explicit temp table declaration and then check!!
Like this
CREATE TABLE ##tempt (
supplierid int,
other fields ..)