I have two tables A(column1,column2) and B(column1,column2).
How to ensure that a value from A(column1) is not contained within B(column1) and insert it in this column.
My Query will be like this :
insert into B.column1 values()
where
...
I want to complete B.column1 with data from A.column1
What should i put in the where clause ?
Insert Into B(column1)
Select A.Column1
From A
Where A.Column1 not in (Select Column1 From B)
I would use MINUS command and select all rows from A(column1) which are not in B(column1) and then SELECT INTO result into B table.
insert into B
select a.column1, a.column2 from a
left join b
on a.column1 = b.column1
where b.column1 is null
Related
I couldn't figure out how to remove mirror results like this:
select
b.column1 as result1,
c.column2 as result2
from table a
left join table b on a.column1 = b.column1
left join table c on a.column2 = c.column1;
The results I get are the following:
result1|results2
b1 |b22
b5 |b66
b74 |b31
......
b22 |b1
b66 |b5
b31 |b74
How could I get only the first combination - if there is a combination b1-b22, I don't need b22-b1.
I've tried which distinct on the sum of b.column1 and c.column1 - I casted them to int and it works, but I don't believe it's the best way since there could be duplicated sums coming from different combinations and I'll lose some data.
This is a bit tricky if you don't have all combinations. Your sample results do not have null, so I will change the joins to inner joins and then use distinct on:
select distinct on (least(b.column1, c.column2), greatest(b.column1, c.column2))
b.column1 as result1, c.column2 as result2
from table a join
table b
on a.column1 = b.column1 join
table c
on a.column2 = c.column1
order by least(b.column1, c.column2), greatest(b.column1, c.column2);
Actually as your query is phrased, the joins don't seem needed at all. So you might consider:
select distinct on (least(a.column1, a.column2), greatest(a.column1, a.column2))
a.column1 as result1, a.column2 as result2
from table a
order by least(a.column1, a.column2), greatest(a.column1, a.column2);
One method is to add an inequality condition in the last join:
select
b.column1 as result1,
c.column2 as result2
from table a
left join table b on a.column1 = b.column1
left join table c on a.column2 = c.column1 and b.column1 < c.column2
"Remove mirror results" that sounds like you are for delete. This seems best addressed with a Exists clause:
delete from rtab r1
where r1.result1 > r1.result2
and exists (select null
from rtab r2
where r1.result1 = r2.result2
and r1.result2 = r2.result1
);
If we have 2 tables, tableA (with column1, column2) and tableB (with column1, column2), what's the difference between the following two queries? Which one has better performance? What if we have indexing for both tables?
Query #1:
select
b.column2
from
tableA a,
tableB b
where
a.column1 = b.column1
and a.column2 = ?;
Query #2:
select
b.column2
from
tableA a
inner join
tableB b on a.column1 = b.column1
where
a.column2 = ?;
2nd query has better performance.
You are using cross join in your first query and then filtering the results. Imagine having 10000 records in both the tables, it will produce 10000*10000 combinations.
Both will perform equally. One is an ansi style and the other is old fashioned style of joining
You may compare the explain plans and most likely you will find them to be the same.
I have append Queries A, B to Table C. I am creating a report based off C, but I need the distinct rows. Now I could select the distinct rows from C, but I want to delete them as I go (ie so Table C does not contain 1,000,000,000+ records over time for each append), so the report has ALL the unique records from C, past, present, future until the end user deletes them.
My question is simply this. Is there any way to append only distinct (not append distinct, rather append to the table distinct) rows to Table C?
If not directly possible, VBA?
Use constraints to enforce this behavior, in this case a (composite) primary key: https://support.office.com/en-us/article/Add-or-change-a-table-s-primary-key-in-Access-07b4a84b-0063-4d56-8b00-65f2975e4379
This will make sure, that you can't insert duplicate values into your table, which means that you won't have to delete the duplicates later on.
Define the primary key over all the columns that make a dataset unique.
Before adding a pk or constraint make sure to clean up your data though in order to remove all duplicate rows. The easiest way would probably to create a new table and fill it by using a SELECT DISTINCT .... from your current table.
Consider using any of the three NOT IN, NOT EXISTS, or LEFT JOIN...NULL queries to append data not currently in that table. Below assumes a primary key, ID, is used for the distinctness.
INSERT INTO TableC (Column1, Column2, Column3)
SELECT a.Column1, a.Column2, a.Column3
FROM QueryA a
LEFT JOIN TableC c
ON a.ID = c.ID
WHERE c.ID IS NULL;
INSERT INTO TableC (Column1, Column2, Column3)
SELECT a.Column1, a.Column2, a.Column3
FROM QueryA a
WHERE NOT EXISTS
(SELECT 1 FROM TableC c
WHERE a.ID = c.ID);
INSERT INTO TableC (Column1, Column2, Column3)
SELECT a.Column1, a.Column2, a.Column3
FROM QueryA a
WHERE a.ID NOT IN
(SELECT c.ID FROM TableC c);
Now, if no single column but multiple fields denote uniqueness, add to the JOIN or WHERE clauses:
...
FROM QueryA a
LEFT JOIN TableC c
ON a.Column1 = c.Column1 AND a.Column2 = c.Column2 AND a.Column3 = c.Column3
WHERE a.Column1 IS NULL OR a.Column2 IS NULL OR a.Column3 IS NULL;
...
WHERE NOT EXISTS
(SELECT 1 FROM TableC c
WHERE a.Column1 = c.Column1 AND a.Column2 = c.Column2 AND a.Column3 = c.Column3);
...
WHERE a.Column1 NOT IN
(SELECT c.Column1 FROM TableC c)
AND a.Column2 NOT IN
(SELECT c.Column2 FROM TableC c)
AND a.Column3 NOT IN
(SELECT c.Column3 FROM TableC c);
Backstory,
My company runs redundant call recording servers, each with a list of extensions.
We query these using SQL. I can see there is a 20+ extension difference between the two servers. These are columns that exist in the same table...so essentially I need to do the following:
Compare column1 data from column 2 'server1' in table system.name with column1 data from column 2 'server2' in table system.name and display those that DO NOT exist on both, but exist on one or the other.
Based on what I can undertand from your question
Select column1
from table1 a
where column2 = 'server1'
and not exists
(select *
from table1 b
where a.column1 = b.column1
and b.column2 = 'server2'
)
UNION
Select column1
from table1 a
where column2 = 'server2'
and not exists
(select *
from table1 b
where a.column1 = b.column1
and b.column2 = 'server1'
)
I got error on the following query
INSERT INTO tableName3 (column1, column2)
SELECT
b.column1,
SUM (a.column2) AS SumColumn2
FROM
tableName1 AS a
JOIN
tableName2 AS b ON a.column1 = b.column2
GROUP BY
b.column2
ON DUPLICATE KEY UPDATE tableName3
SET column2 = SUM (a.column2) AS SumColumn2
The error messages are
Incorrect syntax near the keyword 'ON*'
and
Incorrect syntax near the keyword 'AS'
How to fix it?
First, your select statement is wrong.
SELECT
b.column1,
SUM (a.column2) AS SumColumn2
FROM
tableName1 AS a
JOIN
tableName2 AS b ON a.column1 = b.column2
GROUP BY
b.column2
this should probably be grouped by b.column1, otherwise you will get an exception since columns in the select clause must appear either in the group by clause or in an aggregating function in sql-server
Second, there is no ON DUPLICATE KEY directive in Sql server. A Quick search found many references for this in mysql, but mysql is not sql-server.
To achieve this kind of behavior in Sql server you should probably use MERGE statement.
Your code should look something like this:
MERGE tableName3 AS target
USING (
SELECT
b.column1,
SUM (a.column2) AS SumColumn2
FROM
tableName1 AS a
JOIN
tableName2 AS b ON a.column1 = b.column2
GROUP BY
b.column1
) AS source (column1, SumColumn2)
ON (target.column1= source.column1)
WHEN MATCHED THEN
UPDATE SET column2= source.SumColumn2
WHEN NOT MATCHED THEN
INSERT (column1, column2)
VALUES (source.column1, source.SumColumn2)