Solved

Join query on access

Posted on 2011-02-15
7
868 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
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…

943 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now