How to Insert values into datatable from another datatable SQL - sql

I have table1 with the following data :
ID
Name
Date
1
Paul
01-11-2020
1
Paul
03-11-2020
and have table2 only with a Date column:
Date
02-11-2020
I want to get output from those tables as:
ID
Name
Date
1
Paul
01-11-2020
1
Paul
02-11-2020
1
Paul
03-11-2020
Could someone help me how to join two tables to get the output like above. I tried so many ways but I couldn't solve this issue. Thank you...

You can use CROSS JOIN after applying UNION ALL in order to return from the both tables in a row-wise manner such as
SELECT DISTINCT COALESCE(tt.ID,t1.ID) AS ID,
COALESCE(tt.Name,t1.Name) AS Name, tt.Date
FROM table1 AS t1
CROSS JOIN (SELECT * FROM table1 UNION ALL
SELECT null,null,Date FROM table2) AS tt
ORDER BY 3
Demo

Related

Finding the most recent records from duplicates

I'm trying to get the most recent records from a table where there are duplicates for each row.
Every month a new row for some IDs is getting added to the table, but some other records might not have a new row each month so the data will be like this
ID Date
1 8/30/2022
1 7/30/2022
3 8/30/2022
3 7/30/2022
3 6/30/2022
4 1/11/2021
The query result should be
ID Date
1 8/30/2022
3 8/30/2022
4 1/11/2021
I tried to use a sub-query, but it is only returning records that actually has the most recent for the whole table not per ID so it is only returning those who has a record in 8/30/2022.
This is my query
create table test as (
select * from table1 inner join
(select EmpID, max(Record_Date) as maxdate
from table1 group by EmpID) ms
on table1.EmpID ms.EmpID and Record_Date=maxdate)
WITH DATA;
You may use NOT EXISTS operator with correlated subquery as the following:
SELECT T.ID, T.Date
FROM Table1 T
WHERE NOT EXISTS(SELECT * FROM Table1 D WHERE D.ID=T.ID AND D.Date>T.Date)
And of course, if you want to create a new table from this statement the query will be:
CREATE TABLE test AS
(
SELECT T.ID, T.Date
FROM Table1 T
WHERE NOT EXISTS(SELECT * FROM Table1 D WHERE D.ID=T.ID AND D.Date>T.Date)
) WITH DATA;

SQL Joining two tables and removing the duplicates from the two tables but without loosing any duplicates from the tables itslef

I want to join two tables and remove duplicates from both the tables but keeping any duplicate value found in the first table.
T1
Name
-----
A
A
B
C
T2
Name
----
A
D
E
Expected result
A - > FROM T1
A - > FROM T1
B
C
D
E
I tried union but removes all duplicates of 'A' from both tables.
How can I achieve this?
Filter T2 before UNION ALL
select col
from T1
union all
select col
from T2
where not exists (select 1 from T1 where T1.col = T2.col)
Assuming you want the number of duplicates from the table with the most repetitions for each value, you can do it with the ROW_NUMBER() windowing function, to eliminate duplicates by their sequence with the set of repetitions in each table.
SELECT Name FROM (
SELECT Name, ROW_NUMBER() OVER ( PARTITION BY Name ORDER BY Name ) AS Row
FROM T1
UNION
SELECT Name, ROW_NUMBER() OVER ( PARTITION BY Name ORDER BY Name ) AS Row
FROM T2
) x
ORDER BY Name
To see how this works out, we add two B rows to T2 then do this:
SELECT Name, ROW_NUMBER() OVER ( PARTITION BY Name ORDER BY Name ) AS Row
FROM T1
Name Row
A 1
A 2
B 1
C 1
SELECT Name, ROW_NUMBER() OVER ( PARTITION BY Name ORDER BY Name ) AS Row
FROM T2
Name Row
A 1
B 1
B 2
D 1
E 1
Now UNION them without ALL to combine and eliminate duplicates:
SELECT Name, ROW_NUMBER() OVER ( PARTITION BY Name ORDER BY Name ) AS Row
FROM T1
UNION
SELECT Name, ROW_NUMBER() OVER ( PARTITION BY Name ORDER BY Name ) AS Row
FROM T2
Name Row
A 1
A 2
B 1
B 2
C 1
D 1
E 1
The final query up top is then just eliminating the Row column and sorting the result, to ensure ascending order.
See SQL Fiddle for demo.
select * from T1
union all
select * from T2 where name not in (select distinct name from T1)
Sql Fiddle Demo
you should use "union all" instead of "union".
"union" remove other duplicated records while "union all" gives all of them.
for you result,because of we filtered intersects from table 2 in "where",we don't need "UNION ALL"
select col1 from t1
union
select col1 from t2 where t2.col1 not in(select t1.col1 from t1)
I D'not know the following code is good practice or not But it's working
select name from T1
UNION
select name from T2 Where name not in (select name from T1)
The Above Query Filter the value based on T1 value and then join two tables values and show the result.
I hope it's helps you thanks.
Note : It's not better way to get result it's affect your performance.
I sure i update the better solution after my research
You want all names from T1 and all names from T2 except the names that are in T1.
So you can use UNION ALL for the 2 cases and the operator EXCEPT to filter the rows of T2:
SELECT Name FROM T1
UNION ALL
(
SELECT Name FROM T2
EXCEPT
SELECT Name FROM T1
)
See the demo.
Results:
> | Name |
> | :--- |
> | A |
> | A |
> | B |
> | C |
> | D |
> | E |

Cross join returning row from only one table

I'm trying to get two different columns by cross joining on same table but getting only on e column. Following is the sample query :
select 1 from dual cross join (select 2 from dual) t1;
Expected Result : 1 2
but getting only 1.
You have the select clause of
select 1
where you select a single column. If you want an output of 1 2 then use
select 1, 2
as your select clause.
You are not retrieving data from t1
select 1 as id, t1.*
from dual cross join (select 2 id1 from dual) t1;

Sql Server Query design

I have two tables in Sql Server Table1 and Table2.
The First Table has PartID, Code, Brand
The Second Table has ID, PartID, AddCode, AddBrand
The idea is that the first table is main table where Some Article is entered with his original code and Brand.
The Second Table is table where we can store additional Codes and Brands which original Article is related to them
Let say that in First Table We have following Data:
PartId Code Brand
100 15FY MCD
Second Table Has following data:
ID PartID AddCode AddData
1 100 1888 AddBrand1
2 100 FF0-1 AddBrand2
I want to display data with select like this:
PartId Code Brand
100 15FY MCD
100 1888 AddBrand1
100 FF0-1 AddBrand2
I've tried to use:
Select a.PartID, a.Code, a.Brand,b.AddCode,b.AddData
from table1 a left outer join
table2 b on a.PartId=b.PartId
but i cant figure out how to do it...
Thank you in advance
This sounds more like union all then join:
select PartId, Code, Brand
from ((select t1.PartId, t1.Code, t1.Brand, 1 as seq
from table1 t1
) union all
(select t2.PartId, t2.AddCode as Code, t2.AddBrand as brand, 2 as seq
from t2
)
) x
order by PartId, seq;
Note that this orders the results so all PartIds appear together in the result set, with the row from the first table appearing first.
Use UNION ALL Statement In SELECT Clause :
SELECT PartId, Code, Brand
FROM Table1
UNION ALL
SELECT PartID ,AddCode Code,AddData Brand
FROM Table2
SELECT *
FROM (
SELECT A.PARTID
,A.CODE
,A.BRAND
FROM TABLE1 A
UNION ALL
SELECT B.PARTID
,B.ADDCODE
,B.ADDDATA
FROM TABLE B
) RESULT
ORDER BY RESULT.PARTID
Use Union of both tables like this
Select PartId, Code, Brand from table1
UNION ALL
Select PartID, AddCode, addData
from table2

PostgreSQL: How to add Column from other table in select statement?

I'm trying to select a column from another table like I used to do in MSSQL:
select * , Date = (select top 1 Date from [dbo].TableB where status = 1 order by Date desc)
from [dbo].TableA
How can I do that in PostgreSQL?
Additional sample data:
TableA
Names
Richards
Marcos
Luke
Matthew
John
TableB
Date Status
2016-01-01 1
2016-01-02 0
2016-01-03 1
2016-01-04 1
2016-01-05 1
Expected Output:
Name Date
Richards 2016-01-02
Marcos 2016-01-02
Luke 2016-01-02
Matthew 2016-01-02
John 2016-01-02
Thanks!
Date = (...) is invalid (standard) SQL and won't work in Postgres (or any other DBMS except for SQL Server)
A column alias is defined using AS ... in SQL (and Postgres). Postgres also doesn't have top. It uses limit.
Using square brackets in an identifier is also not allowed in SQL. So [dbo] needs to become dbo or "dbo" depending on how you created the schema.
select a.*,
(select date
from dbo.tableb as b
where b.status = 1
order by b.date desc
limit 1) as date
from dbo.tablea a
date is a reserved word and should not be used as an identifier (column name)
If you want to use standard ANSI SQL, you can also use fetch first 1 row only instead of limit 1.
Another option would be to use max() instead of the limit in the sub-select which doesn't need the limit at all:
select a.*,
(select max(date)
from dbo.tableb as b
where b.status = 1) as date
from dbo.tablea a
I'm not sure if this is the correct syntax, but did you try this:
select * , (select NewColumn from [dbo].TableB) as NewColumn
from [dbo].TableA
I hope it helps.
I'm not too familiar with PostgreSQL but SQL is still SQL.
First thing to say is you have to have only one result on the second table query and you can do it in
SELECT
A.*
(select NewColumn from [dbo].TableB.NewColumn) as NewColumn
FROM TableA
However I think you'll need to declare a join condition.
SELECT
A.*
(select NewColumn from [dbo].TableB.NewColumn where A.Col1 = TableB.col1)
FROM TableA A
without a real example I cant be more specific.
Try this:
select a.*, b.column
from tableA as a, tableB as b;
You could try doing a CROSS JOIN:
SELECT * FROM
(SELECT * FROM dbo.TableA),
(SELECT Date FROM dbo.TableB WHERE status = 1 ORDER BY Date DESC LIMIT 1)