I am having two tables .
Table1 , Table2.
Table1 has name, age, salary.
Table2 has name,height, weight,Relevance,Weight_po etc.
. Both the tables has name as primary key.
Now I want to insert a two more new column in table 1 ie height, weight.
The values for height and weight has to fetch from Table2 where table1.name matches with table2.name.
Help me how to achieve this in postgres.
You can use select into statement and insert all data you want onto a NewTable with the structure you describe:
EDIT: Based on comment
Create table NewTable as
SELECT Table1.name,age,salary,height,weight
INNER JOIN Table2
ON Table1.name=Table2.name
Have you created height and weight columns in Table 1?
This will help to populate the values:
UPDATE Table1 t1
SET height= t2.height,weight=t2.weight
FROM Table2 t2
WHERE t1.name= t2.name
Tell me how it goes
Related
I have 2 SQL tables with same column label, Table1 and Table2.
I want to update Table1 with value of Table2 if there is a same value for an attribute.
The tables has CODE, POSITION, DESCRIPTION as columns.
I do this:
UPDATE Table1
SET CODE = Table2.CODE,
POSITION= Table2.POSITION
FROM Table2
WHERE DESCRIPTION = Table2.DESCRIPTION
It works, but if in the DESCRIPTION Value of Table2 is not present into Table1, I want to insert the entire row, in other words I need to do something like:
UPDATE IF DESCRIPTION EXISTS
ELSE INSERT
How can I do this?
You can do this from first-principals in 2 steps
Update the existing values like you have done:
UPDATE Table1
SET
CODE= t2.CODE,
POSITION= t2.POSITION
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.DESCRITPION = t2.DESCRITPION
Then you can insert the missing records
INSERT INTO Table1 (CODE, POSITION, DESCRITPION)
SELECT CODE, POSITION, DESCRITPION
FROM Table2 t2
WHERE NOT EXISTS (SELECT DESCRITPION
FROM Table1 t1
WHERE t1.DESCRITPION = t2.DESCRITPION)
Most RDBMS have a MERGE statement that allows you to combine these two queries into a single atomic operation, you'll need to check the documentation for your vendor, but an MS SQL Server MERGE solution looks like this:
MERGE INTO Table1 AS tgt
USING Table2 AS src
ON tgt.DESCRITPION = src.DESCRITPION
WHEN MATCHED
THEN UPDATE SET CODE= src.CODE,
POSITION= src.POSITION
WHEN NOT MATCHED THEN
INSERT (CODE, POSITION, DESCRITPION)
VALUES (src.CODE, src.POSITION, src.DESCRITPION)
An additional benefit to the MERGE clause is that it simplifies capturing the changed state into the OUTPUT clause which allows for some complex logic all while keeping the query as a single operation without having to manually create and track transaction scopes.
So, let's say that I have two tables. Table1 and Table2 and I want to see if the values of the column ID of Table1, also exist in column ID of Table2.
The query that I tried was this one:
SELECT ID
FROM Table1
WHERE ID IN (SELECT ID FROM Table2)
but although I see that they have some values that are the same, the result is 0.
So, how can I see which IDs of Table1, also belong to the ID column of Table2?
Try with join:
SELECT ID FROM Table1 LEFT JOIN Table2 ON Table1.ID=Table2.ID
I have 2 Tables.
1 table has my Data with a code.
The other table is a conversion table, where each code is assigned to a certain text.
I want that text to be in Table 1, my Data table.
Example:
i tried this:
You can use an inner select:
update table1 t1
set brandName = (select t2.text from table2 t2 where t1.key = t2.brand);
Using SQL Server, I Need to return the entire row from whatever table contains 'value' in the Filename column (A column each of the tables contain), but the tables do not have the same number of columns, and each table has unique columns with their own specific data types (The only column Name/Type they have in common is the Filename column that I need to check for 'value').
Ideally, I would be able to do something along the lines of:
SELECT * FROM Table1, Table2, Table3, Table4, Table5
WHERE Filename = 'someValue'
Since all tables share the same column name for the Filename.
I have tried using Union but have issues since the number of columns and datatypes of the tables do not align.
I have also tried every combination of JOIN I could find.
I'm sure this could be accomplished with IF EXISTS, but that would be many, many lines of what seems like unnecessary code. Hoping there is a more elegant solution.
Thanks in advance!
You can try to join the tables together. First create temporary table where you store the input. And then join the tables with this temporary to get all records you want. When there is no record for that filename in the table, then you will get NULL values.
create table Table1 (id int,value int);
insert into Table1 values (1,10)
create table Table2 (id int,value int);
insert into Table2 values (1,20)
create table Table3 (id int,value int);
insert into Table3 values (2,30)
Here is the query itself
create table #tmp (id int)
insert into #tmp
values (1)
select t.id, t1.value, t2.value, t3.value from #tmp as t
left join Table1 as t1
on t.id = t1.id
left join Table2 as t2
on t.id = t2.id
left join Table3 as t3
on t.id = t3.id
And this is what you get
id value value value
1 10 20 NULL
this should work too:
EXEC sp_MSforeachtable
#command1='SELECT * FROM ? where filename = ''someValue''',
#whereand='AND o.id in (select object_id from sys.tables where name in (''Table1'',''Table2'',''Table3''))'
I need to insert values from two tables into one table. Eg Table 1 is outward and table 2 is product table. I need to insert values into outward table and from product table i have productname which needs to be inserted into outward table. My code is
INSERT INTO tbltrn_outward, tbltrn product(chalanno,godownsrno, igodownsrno,deladdress,outwarddate,productname,qty,boxes,rate,price,batchcombo,active,createdby,createdon,fyearsno)
VALUES('$chalanno','$godownsrno','$igodownsrno','$deladdress','$outwarddate','$productname','$qty','$boxes','$rate','$price','$batchcombo','$active','$createdby','$createdon','$fyearsrno')";
You should use insert into .. select from construct for this purpose like
INSERT INTO tbltrn_outward(col1,col2,col3, ...)
select col1,col2,col3,...
from tbltrn_product
There is the basic:
INSERT INTO tbltrn_outward(chalanno, godownsrno, ... )
SELECT
column1
, column2
, ...
FROM tbltrn_product
The syntax mentioned in question is not correct.
Below Query would help if fields in table1, table2 are unique and table3 contains all fields of table1 and 2:
INSERT INTO TABLE3 (SELECT * FROM TABLE1,TABLE2);
Refer fiddle here:http://sqlfiddle.com/#!2/e3cc4/1
Below query would help if fields overlap in table1 and 2:
INSERT INTO TABLE3 (SELECT TABLE2.FIELD1, TABLE1.FIELD2,
TABLE2.FIELD3, TABLE2.FIELD4
FROM TABLE1,TABLE2);
Refer fiddle here:http://sqlfiddle.com/#!2/479f4/1