ioustinos_sarris
asked on
Join query on access
Hi there,
In access i have a table A and a table B.
A.asid is the primary key and B.asid is the foreign key.
I need the following set of results
I need
all records of A that do not appear in B OR appear in B and have B.smth = 1
moreover i need the following to stand true
A.check1 = 1 and
A.check2 = 1 and
A.check3 is not null
Can you help me with the query???
Thanks
In access i have a table A and a table B.
A.asid is the primary key and B.asid is the foreign key.
I need the following set of results
I need
all records of A that do not appear in B OR appear in B and have B.smth = 1
moreover i need the following to stand true
A.check1 = 1 and
A.check2 = 1 and
A.check3 is not null
Can you help me with the query???
Thanks
try this:
select a.*, b.asisid as b_asisid from (
select * from a left join b on a.asisid= b.asisid
) x where ((b_asisid is null) or (b_asisid is not null and b.smth=1))
and A.check1 = 1 and
and A.check2 = 1 and
and A.check3 is not null
select a.*, b.asisid as b_asisid from (
select * from a left join b on a.asisid= b.asisid
) x where ((b_asisid is null) or (b_asisid is not null and b.smth=1))
and A.check1 = 1 and
and A.check2 = 1 and
and A.check3 is not null
ASKER
@shaun kline
I used your query and it seems almost perfect.
I have two problems which probably occur due to me giving wrong specification
The relation A-B is one to many.
A) If at least one b.smth for one record of A is 0 then i do not want that A record in the results set
B) If one record of A has two instances on B with b.smth = 1 then i need only one appearance of A in the results set
Could you help me futhermore please?
I used your query and it seems almost perfect.
I have two problems which probably occur due to me giving wrong specification
The relation A-B is one to many.
A) If at least one b.smth for one record of A is 0 then i do not want that A record in the results set
B) If one record of A has two instances on B with b.smth = 1 then i need only one appearance of A in the results set
Could you help me futhermore please?
try this( this query will not produce duplicate a record):
Select * from [table a] as a
where a.check1 = 1
and a.check2 = 1
and a.check3 is not null
and a.asid not in (select asid from B group by asid having sum(iif(b_smth=0,1,0))>0)
Select * from [table a] as a
where a.check1 = 1
and a.check2 = 1
and a.check3 is not null
and a.asid not in (select asid from B group by asid having sum(iif(b_smth=0,1,0))>0)
ASKER
@HainKurt
I do not understand the syntax iif(b_smth=0,1,0))>0)
B.smth actually takes the value "trial" and "regular" not 0 and 1
Could you syntax it for me once again because every effort of mine produced a syntax error??
Will this query produce exactly what i needed???
I do not understand the syntax iif(b_smth=0,1,0))>0)
B.smth actually takes the value "trial" and "regular" not 0 and 1
Could you syntax it for me once again because every effort of mine produced a syntax error??
Will this query produce exactly what i needed???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you only want the record from table a without any data from table b, try this:
select *
from [table a]
where check1 = 1
and check2 = 1
and check3 is not null
and asid not in (select asid from [table b] where smth = 0)
If you want values from one record in table b, you will need to let us know what field (including data type) will make the distinction and whether it should be the first (min) or last (max).
select *
from [table a]
where check1 = 1
and check2 = 1
and check3 is not null
and asid not in (select asid from [table b] where smth = 0)
If you want values from one record in table b, you will need to let us know what field (including data type) will make the distinction and whether it should be the first (min) or last (max).
Select * from [table a] as A
left outer join [table b] as B on A.asid = B.asid
where a.check1 = 1
and a.check2 = 1
and a.check3 is not null
and (b.asid is null or b.smth = 1)