472,353 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

limit query results to no duplicates on 1 field

Dom


I need to run a query based on a query in Access. The second query has
a number of conditions which all work well, but there is one more
contition I need to set to make it run properly.

the first query returns a number of data items, consisting, among other
fields, of a Company_ID and a Rating_Date. For most companies, the
latest value of Rating_Date is equal to a value in a seperate table
(tblVariables) which logs the last time a ratings update took place on
the database. To return one line per company for these companies, the
second query has a condition

...
WHERE (((qryCompanyInfo.Rating_Date)=[Variables]![LastRatingDate] ...

However, this does not show companies which are no longer being rated,
and therefore have no record where Rating_Date = LastRatingDate.
Companies which are no longer being rated have a Company_Type value of
9, so to show these companies, I added to the SQL as follows:

...
WHERE (((qryCompanyInfo.Date)=[Variables]![LastRatingDate] OR
(((qryCompanyInfo.Company_Type)=9));
...

The problem is that this now returns 1 line for all rated companies,
using the first condition, but then every record for the unrated
companies. I need to somehow filter the second condition, so that only
1 instance of each company appears in the final query.

I hope somebody can help on this, as I've been going around in circles
trying to figure it out. Thanks

dom

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #1
2 18588
Dom,

If I understand you correctly, you need to use an "Outer Join". You can
do this by opening your second query in design view and double-clicking
the black join line connecting qryCompanyInfo and tblVariables.

Choose the option: "Include ALL records from 'qryCompanyInfo' and only
those records from 'tblVariables' where the joined fields
are equal."

The resulting SQL will look something like:

SELECT qryCompanyInfo.Company_ID, qryCompanyInfo.Rating_Date,
tblVariables.LastRatingDate
FROM qryCompanyInfo LEFT JOIN tblVariables ON qryCompanyInfo.Rating_Date
= tblVariables.LastRatingDate;

HTH

Stuart

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #2
Dom <cr**************@bilge.net> wrote in message news:<3f*********************@news.frii.net>...
I need to run a query based on a query in Access. The second query has
a number of conditions which all work well, but there is one more
contition I need to set to make it run properly.

the first query returns a number of data items, consisting, among other
fields, of a Company_ID and a Rating_Date. For most companies, the
latest value of Rating_Date is equal to a value in a seperate table
(tblVariables) which logs the last time a ratings update took place on
the database. To return one line per company for these companies, the
second query has a condition

..
WHERE (((qryCompanyInfo.Rating_Date)=[Variables]![LastRatingDate] ...

However, this does not show companies which are no longer being rated,
and therefore have no record where Rating_Date = LastRatingDate.
Companies which are no longer being rated have a Company_Type value of
9, so to show these companies, I added to the SQL as follows:

..
WHERE (((qryCompanyInfo.Date)=[Variables]![LastRatingDate] OR
(((qryCompanyInfo.Company_Type)=9));
..

The problem is that this now returns 1 line for all rated companies,
using the first condition, but then every record for the unrated
companies. I need to somehow filter the second condition, so that only
1 instance of each company appears in the final query.

I hope somebody can help on this, as I've been going around in circles
trying to figure it out. Thanks

dom

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


hello,

Why do you make the rather complicated construct of joining with a
separate query containing records with the last ratingdate ?
Didn't you try to use aggregate functions like Max(ratingdate) to
filter out the latest ratings for each company ? You just have to make
a query consisting of some company-idtifier and the ratingdate to
achieve this (and eventually some other conditions ..) and make it an
aggregation by pressing the sum-button (the greek E-like symbol on the
menu) and to select max in stead of 'group by' for the ratingdate.
After that you can join this query with other data based on the
company-id and the ratingdate to get other relevant data by using the
query again as a table for another query.
May be you tried this procedure, but then you had to make a separate
small aggregation-query for companies not rated with some dummy or
empty value for the last rating-date if these are not already result
of the other aggregation-query for some reason and make a union, but
this method is not an elegant one.
Often this can be avoided by ensuring all companies are retained in
the first query when these are joined with other tables. In that case
you have to connect the other tables with an outer-join (selecting the
connection-lines and changing the jointype so the companies are all
kept into the query and also adding a 'or is null' clause for every
select-condition for fields in the tables connected by the outer
join-conditions. If the companies not rated are retained, the
resulting aggregation-query automatically produces null-values for
company's without any ratingdates.
May be I dont quite understand why it seems so difficult, but I hope
you get some suggestions to find a more elegant solution.

Marc
Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Guy Erez | last post by:
Hi, I'm running queries with MySql 4.0.17 that return thousands of records. Because I need to present them in GUI, I returieve the results in...
1
by: AP | last post by:
We commonly use this method to "pivot/crosstab" table results. For example lets say there are two offices, and we want the total sales for each...
6
by: Brian | last post by:
Hello, Basically, I'm running a query on a form's activation, and I'd like to have the results of the query be placed into other fields on the...
0
by: Rob | last post by:
I doubt this is the best way to do it, but what I came up with was to hide the XML in an HTML Comment then edit the file deleting the HTML stuff...
3
by: Sim Zacks | last post by:
I am using 8.0 beta 1 on an RH 8 Linux server. I have a union query that I am converting from access (where it worked) and it is returning...
9
by: Kelii | last post by:
I've been trying to get this piece to work for a few hours, but have given up. I hope someone out there can help, I think the issue is relatively...
2
by: milo1955 | last post by:
I have a form that passes a single variable via the GET function to a mysql databse query with the results paginated. The first page of the results...
1
by: DAHMB | last post by:
I have a column in a report that I want to return a sum of certain criteria from a query between two dates. I set the dates in a form that I launch...
16
by: dougmeece | last post by:
Good day everyone, I have a database with 2 main forms. The first form is used to add records to the database and contains a command button that...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.