How to print all columns when columns are dynamic and one common column is present? - sql

Lets say i have 2 table A,B .In both the tables (id) is the common column ,and rest columns dynamic.so write a query to print id of "A" and rest all columns .
A(id,name,city),B(id,phone,phone_num). Here I only know "id" column ,rest columns(name,city,phone) are coming dynamically,So i can not use A.name,A.city,B.phone etc .In
select * from A FULL OUTER JOIN B ON A.id = B.id;
is printing id column twice.

If you want to display the id column only once, use the using clause for the join :
SELECT *
FROM tableA a
JOIN tableB b using (id)
The using clause as the effect that the join column(s) are only included once with select *.

If I'm not wrong, you want to display all the column of both the tables and the common column i.e id should come only once.If that's the case use the below query and replace the column name with the column names you want to display.
SELECT a.id,a.column1,a.column2,b.column1,b.column2
FROM tableA a
INNER JOIN tableB b ON a.id=b.id

Demo
create table my_table_1 (id int,c1 int,c2 int,c3 int);
create table my_table_2 (id int,c4 int,c5 int);
select array_to_string(array_agg (table_name || '.' || column_name::text),',')
from information_schema.columns
where table_name in ('my_table_1','my_table_2')
and not (table_name,column_name) = ('my_table_2','id')
my_table_1.id,my_table_1.c1,my_table_1.c2,my_table_1.c3,my_table_2.c4,my_table_2.c5

Related

SQL query to append values not contained in second table

I have table A and table B with different number of columns but both containing a column with IDs. Table A contains more complete list of IDs and table B contains some of the IDs from the table A.
I would like to return resulting table B with original information plus appended IDs that are missing in B but contained in A. For these appended rows, other columns should be blank while column with IDs in B should just contain missing ID values.
Simple solution UNION ALL, with NOT EXISTS:
select b.id, b.c1, ..., b.cn
from b
UNION ALL
select distinct a.id, null, ..., null -- should be same number of columns as in the above select
from a
where not exists (select 1 from b where b.id = a.id)
I think you described left join:
select *
from b left join
a
using (id)

How to Select all columns of a table except one

My code looks like:
CREATE TABLE tableC AS
(SELECT tableA.*,
ST_Intersection (B.geom, A.geom) as geom2 -- generate geom
FROM tableB, tableA
JOIN tableB
ON ST_Intersects (A.geom, b.geom)
WHERE test.id = 2);
Now It is working but I have two columns geom and geom2!
Inside geom column I will have the new geometry based on the intersection. So how can I select tableA except the geom column?
Create the table with all the columns and after that drop the geom column and rename the new one:
CREATE TABLE tableC AS
SELECT
tableA.*,
ST_Intersection (B.geom, A.geom) as geom2 -- generate geom
FROM
tableA inner JOIN tableB ON ST_Intersects (A.geom, b.geom)
WHERE test.id = 2
;
alter table tableC drop column geom;
alter table tableC rename column geom2 to geom;
The only way you would be able to do this would be to generate a dynamic SQL statement based on the columns within the table that excludes those you don't want. Obviously this will be a lot more effort than simply adding in all the column names.
There are also a lot of very good reasons to never include a select * in a production environment, given how picky SQL often is on the number and format of columns that are returned. By using select * you open yourself up to a changing query result in the future that could potentially break things.
If you have a LOT of columns and you simply don't want to manually type them all out, run the query below for your table and then format the result so you can copy/paste into your script:
SELECT *
FROM information_schema.columns
WHERE table_schema = 'your_schema'
AND table_name = 'your_table'

Getting common fields of two tables in PL/SQL

Suppose Table A and Table B have various fields. What is an easy way to get the common fields among Table A and Table B ? I want to do an inner join on these tables but I don't know what the common fields are.
Note that this is in PL/SQL. When I table A. or B. I get the list of fields names of each table in a drop down menu. But I would like to get the common fields.
It depends on what do you mean by "common fields".
If you want to get all colums which names are the same in both tables, then you can use this query:
SELECT t1.column_name
FROM user_tab_columns t1
JOIN user_tab_columns t2
ON t1.COLUMN_NAME = t2.COLUMN_NAME
/*
AND t1.DATA_TYPE = t2.DATA_TYPE
AND t1.DATA_LENGTH = t2.DATA_LENGTH
*/
WHERE t1.table_name = 'A'
AND t2.table_name = 'B'
;
Demo: http://sqlfiddle.com/#!4/2b662/1
But if you look at tables in the above demo, you will see that table A has a column named X with datatype VARCHAR2, and table B has also a column named X but of different type INT.
If you want to get all columns that have the same names, the same datatypes and the same length, then uncomment respective conditions in the above query.
Do mean something like this:
TableA has 2 columns: Id and Name.
TableB has 2 columns: Name and PhoneNumber
Query:
SELECT A.Id, A.Name, B.PhoneNumber
FROM TableA A, TableB B
WHERE A.Name = B.Name;
Edit:
If you want know what Columns names there a re from your table I believe you can use DESC TableA. Then you get a list a column names. You can use those to compaire against another list, for example from TableB.

Insert new/Changes from one table to another in Oracle SQL

I have two tables with same number of columns :-Table A and Table B
Every day I insert data from Table B to Table A. now the insert query is working
insert into table_a (select * from table_b);
But by this insert the same data which was inserted earlier that is also getting inserted. I only want those rows which are new or are changed from the old data. How can this be done ?
You can use minus:
insert into table_a
select *
from table_b
minus
select *
from table_a;
This assumes that by "duplicate" you mean that all the columns are duplicated.
If you have a timestamp field, you could use it to limit the records to those created after the last copy.
Another option is, assuming that you have an primary key (id column in my example) that you can use to know whether a record has already been copied, you can create a table c (with the same structure as a and b) and do the following:
insert into table c
select a.* from table a
left join table b on (a.id=b.id)
where b.id is null;
insert into table b select * from table c;
truncate table c;
You need to adjust this query in order to use the actual primary key.
Hope this helps!
If the tables have a primary or unique key, then you could leverage that in an anti-join:
insert into table_a
select *
from table_b b
where not exists (
select null
from table_a a
where
a.pk_field_1 = b.pk_field_1 and
a.pk_field_2 = b.pk_field_2
)
You don't say what your key is. Assuming you have a key ID, that is you only want ID's that are not already in Table A. You can also use Merge-Statement for this:
MERGE INTO A USING B ON (A.ID = B.ID)
WHEN NOT MATCHED THEN INSERT (... columns of A) VALUES (... columns of B)

Include table name in column from select wildcard sql

Is it possible to include table name in the returned column if I use wildcard to select all columns from tables?
To explain it further. Suppose I want to join two tables and both tables have the column name “name” and many other columns. I want to use wildcard to select all columns and not explicitly specifying each column name in the select.
Select *
From
TableA a,
TableB b
Where
a.id = b.id
Instead of seeing two column with same name "name", could I write a sql to return one column name as "a.name" (or TableA.name) and one as "b.name"(or TableB.name) without explicitly putting the column name in select?
I would prefer a solution for mssql but other database could be a reference too.
Thanks!
You can use select a.*, ' ', b.* from T1 a, T2 b to make it more visible where columns from T1 end and columns from T2 begin.
You are basically joining two tables on the ID field, so you will only see one column labeled "ID", not two, because you are asking to see only those records where the ID is the same in table a and table b: they share the same id.
Try ...
SELECT 'TableA' AS 'Table', A.* FROM TableA A
WHERE A.id IN (SELECT id FROM TableB)
UNION
SELECT 'TableB' AS 'Table', B.* FROM TableB B
WHERE B.id IN (SELECT id FROM TableA)
ORDER BY id, [Table]