473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Query help please

Hi,
Please can you help me with this query which I am struggling with?
Here is a simplified version of the table I am trying to work with

VehicleId, PurchaseId, PurchaseDate, Comment

1, 1, 03/03/2006, 'customer has a big nose'
1, 79, 04/04/2006, 'it's raining'
1, 8, 05/05/2006, 'man, i keep selling this vehicle'
2, 412, 02/02/2006, 'I break for lunch in 10 minutes'
2, 5, 03/03/2006, 'I wonder what's on TV tonight'
3, 2, 05/05/2006, 'I am the angel of death, destroyer of worlds'

I need to select only the rows for the first time the vehicle is sold
(specifically I need the comment). I need to return

1, 1, 03/03/2006, 'customer has a big nose'
2, 412, 02/02/2006, 'I break for lunch in 10 minutes'
3, 2, 05/05/2006, 'I am the angel of death, destroyer of worlds'

Purchase Id cannot be guaranteed to be in ascending date order.

Can anyone help please?
Thanks
Rob

Dec 13 '06 #1
3 1379
I used two queries:

Query1:
SELECT tbl_purchases.VehicleID, Min(tbl_purchases.PurchaseDate) AS
MinOfPurchaseDate
FROM tbl_purchases
GROUP BY tbl_purchases.VehicleID;

Query2:
SELECT Query1.VehicleID, tbl_purchases.PurchaseID,
Query1.MinOfPurchaseDate, tbl_purchases.Comment
FROM Query1 INNER JOIN tbl_purchases ON (Query1.VehicleID =
tbl_purchases.VehicleID) AND (Query1.MinOfPurchaseDate =
tbl_purchases.PurchaseDate)
GROUP BY Query1.VehicleID, tbl_purchases.PurchaseID,
Query1.MinOfPurchaseDate, tbl_purchases.Comment;

Cheers,
Jason Lepack
ro***********@hotmail.com wrote:
Hi,
Please can you help me with this query which I am struggling with?
Here is a simplified version of the table I am trying to work with

VehicleId, PurchaseId, PurchaseDate, Comment

1, 1, 03/03/2006, 'customer has a big nose'
1, 79, 04/04/2006, 'it's raining'
1, 8, 05/05/2006, 'man, i keep selling this vehicle'
2, 412, 02/02/2006, 'I break for lunch in 10 minutes'
2, 5, 03/03/2006, 'I wonder what's on TV tonight'
3, 2, 05/05/2006, 'I am the angel of death, destroyer of worlds'

I need to select only the rows for the first time the vehicle is sold
(specifically I need the comment). I need to return

1, 1, 03/03/2006, 'customer has a big nose'
2, 412, 02/02/2006, 'I break for lunch in 10 minutes'
3, 2, 05/05/2006, 'I am the angel of death, destroyer of worlds'

Purchase Id cannot be guaranteed to be in ascending date order.

Can anyone help please?
Thanks
Rob
Dec 13 '06 #2
I now understand access subqueries. They're different from Oracle.

This one query does what you want.
SELECT T1.VehicleID, tbl_purchases.PurchaseID, T1.MinDate,
tbl_purchases.Comment
FROM [SELECT tbl_purchases.VehicleID, Min(tbl_purchases.PurchaseDate)
AS MinDate
FROM tbl_purchases
GROUP BY tbl_purchases.VehicleID]. AS T1 INNER JOIN tbl_purchases ON
(T1.MinDate = tbl_purchases.PurchaseDate) AND (T1.VehicleID =
tbl_purchases.VehicleID);

Cheers,
Jason Lepack

Jason Lepack wrote:
I used two queries:

Query1:
SELECT tbl_purchases.VehicleID, Min(tbl_purchases.PurchaseDate) AS
MinOfPurchaseDate
FROM tbl_purchases
GROUP BY tbl_purchases.VehicleID;

Query2:
SELECT Query1.VehicleID, tbl_purchases.PurchaseID,
Query1.MinOfPurchaseDate, tbl_purchases.Comment
FROM Query1 INNER JOIN tbl_purchases ON (Query1.VehicleID =
tbl_purchases.VehicleID) AND (Query1.MinOfPurchaseDate =
tbl_purchases.PurchaseDate)
GROUP BY Query1.VehicleID, tbl_purchases.PurchaseID,
Query1.MinOfPurchaseDate, tbl_purchases.Comment;

Cheers,
Jason Lepack
ro***********@hotmail.com wrote:
Hi,
Please can you help me with this query which I am struggling with?
Here is a simplified version of the table I am trying to work with

VehicleId, PurchaseId, PurchaseDate, Comment

1, 1, 03/03/2006, 'customer has a big nose'
1, 79, 04/04/2006, 'it's raining'
1, 8, 05/05/2006, 'man, i keep selling this vehicle'
2, 412, 02/02/2006, 'I break for lunch in 10 minutes'
2, 5, 03/03/2006, 'I wonder what's on TV tonight'
3, 2, 05/05/2006, 'I am the angel of death, destroyer of worlds'

I need to select only the rows for the first time the vehicle is sold
(specifically I need the comment). I need to return

1, 1, 03/03/2006, 'customer has a big nose'
2, 412, 02/02/2006, 'I break for lunch in 10 minutes'
3, 2, 05/05/2006, 'I am the angel of death, destroyer of worlds'

Purchase Id cannot be guaranteed to be in ascending date order.

Can anyone help please?
Thanks
Rob
Dec 13 '06 #3
A few different approaches.

SELECT *
FROM Purchases as A
WHERE PurchaseDate =
(select min(PurchaseDate) from Purchases as B
where A.VehicleID = B.VehicleID)

SELECT *
FROM Purchases as A
WHERE NOT EXISTS
(select * from Purchases as B
where A.VehicleID = B.VehicleID
and A.PurchaseDate B.PurchaseDate)

SELECT *
FROM Purchases as A
WHERE PurchaseID =
(select TOP 1 PurchaseID from Purchases as B
where A.VehicleID = B.VehicleID
order by PurchaseDate)

Roy Harvey
Beacon Falls, CT

On 13 Dec 2006 05:09:34 -0800, ro***********@hotmail.com wrote:
>Hi,
Please can you help me with this query which I am struggling with?
Here is a simplified version of the table I am trying to work with

VehicleId, PurchaseId, PurchaseDate, Comment

1, 1, 03/03/2006, 'customer has a big nose'
1, 79, 04/04/2006, 'it's raining'
1, 8, 05/05/2006, 'man, i keep selling this vehicle'
2, 412, 02/02/2006, 'I break for lunch in 10 minutes'
2, 5, 03/03/2006, 'I wonder what's on TV tonight'
3, 2, 05/05/2006, 'I am the angel of death, destroyer of worlds'

I need to select only the rows for the first time the vehicle is sold
(specifically I need the comment). I need to return

1, 1, 03/03/2006, 'customer has a big nose'
2, 412, 02/02/2006, 'I break for lunch in 10 minutes'
3, 2, 05/05/2006, 'I am the angel of death, destroyer of worlds'

Purchase Id cannot be guaranteed to be in ascending date order.

Can anyone help please?
Thanks
Rob
Dec 13 '06 #4

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

Similar topics

29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
4
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',...
4
by: Max Harvey | last post by:
Hi, I have looked at the example called "Open Parameter queries from code" from the site http://www.mvps.org/access/queries/qry0003.htm I made up a test which I though looked pretty close...
7
by: Nicolae Fieraru | last post by:
I have two tables, they contain: Table1: ID1, Name1, Address1, Purchase1 Table2: ID2, Name2, Address2, Purchase2 I need a query which creates Table3 with content from Table1 and Table2. The...
6
by: Nicolae Fieraru | last post by:
Hi All, I have a query, Select Count(BoolField) from tblMyTable, Where BoolField = true. If I run the query by itself, it returns the number of true records I want to use the result of that...
5
by: Ryan Hubbard | last post by:
Is it possible to get the recordset from an open query window? So you run the query. The window is open. Can vba retrieve this data?
4
by: Apple | last post by:
1. I want to create an autonumber, my requirement is : 2005/0001 (Year/autonumber), which year & autonumber no. both can auto run. 2. I had create a query by making relation to a table & query,...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
9
by: mharrison | last post by:
Hello, I am developing a small java web-based car-pool booking system app which interacts with an access database. I am trying to write 2 queries: The first which will specify whether a given car...
4
by: Doris | last post by:
It does not look like my message is posting....if this is a 2nd or 3rd message, please forgive me as I really don't know how this site works. I want to apologize ahead of time for being a novice...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
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 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.