Solved

Join query on access

Posted on 2011-02-15
7
891 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:ioustinos_sarris
  • 3
  • 2
  • 2
7 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 34898407
Try this:

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)
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34898430
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

0
 

Author Comment

by:ioustinos_sarris
ID: 34902507
@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?
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 51

Expert Comment

by:HainKurt
ID: 34903369
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)
0
 

Author Comment

by:ioustinos_sarris
ID: 34906769
@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???
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 34908330
sum(iif(b_smth=0,1,0))>0)

if b_smth=0 then return 1 else 0
then we sum all these
then if this sum is >1 it means there is at least one record that b_stmt is 0

rec b_stmth iif(b_smth=0,1,0))>0)
A    0            1
A    1            0
A    1            0
B    0            1
B    1            0
C    1            0
C    1            0

sum (iif(b_smth=0,1,0))
A     1  >>> we do not want this record
B     1  >>> we do not want this record
C     0
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 34909616
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).
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question