Can this nested-select SQL query be simplified? - sql

I feel like there should be a simpler way to write this, since both SELECT statements are pulling from the same table:
SELECT column1
FROM table1
WHERE column2 = 'VIRTUAL'
AND column3 IN
(SELECT column3
FROM table1
WHERE column1 = 'ELEC-035A');

Here is the same query but i replaced the subquery by an INNER JOIN clause:
SELECT T1.column1
FROM table1 T1
INNER JOIN table1 T2 ON T2.column3 = T1.column3
AND T2.column1 = 'ELEC-035A'
WHERE T1.column2 = 'VIRTUAL'
GROUP BY T1.column1
Hope this will help.

Related

update statement in postgresql for multiple tables join, i want optimized update for below update statement, below statement is taking long time

update tabel1 t1 set column5 = t2.column1, column2 = 0
from table1 t
join table2 t2 on t2.column3 = t.column3
left join table3 t3 on t3.coulmn4 = t.column4
where t3.column5 is null
As documented in the manual:
Do not repeat the target table as a from_item unless you intend a self-join
So, don't repeat the target table in the FROM clause:
update table1 t1
set column5 = t2.column1,
column2 = 0
from table2 t2
left join table3 t3 on t3.column4 = t2.column4
where t2.column3 = t1.column3 --<< this replaces the original JOIN to t1
and t3.column5 is null

How to add a column in a table from another table having common id column in both the tables in SQL Server?

I am trying to update a table1 with a Column2 from table2 where id should match while inserting the Column2 values from table2 in table1 for SQL Server.
I tried with the below 2 sets of code, but it is not working.
Can anyone help me out with this?
alter table table1
add Column2 varchar(20)
insert into table1
Select t2.Column2
from table1 as t1
inner join table2 as t2
on t1.id = t2.id
And, below also:
Update table1
Set Column2 = (Select t2.Column2
from table1 as t1
inner join table2 as t2
on t1.id = t2.id)
Update t1
Set Column2 = t2.Column2
from table1 as t1
inner join table2 as t2
on t1.id = t2.id

comparing two tables to make sure they are same row by row and column by column on SQl server

I am comparing two tables to make sure they are same row by row and column by column on SQl server.
SELECT *
FROM t1, t2
WHERE t1.column1 = t2.column1 AND t1.column2 = t2.column2
AND t1.column3 = t2.column3 AND t1.column4 != t2.column4
The tables are vey large, more than 100 million.
I got error:
ERROR [HY000] ERROR: 9434 : Not enough memory for merge-style join
Are there better ways to do this comparison.
thanks !
A much efficient way of checking the row by row difference will be using Exists operator.
Something like this....
SELECT *
FROM t1
WHERE NOT EXISTS (SELECT 1
FROM t2
WHERE t1.column1 = t2.column1
AND t1.column2 = t2.column2
AND t1.column3 = t2.column3
AND t1.column4 = t2.column4
)
You could try EXCEPT http://technet.microsoft.com/en-us/library/ms188055(v=sql.100).aspx
SELECT column1, column2, column3, column4 FROM t1
EXCEPT
SELECT column1, column2, column3, column4 FROM t2
What if you try an INNER JOIN (and not select all the data from both tables)?
SELECT t1.column4, t2.column4
FROM t1 INNER JOIN t2 ON t1.column1 = t2.column1 AND t1.column2 = t2.column2
AND t1.column3 = t2.column3
WHERE t1.column4 != t2.column4
Do you want to identify all the rows that are different or just identify IF there are any rows that are different?
Here's how I would do this: first, I assume you have primary keys on both tables. When you join those tables, the best way to join is using primary key fields, not all of them:
select t1.*, t2.*
from t1 join t2 on t1.id = t2.id
then you can compare those tables field-by-field without overloading sql:
select t1.*, t2.*
from t1 outer join t2 on t1.id = t2.id
where t1.field1 <> t2.field1 ot t1.field2 <> t2.field2 .....
the resulting records would be mismatches.
the code I wrote here is conceptual, I personally didn't run it on sql, so you might need to adjust
All of the above are good suggestions (My first try would be SELECT * FROM t1 EXCEPT SELECT * FROM t2), but you indicate they all give the same out of memory error. Therefore I must conclude your tables are simply too large to perform the operation you desire all in one go. You'll have to run the query in stages, using a technique like one of the ones from "Equivalent of LIMIT and OFFSET for SQL Server?" I'd start with something like this (SQL Fiddle):
DECLARE #offset INT = 0
SELECT TOP 50000000 *
FROM (
SELECT *,
ROW_NUMBER() over (order by column1) AS r_n_n
FROM t1
) xx
WHERE r_n_n >= #offset
EXCEPT
SELECT TOP 50000000 *
FROM (
SELECT *,
ROW_NUMBER() over (order by column1) AS r_n_n
FROM t2
) xx
WHERE r_n_n >= #offset
Then you can increment #offset by the amount of TOP n and do it again. This will likely involve some trial and error to find the limit for the TOP n clause that will run to completion rather than throw an error. I'd start with half, then try quarters, eighths, etc. as necessary.

Add additional information to joined rows

How can I add additional information to joined rows?
SELECT column1, column2
FROM table1
LEFT JOIN table2
ON column1 = col1_table2 //The row which matches with this join should have additional information e.g. "'joined' AS info
Thanks a lot for your help!
If I understand your question, this should work:
SELECT
t1.column1,
t1.column2,
CASE WHEN t2.col1_table2 IS NOT NULL THEN 'joined' END AS info
FROM table1 t1
LEFT JOIN table2 t2 ON t1.column1 = t2.col1_table2
try this:
SELECT column1,
column2,
CASE WHEN col1_table2 IS NOT NULL THEN 'joined' END AS additional_field
FROM table1
LEFT JOIN table2 ON column1 = col1_table2

Access function return value in joining table column

The partial select statement is:
SELECT t1.Column1, t1.Column2, **"Column3"** = dbo.FunctionName(Column1, Column2)
FROM Table1 t1
Now I wanted to left join Table2 in above query but join needs to be "Column3" = Table2.Column3; something like as below query which does not work and how to achieve that.
SELECT t1.Column1, t1.Column2, Column3 = dbo.FunctionName(Column1, Column2),
t2.Abc, t2.bcd
FROM Table1 t1 LEFT OUTER JOIN Table2 t2 ON t2.Column3 = ????
Thanks in advance.
Did you try
SELECT
t1.Column1,
t1.Column2,
Column3 = dbo.FunctionName(Column1, Column2)
t2.Abc, t2.bcd
FROM
Table1 t1
LEFT OUTER JOIN Table2 t2
ON t2.Column3 = dbo.FunctionName(t1.Column1, t1.Column2)
This should also work
SELECT
t1.Column1,
t1.Column2,
t1.Column3,
t2.Abc,
t2.bcd
FROM
Table2 t2 LEFT JOIN JOIN
(SELECT t1.Column1, t1.Column2, Column3 = dbo.FunctionName(Column1, Column2)
FROM Table1 t1) t1
ON t2.Column3 = t1.Column3