How can I display two columns together in SQL? - sql

I have 2 queries that return data in the form:
query 1:
column 1
a
b
c
query 2:
column 2
d
e
How can I combine the 2 queries to get output as:
column 1 column 2
a d
b e
c
The order of data in the columns does not matter.
Possibly anything with joins ?
Thanks

use row_number()
select t1.col1,t2.col2 from
(
select *,row_number() over(order by col1) rn from query1
) t1 full outer join
(
select *,row_number() over(order by col2) rn from query2
) t2 on t1.rn=t2.rn
For n,m rows use full outer join

A possible solution is selecting both columns with row_number() and join them by the row_number. One must be aware to select first from the table with the higher number of rows. Example:
select
col_1,
col_2
from (
select
a.col_1,
row_number() over () rn
from a
) s1
FULL OUTER JOIN (
select
b.col_2,
row_number() over () rn
from b
) s2 on s1.rn = s2.rn

Related

Using Partition By in an inner join to return a single value

I have read a table and it returned a value. I now need to do an Inner Join with another table to get the next highest value available after this value in this Table.
I.e I have just returned the value 7 from a select and the Table I now need to join with contains the values;
1
5
7
11
20
I only want to return 11 in my join.
I have tried 'Row_Number () Over Partition By' but doesn't work for me because I am using an 'on (A.Number > B.Number) in the Join statement so the Row Number returned will not always be 1 for me.
Any advice?
I tried something like this;
SELECT a_number_field
FROM Table_A A
INNER JOIN (
SELECT ROW_NUMBER() OVER (
PARTITION BY another_number_field
ORDER BY another_number_field
) rn
FROM Table_B
) B
ON (b.another_number_field > a.A_number_field)
WHERE a.number_field = 7
and rn=1;
I am expecting only the value 11 to be returned to me.
Use the ROW_NUMBER with your join query as the following:
SELECT a_number_field, another_number_field
FROM
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY A.a_number_field ORDER BY another_number_field) rn FROM
tableA A JOIN tableB B
ON B.another_number_field > A.a_number_field
) T
WHERE rn = 1 AND a_number_field = 7
See a demo.

The efficient way to use left join to find the closest value

I need to find the closest value in the right table and combine them all.
but for doing do my left join query runs on all the permutations and it tasks a lot of resources to calculate (my basic tables are huge)
For example my A table looks like
<A,1>
<A,2>
<A,10>
And table B looks like :
<A,4>
<A,5>
<A,6>
<A,7>
For this example the result will be:
<A,1,4>
<A,2,4>
<A,10,7>
This how I thought to do so:
select * from (
select *,row_number() over(partition by rown order by abs(b-a) asc) diff from (
(select a,b, row_number over () rown from x) a
CROSS JOIN
(select a,b from x) b
on a.a = b.a
) )where diff =1
Is there a better and efficient way to do so?
Consider below example
select id, a.val a_val, b.val b_val
from tableA a
left join tableB b
using(id)
where true
qualify row_number() over(partition by id, a.val order by abs(a.val - b.val)) = 1
If applied to sample data in your question - output is

How to replace TOP 1000 rows of select columns indiscriminately

Basically I have a table that contains 1000 rows with three columns. (TABLE A)
I have ANOTHER table with 200 columns with 1million+ records. (TABLE B)
I am trying to replace the THREE COLUMNS OF 1000 rows of TABLE B with those of TABLE A. I've read a lot of solutions where you can INSERT into table B from TABLE A.. but that's useless because I'll get NULLs in the remaining 197 columns that I need data for.
So the task is to replace rows of certain columns from one table to select columns of another table. There is NO conditions, just the top rows or whatever order you can think of is fine. If you can give an answer that takes ORDER BY something into account, that'd be bonus! Thank you so much!
If I understood your requirements
WITH TA
AS (SELECT *,
ROW_NUMBER()
OVER (
ORDER BY col1) AS RN
FROM TableA),
TB
AS (SELECT *,
ROW_NUMBER()
OVER (
ORDER BY col1) AS RN
FROM TableB)
UPDATE TB
SET TB.col1 = TA.col1,
TB.col2 = TA.col2,
TB.col3 = TA.col3
FROM TB
JOIN TA
ON TB.RN = TA.RN
Try something like this:
WITH topB AS (
SELECT TOP 1000 row_number() OVER(ORDER BY field_n) rn, b.* FROM table_b b
ORDER BY field_x),
topA AS (
SELECT row_number() OVER(ORDER BY field_m) rn, a.*
FROM table_a a)
UPDATE b
SET
b.Field_1 = a.Field_1,
b.Field_2 = a.Field_2,
b.Field_3 = a.Field_3
FROM
TopB b JOIN TopA a ON b.rn = a.rn
Idea here is to assign row numbers in both tables, join them by these numbers, and update the B part of the join with values from A.

SQL with multiple select statements for distinct entities

I am trying to learn some SQL and what I want to do is simply select some rows from a table according to some criteria.
So, I am trying something like:
Select * from mytable where id=1090 as A, Select * from mytable where id=1075 as B;
I need to keep them as distinct entities (A and B) in my example, so that I can do something like:
Select A.col, B.row from A, B where <some criteria>
I am unable to figure out how to put all this together in a SQL query
You can do this magic to achieve what you want, if I understand you correctly. This will return rows side by side, don't know how to explain:
;with a as(select *, row_number() over(order by(select null)) rn
from tableA where id = 1090),
b as(select *, row_number() over(order by(select null)) rn
from tableB where id = 1075)
select a.*, b.*
from a
full join b on a.rn = b.rn
If first select returns 4 rows and second 2 rows the output will be something like:
A(rn, cols) B(rn, cols)
1 ......... 1 .........
2 ......... 2 .........
3 ......... NULL
4 ......... NULL
select A.*, B.*
from
(Select * from mytable where id=1090) as A
join
(Select * from mytable where id=1075) as B ON <some criteria>

How to join several unrelated tables

I have five queries and each of them will return me single column multiple row output. I want to to write a function which will contain all of these queries.
Can anyone help?
query 1:
Select Col1 as X from Table1;
query 2:
Select Col3 as Y from Table2;
From a function I want to get a table which will have columns
X, Y
How to club these queries under single function?
Add a ROW_NUMBER() to each of the queries and join them by the row number.
Depending on number of rows returned by each of the query you'd join then by inner, left or full join.
Example below assumes that two queries return the same number of rows.
WITH
CTE1
AS
(
SELECT Col1 as X, ROW_NUMBER() OVER(ORDER BY Col1) AS rn
FROM Table1
)
,CTE2
AS
(
SELECT Col3 as Y, ROW_NUMBER() OVER(ORDER BY Col3) AS rn
FROM Table2
)
SELECT
CTE1.X, CTE2.Y
FROM
CTE1
INNER JOIN CTE2 ON CTE1.rn = CTE2.rn
Use the UNION operator:
SELECT
column_1
FROM
tbl_name_1
UNION ALL
SELECT
column_1
FROM
tbl_name_2;
If there is a relation between the two tables, try using a join.
Maybe a simple inner join would be possible here?
select Col1 as X from Table1
join
on Table1.Col1_name = Table2.col3_name