Table 1:
Name Address Height Weight
X. y z. p
G. H. I J
Q W. E. R
Table 2:
Name Income Tax
X. y z
A B C
Trying compare the rows between the two tables and get the names that appear only in table 1 and not in table 2 as well? However, I am only trying to compare only certain rows from table_1, for example – the rows which contain Height= I and E do not need to be on the count.
Here is what I have so far but I am getting a syntax error:
SELECT count(DISTINCT "Name") FROM table_1 WHERE table_1.Height NOT LIKE ANY CONCAT('%', {'I', 'E'} '%')
WHERE NOT EXISTS (
SELECT DISTINCT UPPER("Name") FROM table_2 WHERE table_2."Income" = 'y' AND UPPER(table_2."Name") LIKE CONCAT('%', UPPER(table_1."Name"), '%')
)
syntax error at or near "WHERE"
The result of the query should be 1 as there is a count() but without count it should look like
Name Address Height Weight
X. y z. p
3
Answers
Sorry,for me it is not very clear what you want to achieve, but based on this " get the names that appear only in table 1 and not in table 2 as well? However, I am only trying to compare only certain rows from table_1, for example – the rows which contain Height= I and E do not need to be on the count"
Can you left join table 2 to table 1 and filter for where name in table 2 is null?
ie:
Here’s what I cam up with. You do know that this will have no results…
create table Table_1
(Name varchar(2)
,Address varchar(2)
,Height varchar(2)
,Weight varchar(2)); commit;
–=================================================================================
insert into Table_1 (Name, Address, Height, Weight) values (‘X.’, ‘y’, ‘z.’, ‘p’); commit;
insert into Table_1 (Name, Address, Height, Weight) values (‘G.’, ‘H.’, ‘I’, ‘J’); commit;
insert into Table_1 (Name, Address, Height, Weight) values (‘Q’, ‘W.’, ‘E.’, ‘R’); commit;
— select * from Table_1
–=================================================================================
–=================================================================================
create table Table_2
(Name varchar(2)
,Income varchar(2)
,Tax varchar(2)); commit;
–=================================================================================
insert into Table_2 (Name, Income, Tax) values (‘X.’, ‘y’, ‘z’); commit;
insert into Table_2 (Name, Income, Tax) values (‘A’, ‘B’, ‘C’); commit;
— select * from Table_2
–=================================================================================
–=================================================================================
select distinct name from Table_1 t1 where t1.height not in (‘I’,’E.’) and t1.Name not in (select distinct Name from Table_2);