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;
Related
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;
Partitioned table (table1):
second table (table2):
Needed output (table3):
With Single query it didnt work, so I tried with multiple queries. I had issue with only one partition. So I separated the query based on same.
For the issue less partitions :
insert into table3 partition(paritioned_c4) select a.c1,a.c2,a.c3,a.partitioned_c4 from db1.table1 a where partitioned_c4<>’3’;
For the rest part :
insert into table3 partition(paritioned_c4) select a.c1,a.c2,a.c3,a.partitioned_c4 from db1.table1 a join table2 b on a.c1=b.t1;
you need to join both tables. Create a subquery and then join both tables on a key.
i am fasing a situation when i want select some data to created table. This data must be in joined between 2 tables based on a rowid. I know this can be done like i wrote, but it's not a correct way. Could you please suggest me a good way of my code.
CREATE TABLE new_bi_ccm_agg_mth AS
WITH
RID_SEC AS
(
SELECT rowid
FROM bi_ccm_agg_mth t left join Base_ccm m on t.sub_id = m.sub_id and t.current_brand =
m.current_brand
)
select agg.* ,base.CURRENT_SEC FROM bi_ccm_agg_mth agg join base_ccm base
where rid_sec.rowid = agg.r owid
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.
I currently have two tables:
Table 1 has a unique ID and a count.
Table 2 has some data columns and one column where the value of the unique ID of Table 1 is inside.
When I insert a row of data in Table 2, the the count for the row with the referenced unique id in Table 1 should be incremented.
Hope I made myself clear. I am very new to PostgreSQL and SQL in general, so I would appreciate any help how to do that. =)
You could achieve that with triggers.
Be sure to cover all kinds of write access appropriately if you do. INSERT, UPDATE, DELETE.
Also be aware that TRUNCATE on Table 2 or manual edits in Table 1 could break data integrity.
I suggest you consider a VIEW instead to return aggregated results that are automatically up to date. Like:
CREATE VIEW tbl1_plus_ct AS
SELECT t1.*, t2.ct
FROM tbl1 t1
LEFT JOIN (
SELECT tbl1_id, count(*) AS ct
FROM tbl2
GROUP BY 1
) t2 USING (tbl1_id)
If you use a LEFT JOIN, all rows of tbl1 are included, even if there is no reference in tbl2. With a regular JOIN, those rows would be omitted from the VIEW.
For all or much of the table, it is fastest to aggregate tbl2 first in a subquery, then join to tbl1 - like demonstrated above.
Instead of creating a view, you could also just use the query directly, and if you only fetch a single row, or only few, this alternative form would perform better:
SELECT t1.*, count(t2.tbl1_id) AS ct
FROM tbl1 t1
LEFT JOIN tbl2 t2 USING (tbl1_id)
WHERE t1.tbl1_id = 123 -- for example
GROUP BY t1.tbl1_id -- being the primary key of tbl1!