Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Join query on access

Posted on 2011-02-15
7
Medium Priority
?
911 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 27

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 59

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 59

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 59

Accepted Solution

by:
HainKurt earned 2000 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 27

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

722 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