dbTalk Databases Forums  

Query Showing Common Values

comp.databases.ms-access comp.databases.ms-access


Discuss Query Showing Common Values in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
mpdsal
 
Posts: n/a

Default Query Showing Common Values - 01-10-2011 , 05:38 PM






I hope I can word this so that you can understand. I am using Access
2007 and I have a simple query that returns the following results:


User_ID SAP Role
smith abc:je entry

But within the results of this query there are roles that are common
to many users. I want to find the same roles that are assigned to
users and then to find the roles that are different. Does this make
sense? This way I can show all roles that users have that are the same
and then conversely show which roles some people have and others
don't.

I appreciate the assistance.

Mark

Reply With Quote
  #2  
Old   
Bob Barrows
 
Posts: n/a

Default Re: Query Showing Common Values - 01-11-2011 , 07:42 AM






mpdsal wrote:
Quote:
I hope I can word this so that you can understand. I am using Access
2007 and I have a simple query that returns the following results:


User_ID SAP Role
smith abc:je entry

But within the results of this query there are roles that are common
to many users. I want to find the same roles that are assigned to
users and then to find the roles that are different. Does this make
sense? This way I can show all roles that users have that are the same
and then conversely show which roles some people have and others
don't.

I'm not sure I understand. Show us some sample data, then show us the
results you want from that sample data - a picture is worth a thousand
words. Additionally, going through this exercise might allow you to figure
out how to do it on your own.

Reply With Quote
  #3  
Old   
mpdsal
 
Posts: n/a

Default Re: Query Showing Common Values - 01-11-2011 , 09:29 AM



On Jan 11, 8:42*am, "Bob Barrows" <reb01... (AT) NOyahooSPAM (DOT) com> wrote:
Quote:
mpdsal wrote:
I hope I can word this so that you can understand. I am using Access
2007 and I have a simple query that returns the following results:

User_ID * SAP Role
smith * * * abc:je entry

But within the results of this query there are roles that are common
to many users. I want to find the same roles that are assigned to
users and then to find the roles that are different. Does this make
sense? This way I can show all roles that users have that are the same
and then conversely show which roles some people have and others
don't.

I'm not sure I understand. Show us some sample data, then show us the
results you want from that sample data - a picture is worth a thousand
words. Additionally, going through this exercise might allow you to figure
out how to do it on your own.

Bob,

Good point. Here is what the data looks like: ColA is User ID and ColB
is RoleName. What I would like to do is show the results of all roles
that are common to users, in this case MarkD, BillS, and JoeD have
Role2 assigned. This way I can prepare a report that says these users
all have the same role(s) assigned to them. Does this make it a bit
more clearer?


UserID RoleName

MarkD Role1
MarkD Role2
MarkD Role3
BillS Role1
BillS Role2
BillS Role26X
JoeD Role2
JoeD Role3
JoeD Role42B

So my new query showing common roles to each user would look like
this:
MarkD Role2
BillS Role2
JoeD Role2

Thanks

Mark

Reply With Quote
  #4  
Old   
Bob Barrows
 
Posts: n/a

Default Re: Query Showing Common Values - 01-11-2011 , 04:04 PM



mpdsal wrote:
Quote:
Bob,

Good point. Here is what the data looks like: ColA is User ID and ColB
is RoleName. What I would like to do is show the results of all roles
that are common to users, in this case MarkD, BillS, and JoeD have
Role2 assigned. This way I can prepare a report that says these users
all have the same role(s) assigned to them. Does this make it a bit
more clearer?


UserID RoleName

MarkD Role1
MarkD Role2
MarkD Role3
BillS Role1
BillS Role2
BillS Role26X
JoeD Role2
JoeD Role3
JoeD Role42B

So my new query showing common roles to each user would look like
this:
MarkD Role2
BillS Role2
JoeD Role2

Thanks

Mark
Common to _all_ users is more correct isn't it? If BillS had Role3 in
addition to the others the results should be

MarkD Role2
BillS Role2
JoeD Role2
MarkD Role3
BillS Role3
JoeD Role3

Correct?
One way would be to compare the count of users for each role to the count of
distinct users in the database. The cleanest way to explain and do this is
by using saved queries:

Saved query 1 - DistinctUsersCount:
select count(*) as CountDistinct from (select distinct userid from
tablename) as q

Saved query 2 - CommonRoles:
select rolename,count(*) from tablename
group by rolename
having count(*) = (select CountDistinct FROM DistinctUsersCount)

Final query:
select userid,rolename from tablename as t
where exists (select * from CommonRoles as c
where = c.rolename = t.rolename)

Reply With Quote
  #5  
Old   
Bob Barrows
 
Posts: n/a

Default Re: Query Showing Common Values - 01-12-2011 , 06:42 AM



Bob Barrows wrote:
Quote:
where = c.rolename = t.rolename)
Oops, an extra "=" in there - how'd that happen??
where c.rolename = t.rolename)

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.