Solved

Query with too many variables

Posted on 2013-01-03
5
433 Views
Last Modified: 2013-01-14
I am trying to write an update query but I can't get the initial select query right.  I am hoping I am making this more difficult than it needs to be but I am stuck.

I have two tables - Temp (shortened for here) and History.  They have all the same fields (I perform calculations in temp before appending it to history) and the link is ticker.  I need to write a query to return values where ticker in the two tables is equal,  quarter from history references the field on a form, and category from one table does not equal category from the other.

In other words, I want the values where
History.Ticker
History.Quarter (Form!Front!PreviousQuarter)
History.Category
is compared to
Temp.Ticker
Temp.Category
and values where the category is different will be returned.
0
Comment
Question by:jmerulla
5 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 38742033
Try this:

SELECT * 
FROM History, Temp
WHERE History.Ticker = Temp.Ticker AND History.Quarter = Forms!Front!PreviousQuarter AND History.Category <> Temp.Category

Open in new window

0
 
LVL 30

Expert Comment

by:hnasr
ID: 38742322
One way is to select records using INNER JOIN, then apply the where clause.

UPDATE HISTORY INNER JOIN Temp ON History.Ticker = Temp.Ticker
SET HISTORY.fld1 = Temp.fld1, HISTORY.fld2 = Temp.fld2
WHERE History.Quarter = Form!Front!PreviousQuarter AND History.Category <> Temp.Category

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 38744065
Can you provide some sample data for the two tables and what you expect the result of the update to look like?
0
 
LVL 2

Author Comment

by:jmerulla
ID: 38747570
Here is what I have and the query doesn't return anything -

SELECT [Temporary Add to History per Quarter2].*, *
FROM [Temporary Add to History per Quarter2], [Fund History]
WHERE ((([Fund History].Quarter)=[Forms]![Front]![Previous Qtr]) AND (([Fund
History].[Full Name Category])<>[Temporary Add to History per Quarter2]![Full Name
Category]) AND (([Temporary Add to History per Quarter2].Ticker)=[Temporary Add to
History per Quarter2]![Ticker]));

The temp file is there and the field on the form is correct.

In the end, I want it to return a table like this -

Ticker      Quarter      Recommendation      Full Name Category
AOGIX            2012-9-30      1                  Aggressive Allocation
So I can make an update query to change recommendation from 1 to 2.
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 38747620
A couple of corrections and simplifying the syntax a bit:

SELECT temp.*, hist.*
FROM [Temporary Add to History per Quarter2] temp, [Fund History] hist
WHERE (hist.[Full Name Category] <> temp.[Full Name 
Category]) AND (temp.Ticker = hist.[Ticker]) AND (hist.Quarter = [Forms]![Front]![Previous Qtr]) 

Open in new window


If that still returns no records, then as requested above please provide some sample data meaning:

- Sample data representative of what you see in both tables
- Expected results based on the data given

(Mask any sensitive data as needed)

Or better yet, upload a sample database  (.mdb or accdb file) with the query, tables and data involved (again, masking any sensitive data).
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Outlook Free & Paid Tools
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
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 …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

778 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