Hi Folks,
I have a query that joins 3 tables. One of the tables (SalaryData) has data
where there may be duplicate records with different dates. Of those
duplicate records, I want the query to show me only the one with the latest
date. The query below shows me all the records - including the duplicates
with older dates. I have tried various ways of using the MAX(DateField)
But can't get it to work. I need to keep the joins the way they are to
retain the proper recordset. This will be implimented on an ASP page.
Can anyone help me correct the query below - so I only get the newest
records from the table "SalaryData"- where the field "SalaryData.BenchID" is
the same? (the date field is called "SalaryData.Dat")
SELECT SalaryData.BenchID, SalaryData.OrgID, SalaryData.Min, SalaryData.Mid,
SalaryData.Max, SalaryData.Avr, SalaryData.NumPos, SalaryData.OM,
SalaryData.FY, OrgJobs.OrgID, OrgJobs.BenchID, Benchmarks.CatID,
Benchmarks.BenchID, Benchmarks.Benchmark, Max(SalaryData.Dat) AS Expr1
FROM (Benchmarks INNER JOIN OrgJobs ON Benchmarks.BenchID = OrgJobs.BenchID)
LEFT JOIN SalaryData ON OrgJobs.BenchID = SalaryData.BenchID
WHERE (((OrgJobs.OrgID)=1083 Or (OrgJobs.OrgID) Is Null) AND
((Benchmarks.CatID)=40))
GROUP BY SalaryData.BenchID, SalaryData.OrgID, SalaryData.Min,
SalaryData.Mid, SalaryData.Max, SalaryData.Avr, SalaryData.NumPos,
SalaryData.OM, SalaryData.FY, OrgJobs.OrgID, OrgJobs.BenchID,
Benchmarks.CatID, Benchmarks.BenchID, Benchmarks.Benchmark;
This is actually a sub-query which is nested in another query. Together
they give me the correct recordset - except that I get duplicate records
with older dates. I have tried to figure out a way to break up the query
into seperate queries, but then my ASP will need to loop exhaustively.
Thanks very much for any help.
Jack Coletti
St. Petersburg, FL 3 1883
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
How about this? Use a subquery to get the Max(Dat):
SELECT SD.BenchID,
SD.OrgID,
SD.Min,
SD.Mid,
SD.Max,
SD.Avr,
SD.NumPos,
SD.OM,
SD.FY,
OJ.OrgID,
OJ.BenchID,
BM.CatID,
BM.BenchID,
BM.Benchmark
FROM (Benchmarks As B
INNER JOIN OrgJobs AS OJ
ON BM.BenchID = OJ.BenchID)
LEFT JOIN SalaryData AS SD
ON OJ.BenchID = SD.BenchID
WHERE BM.CatID=40
AND (OJ.OrgID=1083 Or OJ.OrgID Is Null)
-- This subquery should get what you want
AND SD.Dat = (SELECT MAX(Dat) FROM SalaryData
WHERE BenchID = SD.BenchID)
GROUP BY SD.BenchID,
SD.OrgID,
SD.Min,
SD.Mid,
SD.Max,
SD.Avr,
SD.NumPos,
SD.OM,
SD.FY,
OJ.OrgID,
OJ.BenchID,
BM.CatID,
BM.BenchID,
BM.Benchmark;
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)
-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv
iQA/AwUBQlV8ooechKqOuFEgEQI98wCcCJJQLGui8K0HY2QRE9Rq5n Y21OAAoILQ
WJxPvxRWPZh6R7J606QB2etc
=Q0p3
-----END PGP SIGNATURE-----
MostlyH2O wrote: Hi Folks,
I have a query that joins 3 tables. One of the tables (SalaryData) has data where there may be duplicate records with different dates. Of those duplicate records, I want the query to show me only the one with the latest date. The query below shows me all the records - including the duplicates with older dates. I have tried various ways of using the MAX(DateField) But can't get it to work. I need to keep the joins the way they are to retain the proper recordset. This will be implimented on an ASP page.
Can anyone help me correct the query below - so I only get the newest records from the table "SalaryData"- where the field "SalaryData.BenchID" is the same? (the date field is called "SalaryData.Dat")
SELECT SalaryData.BenchID, SalaryData.OrgID, SalaryData.Min, SalaryData.Mid, SalaryData.Max, SalaryData.Avr, SalaryData.NumPos, SalaryData.OM, SalaryData.FY, OrgJobs.OrgID, OrgJobs.BenchID, Benchmarks.CatID, Benchmarks.BenchID, Benchmarks.Benchmark, Max(SalaryData.Dat) AS Expr1 FROM (Benchmarks INNER JOIN OrgJobs ON Benchmarks.BenchID = OrgJobs.BenchID) LEFT JOIN SalaryData ON OrgJobs.BenchID = SalaryData.BenchID WHERE (((OrgJobs.OrgID)=1083 Or (OrgJobs.OrgID) Is Null) AND ((Benchmarks.CatID)=40)) GROUP BY SalaryData.BenchID, SalaryData.OrgID, SalaryData.Min, SalaryData.Mid, SalaryData.Max, SalaryData.Avr, SalaryData.NumPos, SalaryData.OM, SalaryData.FY, OrgJobs.OrgID, OrgJobs.BenchID, Benchmarks.CatID, Benchmarks.BenchID, Benchmarks.Benchmark;
This is actually a sub-query which is nested in another query. Together they give me the correct recordset - except that I get duplicate records with older dates. I have tried to figure out a way to break up the query into seperate queries, but then my ASP will need to loop exhaustively.
I believe that's what I needed. Thanks VERY much for you answers to both my
posts.
clear skies,
Jack
"MGFoster" <me@privacy.com> wrote in message
news:A0****************@newsread1.news.pas.earthli nk.net... -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
How about this? Use a subquery to get the Max(Dat):
SELECT SD.BenchID, SD.OrgID, SD.Min, SD.Mid, SD.Max, SD.Avr, SD.NumPos, SD.OM, SD.FY, OJ.OrgID, OJ.BenchID, BM.CatID, BM.BenchID, BM.Benchmark
FROM (Benchmarks As B INNER JOIN OrgJobs AS OJ ON BM.BenchID = OJ.BenchID) LEFT JOIN SalaryData AS SD ON OJ.BenchID = SD.BenchID
WHERE BM.CatID=40 AND (OJ.OrgID=1083 Or OJ.OrgID Is Null)
-- This subquery should get what you want
AND SD.Dat = (SELECT MAX(Dat) FROM SalaryData WHERE BenchID = SD.BenchID)
GROUP BY SD.BenchID, SD.OrgID, SD.Min, SD.Mid, SD.Max, SD.Avr, SD.NumPos, SD.OM, SD.FY, OJ.OrgID, OJ.BenchID, BM.CatID, BM.BenchID, BM.Benchmark; -- MGFoster:::mgf00 <at> earthlink <decimal-point> net Oakland, CA (USA)
-----BEGIN PGP SIGNATURE----- Version: PGP for Personal Privacy 5.0 Charset: noconv
iQA/AwUBQlV8ooechKqOuFEgEQI98wCcCJJQLGui8K0HY2QRE9Rq5n Y21OAAoILQ WJxPvxRWPZh6R7J606QB2etc =Q0p3 -----END PGP SIGNATURE-----
MostlyH2O wrote: Hi Folks,
I have a query that joins 3 tables. One of the tables (SalaryData) has
data where there may be duplicate records with different dates. Of those duplicate records, I want the query to show me only the one with the
latest date. The query below shows me all the records - including the
duplicates with older dates. I have tried various ways of using the MAX(DateField) But can't get it to work. I need to keep the joins the way they are to retain the proper recordset. This will be implimented on an ASP page.
Can anyone help me correct the query below - so I only get the newest records from the table "SalaryData"- where the field
"SalaryData.BenchID" is the same? (the date field is called "SalaryData.Dat")
SELECT SalaryData.BenchID, SalaryData.OrgID, SalaryData.Min,
SalaryData.Mid, SalaryData.Max, SalaryData.Avr, SalaryData.NumPos, SalaryData.OM, SalaryData.FY, OrgJobs.OrgID, OrgJobs.BenchID, Benchmarks.CatID, Benchmarks.BenchID, Benchmarks.Benchmark, Max(SalaryData.Dat) AS Expr1 FROM (Benchmarks INNER JOIN OrgJobs ON Benchmarks.BenchID =
OrgJobs.BenchID) LEFT JOIN SalaryData ON OrgJobs.BenchID = SalaryData.BenchID WHERE (((OrgJobs.OrgID)=1083 Or (OrgJobs.OrgID) Is Null) AND ((Benchmarks.CatID)=40)) GROUP BY SalaryData.BenchID, SalaryData.OrgID, SalaryData.Min, SalaryData.Mid, SalaryData.Max, SalaryData.Avr, SalaryData.NumPos, SalaryData.OM, SalaryData.FY, OrgJobs.OrgID, OrgJobs.BenchID, Benchmarks.CatID, Benchmarks.BenchID, Benchmarks.Benchmark;
This is actually a sub-query which is nested in another query. Together they give me the correct recordset - except that I get duplicate records with older dates. I have tried to figure out a way to break up the
query into seperate queries, but then my ASP will need to loop exhaustively.
From your handle "MostlyH2O", I recall a story / joke:
2 university professors (one old and one young) were in earnest discussion
about their scientific endeavours. A young female student (and of course,
blond / tall / slim / beautiful / extraordinarily curvaceous and
indescribably sexy-looking - rated 11 out of 10, basically) walked by and
the young professor couldn't take his eyes of her and briefly lost track of
the discussion. The old professor got very annoyed and said:
"Scientifically, what you are looking at is very ordinary, mostly water
(more than 90%, anyway)."
The young professor (who is specialised in Computational Physics and skilled
in time-sharing his brain) retorted within microseconds:
"Yeah. However, I am observing / invetigating surface tension and the
effects of movements on surface tension."
--
Van T. Dinh
MVP (Access)
"MostlyH2O" <jm*@takeitouthrmp.net> wrote in message
news:qU******************@tornado.tampabay.rr.com. .. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: sah |
last post by:
I need some help with the following query:
DECLARE @SRV VARCHAR(20), @date smalldatetime
SET @SRV = (select @@servername)
SET @date = '20040901'
select Srv_Name = @SRV, DB_Name = 'DB_NAME',...
|
by: David Goodyear |
last post by:
At the moment im experimenting with ideas in C++ and would really like to
solve the following, please please help.
Sorry i dont even know what the subject is this would come under? :(
Sorry if...
|
by: Lyn |
last post by:
I am trying to get my head around the concept of default, special or empty
values that appear in Access VBA, depending on data type. The Access Help
is not much (help), and the manual that I have...
|
by: Jason |
last post by:
Hi,
I was wondering if any could point me to an example or give me ideas on how
to dynamically create a form based on a database table? So, I would have a
table designed to tell my application...
|
by: georges the man |
last post by:
The purpose:
• Sorting and Searching
• Numerical Analysis
Design Specification
You are to write a program called “StockAnalyser”. Your program will read a text file that
contains historical...
| |
by: Takeadoe |
last post by:
Dear NG,
Can someone assist me with writing the little code that is needed to
run an update table query each time the database is opened? From what
I've been able to glean from this group, the...
|
by: settyv |
last post by:
Hi,
Below is the Javascript function that am trying to call from asp:Button
control.
<script language="javascript">
function ValidateDate(fromDate,toDate)
{
var fromDate=new Date();
|
by: jenipriya |
last post by:
Hi all... its very urgent.. please........i m a beginner in oracle.... Anyone please help me wit dese codes i hv tried... and correct the errors...
The table structures i hav
Employee (EmpID,...
|
by: osward |
last post by:
Hi, everyone,
I had managed to make use of the date link from a simple calendar script to my query table. When I click on the date's link or Prev and Next Month link, The table first row will be...
|
by: gubbachchi |
last post by:
Hi all,
In my application I need to display the data fetched from mysql database after the user selects date from javascript calender. I have written the code in which after the user selects the...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |