Query troubles

M

melinda.pluma

I have a query that I am trying to pull out only the Club Members that
have Remote Access.
I thought this would be crazy easy and the information that I have is
not accurate. Please Help!!!



SELECT [Remote Access Users].[First Name], [Remote Access Users].[Last
Name], [Club Members 2007].[FC #], [Club Members 2007].[Club Level],
[Remote Access Users].Active, [Remote Access Users].Branch
FROM [Club Members 2007] LEFT JOIN [Remote Access Users] ON [Club
Members 2007].[FC #] = [Remote Access Users].[FC #]
WHERE ((([Remote Access Users].Active)="Yes"));

Mindy
 
J

John Spencer

IS Active a text field or a Yes/No (boolean field)?

If it is a boolean field then try

SELECT [Remote Access Users].[First Name]
, [Remote Access Users].[Last Name]
, [Club Members 2007].[FC #]
, [Club Members 2007].[Club Level]
, [Remote Access Users].Active
, [Remote Access Users].Branch
FROM [Club Members 2007] INNER JOIN [Remote Access Users]
ON [Club Members 2007].[FC #] = [Remote Access Users].[FC #]
WHERE [Remote Access Users].Active <> 0

Also if you are going to apply criteria to Active then you might as well use
an inner join


--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 
M

melinda.pluma

IS Active a text field or a Yes/No (boolean field)?

If it is a boolean field then try

SELECT [Remote Access Users].[First Name]
, [Remote Access Users].[Last Name]
, [Club Members 2007].[FC #]
, [Club Members 2007].[Club Level]
, [Remote Access Users].Active
, [Remote Access Users].Branch
FROM [Club Members 2007] INNER JOIN [Remote Access Users]
ON [Club Members 2007].[FC #] = [Remote Access Users].[FC #]
WHERE [Remote Access Users].Active <> 0

Also if you are going to apply criteria to Active then you might as well use
an inner join

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
.




I have a query that I am trying to pull out only the Club Members that
have Remote Access.
I thought this would be crazy easy and the information that I have is
not accurate. Please Help!!!
SELECT [Remote Access Users].[First Name], [Remote Access Users].[Last
Name], [Club Members 2007].[FC #], [Club Members 2007].[Club Level],
[Remote Access Users].Active, [Remote Access Users].Branch
FROM [Club Members 2007] LEFT JOIN [Remote Access Users] ON [Club
Members 2007].[FC #] = [Remote Access Users].[FC #]
WHERE ((([Remote Access Users].Active)="Yes"));
Mindy- Hide quoted text -

- Show quoted text -

Active is a text field, because I don't know how to work with a Yes/No
in a query... I also don't know what an inner join is. Sorry
 
J

John Spencer

SELECT [Remote Access Users].[First Name]
, [Remote Access Users].[Last Name]
, [Club Members 2007].[FC #]
, [Club Members 2007].[Club Level]
, [Remote Access Users].Active
, [Remote Access Users].Branch
FROM [Club Members 2007] INNER JOIN [Remote Access Users]
ON [Club Members 2007].[FC #] = [Remote Access Users].[FC #]
WHERE [Remote Access Users].Active = "Yes"

So would you like to explain how your results differ from what you think you
should be getting? No records, wrong records, all records, too many
records? How do you know the results are "not accurate"?
--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
I have a query that I am trying to pull out only the Club Members that
have Remote Access.
I thought this would be crazy easy and the information that I have is
not accurate. Please Help!!!
SELECT [Remote Access Users].[First Name], [Remote Access Users].[Last
Name], [Club Members 2007].[FC #], [Club Members 2007].[Club Level],
[Remote Access Users].Active, [Remote Access Users].Branch
FROM [Club Members 2007] LEFT JOIN [Remote Access Users] ON [Club
Members 2007].[FC #] = [Remote Access Users].[FC #]
WHERE ((([Remote Access Users].Active)="Yes"));
Mindy- Hide quoted text -

- Show quoted text -

Active is a text field, because I don't know how to work with a Yes/No
in a query... I also don't know what an inner join is. Sorry
 
C

Chris2

I also don't know what an inner join is. Sorry

Melinda,

Whenever you run a Query (SELECT, UPDATE, INSERT, DELETE, SELECT...INTO,
TRANSFORM...PIVOT) with more than two tables, you must instruct the database on what
columns are used to "join" the tables together.

INNER JOIN means that rows in both tables are returned where values match in both sets of
columns (one set from each table) that are used in the "join".

LEFT JOIN means that all rows in the left-hand table are returned, and when a row in the
right-hand table has no match for the specified "join" conditions, NULLS are returned in
any output columns of the right-hand table.

RIGHT JOIN means that all rows in the right-hand table are returned, and when a row in the
left-hand table has no match for the specified "join" conditions, NULLS are returned in
any output columns for the left-hand table.

"Left" and "right" refer to SQL code. It means to the left and right of the join keyword
of the SELECT statement (INNER JOIN, LEFT JOIN, RIGHT JOIN are all join keywords).


INNER JOIN Example:

SELECT L1.Key
,R1.Key
FROM LeftHandTable AS L1
INNER JOIN
RightHandTable AS R1
ON L1.Key = R1.Key

Rows in both tables are returned every time L1.Key and R1.Key values match. No other rows
are returned.


LEFT JOIN Example:

SELECT L1.Key
,R1.Key
FROM LeftHandTable AS L1
LEFT JOIN
RightHandTable AS R1
ON L1.Key = R1.Key

All rows in LeftHandTable are retained. Any time there is no R1.Key value for an L1.Key
value, the R1.Key output value is Null.

Left/Right-ness can better be demonstrated if written as shown below, but due to reasons
of horizontal crowding (which reduces visual clarity), it usually isn't written this way.

FROM LeftHandTable AS L1 LEFT JOIN RightHandTable AS R1
ON L1.Key = R1.Key

LeftHandTable is effectively to the "left" of the LEFT JOIN keyword.


RIGHT JOIN Example:

SELECT L1.Key
,R1.Key
FROM LeftHandTable AS L1
RIGHT JOIN
RightHandTable AS R1
ON L1.Key = R1.Key

All rows in RightHandTable are retained. Any time there is no L1.Key value for an R1.Key
value, the L1.Key output value is Null.


CARTESIAN JOIN:

If you leave these instructions (join names) out of a query, the database will join every
row in the first table with every row in the second table. The number of output rows
equals the number of rows in the first table multiplied by the number of rows in the
second table. This is called a Cartesian Join (the results are called a Cartesian
Product), and is not used except for very specific queries.

For two large tables, say with 100,000 rows each, the output would be 10,000,000,000 rows.


EXAMPLES IN PRACTICE:

Two example tables:

Items:
ItemID -- Primary Key
ItemName

ItemID, ItemName
1, Popcorn
2, Chips
3, Soda
4, Fillet Mignon

Prices:
PriceID -- Primary Key
ItemID
ItemPrice
StartDate
EndDate

PriceID, ItemID, ItemPrice, StartDate, EndDate
1, 1, 1.00, 06/01/2007, 06/08/2007
2, 2, 3.50, 06/01/2007, 06/08/2007
3, 3, 1.00, 06/01/2007, 06/08/2007

INNER JOIN Example:

SELECT I1.ItemName
,P1.ItemPrice
FROM Items AS I1
INNER JOIN
Prices AS P1
ON I1.ItemID = P1.ItemID

Returns
ItemName, Price
Popcorn, 1.00
Chips, 3.50
Soda, 1.00

Notice that Fillet Mignon does not appear. There is no = match (after the ON clause) for
ItemID between the two tables.


LEFT JOIN Example:

SELECT I1.ItemName
,P1.ItemPrice
FROM Items AS I1
LEFT JOIN
Prices AS P1
ON I1.ItemID = P1.ItemID

Returns
ItemName, Price
Popcorn, 1.00
Chips, 3.50
Soda, 1.00
Fillet Mingon, Null

Fillet Mignon does appear now, even though there is no match on ItemID between the two
tables. The output column (ItemPrice) from the "right hand" table (Prices) leaves a Null
behind.


Notes:

The above joins are conducted on table primary keys. Joins can be done on any column.
Usually they are done on columns that represent the same types of information, like
numbers, dates, words, etc. With conversion functions like CInt() and CStr(), numbers and
words can be compared in a join. Usuing functions in a join is generally not a first
choice as it will usually stop the use of any indexes, and this can slow down many
queries.

Joins are usually done on primary keys, or between primary keys and foreign keys, because
these columns represent either the final way of identifying the table rows, or are the
natural information connections between the tables.


Extras:

DDL SQL (These queries are usable to create the two tables above so you can run the last
two queries above on your own. You copy and paste these, one each, into the SQL View of
an MS Access Query, and then execute the query. You will need to manually enter in the
sample data noted above into the tables created this way.)

CREATE TABLE Items
(ItemID AUTOINCREMENT
,ItemName TEXT(255) NOT NULL
,CONSTRAINT pk_Items
PRIMARY KEY (ItemID)
)

CREATE TABLE Prices
(PriceID AUTOINCREMENT
,ItemID INTEGER NOT NULL
,ItemPrice CURRENCY NOT NULL
,StartDate DATETIME NOT NULL
,EndDate DATETIME
,CONSTRAINT pk_Prices
PRIMARY KEY (PriceID)
,CONSTRAINT fk_Prices_Items
FOREIGN KEY (ItemID)
REFERENCES Items (ItemID)
,CONSTRAINT un_Prices_ItemId_ItemPrice_StartDate
UNIQUE (ItemID
,ItemPrice
,StartDate)
)


Sincerely,

Chris O.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top