Solved

Join query on access

Posted on 2011-02-15
7
883 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

785 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