Different Values In Two SQL Tables - sql

I'm trying to select different values from two tables in SQL but my code isn't working. The first part of it works:
SELECT distinct a.c1, b."Commodity.Code"::numeric FROM coletados a, commod b
WHERE a.c1 = b."Commodity.Code"::numeric
But when I try to select different values, it doesn't work. My entire SQL statement is:
SELECT * FROM commod b
WHERE b."Commodity.Code"::numeric =!
(SELECT DISTINCT a.c1, b."Commodity.Code"::numeric
FROM coletados a, commod b
WHERE a.c1 = b."Commodity.Code"::numeric)
In reality, I just want the column of numbers that are different in the two tables, so I don't need the '*', but I don't know if I can select the same variable (a.c1 or b."Commodity.Code") twice. Thanks for all the help.

You are comparing one value to two values. In Postgres, one method is:
select *
from commod b
where (b.c1, "Commodity.Code"::numeric) not in (select a.c1, a."Commodity.Code"::numeric
from coletados a
);
Or, using your approach:
select *
from commod b
where "Commodity.Code"::numeric not in (select a."Commodity.Code"::numeric
from coletadoa a
where a.c1 = b.c1
);
That is, the subquery does not need a join, just a correlation clause.

Related

Select Table A minus Table B where condition consists of two columns

I have two tables. Table A and table B.
I would like to select everything from table A which is NOT in table B.
Sounds easy the catch is I need to select it based on two values (two columns)
revision AND casetype. Something like this.
select a.revision, a.casetype from A a
minus
select b.revision, b.casetype from B b;
The problem is I won't get back ID from table A.
Is it possible to select whole table A minus table B where conditions consist of two columns ? I would like to stick to SQL (no PL/SQL)
I also tried to write something like query below but I guess I can't do it since I need to check revision AND casetype altogether
select * from A a where a.casetype IN (select...) and a.revision IN (select...)
Any idea how to work around ? Thanks
Sure, I believe a basic not exists check should work.
select a.id, a.revision, a.casetype
from A a
where not exists (
select 1
from B
where revision = a.revision and casetype = a.casetype
);
Oracle supports tuples, so if you wanted you could do:
select a.*
from a
where (a.revision, a.casetype) in (select a.revision, a.casetype from A a
minus
select b.revision, b.casetype from B b
);
I would normally go for not exists, but this is the solution that builds on what you have already done.
except should work
select a.revision, a.casetype from A a
except
select b.revision, b.casetype from B b;

Returning only duplicate rows from two tables

Every thread I've seen so far has been to check for duplicate rows and avoiding them. I'm trying to get a query to only return the duplicate rows. I thought it would be as simple as a subquery, but I was wrong. Then I tried the following:
SELECT * FROM a
WHERE EXISTS
(
SELECT * FROM b
WHERE b.id = a.id
)
Was a bust too. How do I return only the duplicate rows? I'm currently going through two tables, but I'm afraid there are a large amount of duplicates.
use this query, maybe is better if you check the relevant column.
SELECT * FROM a
INTERSECT
SELECT * FROM b
I am sure your posted code would work too like
SELECT * FROM a
WHERE EXISTS
(
SELECT 1 FROM b WHERE id = a.id
)
You can as well do a INNER JOIN like
SELECT a.* FROM a
JOIN b on a.id = b.id;
You can as well use a IN operator saying
SELECT * FROM a where id in (select id from b);
If none of them, then you can use UNION if both table satisfies the union restriction along with ROW_NUMBER() function like
SELECT * FROM (
SELECT *,
ROW_NUMBER() OVER(PARTITION BY id ORDER BY id) AS rn
FROM (
select * from a
union all
select * from b) xx ) yy
WHERE rn = 1;
Note: there's an ambiguity as to what you mean by a duplicate row, and whether you're talking about duplicate keys, or all fields being the same. My answer deals with all fields being the same; some of the others are assuming it's just the keys. It's unclear which you intend.
You might try
SELECT id, col1, col2 FROM a INNER JOIN b ON a.id = b.id
WHERE a.col1 = b.col1 AND a.col2 = b.col2
adding in other columns as necessary. The database engine should be intelligent enough to do the comparisons on the indexed columns first, so it'll be efficient as long as you don't have rows that are different only on lots of non-indexed fields. (If you do, then I don't think anything will do it particularly efficiently.)

Select returns the same row multiple times

I have the following problem:
In DB, I have two tables. The value from one column in the first table can appear in two different columns in the second one.
So, the configuration is as follows:
TABLE_A: Column Print_group
TABLE _B: Columns Print_digital and Print_offset
The value from the different rows and Print_group column of the Table_A can appear in one row of the Table_B but in different column.
I have the following query:
SELECT DISTINCT * FROM Table_A
INNER JOIN B ON (Table_A. Print_digital = Table_B.Print_group OR
Table_A.Print_offset = Table_B.Print_group)
The problem is that this query returns the same row from the Table_A two times.
What I am doing wrong? What is the right query?
Thank you for your help
If I'm understanding your question correctly, you just need to clarify your fields to come from Table_A:
SELECT DISTINCT A.*
FROM Table_A A
INNER JOIN B ON A.Print_digital = B.Print_group
OR A.Print_offset = B.Print_group
EDIT:
Given your comments, looks like you just need SELECT DISTINCT B.*
SELECT DISTINCT B.*
FROM Table_A A
INNER JOIN B ON A.Print_digital = B.Print_group
OR A.Print_offset = B.Print_group
I've still another question... first,to be clear, the right query version is
SELECT DISTINCT A.*
FROM Table_A A
INNER JOIN B ON A.Print_digital = B.Print_group
OR A.Print_offset = B.Print_group.
If I want it returns also one column from the B table it again returns duplicate rows. My query (the bad one) is the following one:
SELECT DISTINCT A.*, B.Id
FROM Table_A A
INNER JOIN B ON A.Print_digital = B.Print_group
OR A.Print_offset = B.Print_group

Join SQL query to get data from two tables

I'm a newbie, just learning SQL and have this question: I have two tables with the same columns. Some registers are in the two tables but others only are in one of the tables. To illustrate, suppose table A = (1,2,3,4), table B=(3,4,5,6), numbers are registers. I need to select all registers in table B if they are not in table A, that is result=(5,6). What query should I use? Maybe a join. Thanks.
You can either use a NOT IN query like this:
SELECT col from A where col not in (select col from B)
or use an outer join:
select A.col
from A LEFT OUTER JOIN B on A.col=B.col
where B.col is NULL
The first is easier to understand, but the second is easier to use with more tables in the query.
Select register from TABLE_B b
Where not exists (Select register from TABLE_A a where a.register = b.register)
I assumed you have a column named register in TABLE_A and TABLE_B

Why is the ( ) mandatory in the SQL statement "select * from gifts INNER JOIN sentgifts using (giftID);"?

Why is the ( ) mandatory in the SQL statement
select * from gifts INNER JOIN sentgifts using (giftID);
? The ( ) usually is for specifying grouping of something. But in this case, are we supposed to be able to use 2 or more field names...? in the example above, it can be all clear that it is 1 field, is it just that the parser is not made to bypass the ( ) when it is all clear? (such as in the language Ruby).
Yes you can specify multiple columns inside of the using clause like below.
using(col_1,col_2,col_3)
You can in fact specify a list of columns, check here.
And it's mandatory because syntax exceptions blow the parser and are bad for performance.
That said one could argue that USING itself is already just syntactic sugar, it's just a shortcut of specifying a join with the normal ON syntax in case the column names are the same for the joined tables.
Since you can join ON multiple columns USING is just doing the same thing.
The following statements are therefore equivalent:
SELECT A.c1, A.c2, A.c3 FROM TABLE_A A
JOIN TABLE_B B
ON A.c1 = B.c1
ON A.c2 = B.c2
ON A.c3 = B.c3
and
SELECT A.c1, A.c2, A.c3 FROM TABLE_A A
JOIN TABLE_B B
USING (c1,c2,c3)