Combining columns from two unrelated tables in SQL - sql

Let's say I have two tables
Table 1
COl_1 COl_2
1 5
2 6
3 7
4 8
AND Table 2
COL3 COL4
9 13
10 14
11 15
12 16
I want the following:
COL_1 COL_2 COL3 COL4
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
Also, the number of rows are exactly the same everytime in each of the tables,
no key relation or integrity stuff.
Any clue?

You can do this using row_number() to add a "join" key to the two tables:
select t1.col_1, t1.col_2, t2.col_3, t2.col_4
from (select t1.*, row_number() over (order by col_1) as seqnum
from table1 t1
) t1 join
(select t2.*, row_number() over (order by col_3) as seqnum
from table2 t2
) t2
on t1.seqnum = t2.seqnum;
If the tables have different numbers of rows, you might want an outer join.

You can add an identity column as an alternative to row_number() as follows
alter table Table_1 add id int identity(1,1)
alter table Table_2 add id int identity(1,1)
Then using this ID column, you can join both tables as follows (as LONG suggested FULL JOIN)
select col1, col2, COL3, COL4
from Table_1
full outer join Table_2 on Table_1.id = Table_2.id
With some additional data you can get following results

Related

Return rows where specific column has duplicate values

From the table below I want to show the two rows where the values in column 3 are duplicates:
ID
Col2
Col3
1
a
123
2
b
123
3
c
14
4
d
65
5
e
65
This means that the query that I need should return rows with ID 1, 2 and 4, 5.
I wrote query using having:
SELECT *
FROM t1
INNER JOIN (SELECT col3 FROM t1
GROUP BY col3
HAVING COUNT(*) > 1) a
ON t1.col3 = a.col3
This query though only returns 1 and 4 rows for example, not all duplicates.
I would appreciate the help.
Your query should work, but I would suggest window functions:
select t1.*
from (select t1.*, count(*) over (partition by col3) as cnt
from t1
) t1
where cnt > 1;

How to inset one table colume data into another table column

Can u help me how to insert Column A data into col3 at place of null.
See the attachment.
TABLE TAB1
--------------
col1 col2 col3
5 7 NULL
8 11 NULL
3 6 NULL
2 12 NULL
TABLE TAB2
-----------
ColA CoB
7 5
18 8
24 3
36 2
Desire Output Like
col1 col2 col3
5 7 7
8 11 18
3 6 24
2 12 36
This is called commutative sum.
1. It's work for same table.
update TABLENAME set col3=col2
**2.**For inserting one table column data into another table
INSERT into tab1(col1)
select col1 from tab2
From what I can tell, you want a cumulative sum from the second column:
with toupdate as (
select t1.*,
sum(t1.col2) over (order by ??) as cume_col2
from tab1 t1
)
update toupdate
set col3 = cume_col2;
In order to do a cumulative sum, you need a column that specifies the order for the sum. Your data as shown does not have an appropriate column.
EDIT:
Oh, I see. The ordering comes from the second table:
with toupdate as (
select t1.*,
sum(t1.col2) over (order by t2.cola) as cume_col2
from tab1 t1 join
tab2 t2
on t1.col1 = t2.colb
)
update tab1
set col3 = toupdate.cume_col2
from tab1 join
toupdate
on tab1.col1 = toupdate.col1;
Try this:
Update tab1
set Col3=tab2.ColA
from tab1
inner join tab2 on tab1.col1=tab2.colB

SQL: KEEP Unique value of C1 with Highest value in C2

In my two columns of data I would like to keep only the unique values of ColumnOne that have the highest value in ColumnTwo.
For example
ColumnOne ColumnTwo
2 6
3 2
7 8
2 7
3 4
7 3
I would like the results:
ColumnOne ColumnTwo
2 7
3 4
7 8
You can do this with a group by statement:
select Column1, max(Column2)
from your_table
group by Column1
delete t1
from myTable t1
left join (select t2.Column1, max(t2.Column2) maxColumn2
from myTable t2
group by t2.Column1) tMax
on t1.Column1 = tMax.Column1
and t1.Column2 = tMax.maxColumn2
where tMax.Column1 is null
The below query will help you to accomplish your output for tables with huge no. of records:
create table table1_new as (select * from table1) with no data;
insert into table1_new
select columnone, max(columntwo) over(partition by columnone) from table1 group by columnone;
Validate data and then interchange table names:
drop table table1;
rename table1_new to table1;

require to form a sql query

I was working on preparing a query where I was stuck.
Consider tables below:
table1
id key col1
-- --- -----
1 1 abc
2 2 d
3 3 s
4 4 xyz
table2
id col1 foreignkey
-- ---- ----------
1 12 1
2 13 1
3 14 1
4 12 2
5 13 2
Now what I need is to select only those records from table1 for which the corresponding entries in table2 does not have say col1 value as 12.
So the challenge is after applying join even though it will skip for value 1 corresponding to col1 equal to 12 it still has another multiple rows whose values are say 13, 14 for which also they have same foreignkey. Now what I want is if there is a single row having value 12 then it should not pick that id at all from table1.
How can I form a query with this?
The output which i need is say from above table structure i want to get those records from table1 for which col1 value from table2 does not have value as 14.
so my query should return me only row 2 from table1 and not row 1.
Another way of doing that. The first two queries are just for making the sample data.
;WITH t1(id ,[key] ,col1) AS
(
SELECT 1 , 1 , 'abc' UNION ALL
SELECT 2 , 2 , 'd' UNION ALL
SELECT 3 , 3 , 's' UNION ALL
SELECT 4 , 4 , 'xyz'
)
,t2(id ,col1, foreignkey) AS
(
SELECT 1 , 12 , 1 UNION ALL
SELECT 2 , 13 , 1 UNION ALL
SELECT 3 , 14 , 1 UNION ALL
SELECT 4 ,12 , 2 UNION ALL
SELECT 5 ,13 , 2
)
SELECT id, [key], col1
FROM t1
WHERE id NOT IN (SELECT t2.Id
FROM t2
INNER JOIN t1 ON t1.Id = t2.foreignkey
WHERE t2.col1 = 14)
This is a typical case for NOT EXISTS:
SELECT id, [key], col1
FROM table1 t1
WHERE NOT EXISTS (SELECT 1
FROM table2 t2
WHERE t2.foreignkey = t1.id AND t2.col1 = 14)
The above query will not select a row from table1 if there is a single correlated row in table2 having col1 = 14.
Output:
id key col1
-------------
2 2 d
3 3 s
4 4 xyz
If you want to return records that, in addition to the criterion set above, also have correlated records in table2, then you can use the following query:
SELECT t1.id, MAX(t1.[key]) AS [key], MAX(t1.col1) AS col1
FROM table1 t1
INNER JOIN table2 t2 ON t1.id = t2.foreignkey
GROUP BY t1.id
HAVING COUNT(CASE WHEN t2.col1 = 14 THEN 1 END) = 0
Output:
id key col1
-------------
2 2 d
You can also achieve the same result with the second query using a combination of EXISTS and NOT EXISTS:
SELECT id, [key], col1
FROM table1 t1
WHERE EXISTS (SELECT 1
FROM table2 t2
WHERE t2.foreignkey = t1.id)
AND
NOT EXISTS (SELECT 1
FROM table2 t3
WHERE t3.foreignkey = t1.id AND t3.col1 = 14)
select t1.id,t1.key,
(select ROW_NUMBER() OVER(PARTITION BY col1 ORDER BY col1 DESC) AS Row,* into
#Temp from table1)
from table1 t1
inner join table2 t2 on t1.id=t2.foreignkey
where t2.col1=(select col1 from #temp where row>1)

Find matching column data between two rows in the same table

I want to find the matching value between two rows in the same sqlite table. For example, if I have the following table:
rowid, col1, col2, col3
----- ---- ---- ----
1 5 3 1
2 3 6 9
3 9 12 5
So comparing row 1 and 2, I get the value 3.
Row 2 and 3 will give 9.
Row 3 and 1 will give 5.
There will always be one and only one matching value between any two rows in the table.
What it the correct sqlite query for this?
I hardcoded the values for the rows because i do not know how to declare variables in sqllite.
select t1.rowid as r1, t2.rowid as r2, t2.col as matchvalue from <yourtable> t1 join
(
select rowid, col1 col from <yourtable> where rowid = 3 union all
select rowid, col2 from <yourtable> where rowid = 3 union all
select rowid, col3 from <yourtable> where rowid = 3
) t2
on t2.col in (t1.col1, t1.col2, t1.col3)
and t1.rowid < t2.rowid -- you don't need this if you have two specific rows
and t1.rowid = 1
select col from
(
select rid, c1 as col from yourtable
union
select rid, c2 from yourtable
union
select rid, c3 from yourtable
) v
where rid in (3,2)
group by col
order by COUNT(*) desc
limit 1