SQL with multiple select statements for distinct entities - sql

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>

Related

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 do I add two sum queries together?

I have the following two pseudo queries:
SELECT Sum(a)
FROM b
WHERE c
and
SELECT Sum(d)
FROM b
WHERE e
I want to sum these queries together to one value but I can't figure out the syntax. Note the FROM statement is the same ("b"). I've tried a UNION query but this gives me two values...
You can use iif() inside sum() where you apply the conditions:
select sum(iif(c, a, 0)) + sum(iif(e, d, 0))
from b
Since both queries will always return a single record, you could alternatively cross join the two subqueries and simply add the results, e.g.:
select r1 + r2 from
(select sum(a) as r1 from b where c) t1,
(select sum(d) as r2 from b where e) t2
Try
SELECT SUM(col1)
FROM
(
SELECT Sum(a) col1
FROM b
WHERE c
UNION
SELECT Sum(d) col1
FROM b
WHERE e) t
Please try the following
Select sum(sumVal)
FROM
(SELECT Sum(a) sumVal
FROM b
where c
UNION
SELECT Sum(d) sumVal
FROM e
where f
)
Try to use this :
;WITH
t1 as ( select sum(a) as a from b where c>20)
,
t2 as (select sum(d) as d from b where e is not null)
select t1.b1+t2.c2 as s from t1 inner join t2 on t1.b1 != t2.c2

How can I display two columns together in 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

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: Turn a subquery into a join: How to refer to outside table in nested join where clause?

I am trying to change my sub-query in to a join where it selects only one record in the sub-query. It seems to run the sub-query for each found record, taking over a minute to execute:
select afield1, afield2, (
select top 1 b.field1
from anothertable as b
where b.aForeignKey = a.id
order by field1
) as bfield1
from sometable as a
If I try to only select related records, it doesn't know how to bind a.id in the nested select.
select afield1, afield2, bfield1
from sometable a left join (
select top 1 id, bfield, aForeignKey
from anothertable
where anothertable.aForeignKey = a.id
order by bfield) b on
b.aForeignKey = a.id
-- Results in the multi-part identifier "a.id" could not be bound
If I hard code values in the nested where clause, the select duration drops from 60 seconds to under five. Anyone have any suggestions on how to join the two tables while not processing every record in the inner table?
EDIT:
I ended up adding
left outer join (
select *, row_number() over (partition by / order by) as rank) b on
b.aforeignkey = a.id and b.rank = 1
went from ~50 seconds to 8 for 22M rows.
Try this:
WITH qry AS
(
SELECT afield1,
afield2,
b.field1 AS bfield1,
ROW_NUMBER() OVER(PARTITION BY a.id ORDER BY field1) rn
FROM sometable a LEFT JOIN anothertable b
ON b.aForeignKey = a.id
)
SELECT *
FROM qry
WHERE rn = 1
Try this
select afield1,
afield2,
bfield1
from sometable a
left join
(select top 1 id, bfield, aForeignKey from anothertable where aForeignKey in(a.id) order by bfield) b on b.aForeignKey = a.id