Solved

Join query on access

Posted on 2011-02-15
7
857 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

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…
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…
Familiarize people with the process of utilizing SQL Server views 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 Microsoft Access…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

746 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

12 Experts available now in Live!

Get 1:1 Help Now