Solved

Query with too many variables

Posted on 2013-01-03
5
409 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 31

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now