Solved

Access 97: How to write this query efficiently?

Posted on 2014-01-21
15
277 Views
Last Modified: 2014-01-21
In Access 97:

I would like a report to show the first time a product has shipped into a new Country.

I have a table called 'InvoiceOrderEntry' with about 50,000 records.

The table has key fields like:

InvoiceID
ShipDate
ProductModelNumber
Country

Of course, there are many different Invoices, Products and Countries.

For example, I want to see the first time a Red Pen has been shipped to Iceland.
Then I want to see the first time a Blue Pen has been shipped to Iceland.
Then I want to see the first time a Black Pen has been shipped to Canada.

As the years go by, more new Products will get shipped to new Countries.

I can code it a brute-force way whereby I run a query on all products shipped and save that to a temp Table. Then I run a second query to see if any new Countries have been shipped to and append the temp Table. And, so on and so forth.

That does not seem to be very efficient.

Is there an elegant way of doing this? I like the idea of saving the results to a temp Table so I can then run reports on that table.

I hope I have made myself clear here.

High points for a speedy solution!

Thanks!!
0
Comment
Question by:SOTA
  • 8
  • 7
15 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 39797929
try this query


select A.*
from InvoiceOrderEntry As A
inner join
(select min(B.ShipDate) as MinShipDate, B.ProductModelNumber, B.Country
  from InvoiceOrderEntry As B
  group by B.ProductModelNumber, B.Country) As C
ON A.ShipDate=C.MinShipDate and  A.ProductModelNumber=C.ProductModelNumber and A.Country=C.Country
0
 

Author Comment

by:SOTA
ID: 39798044
Thanks capricorn1!!

I got a small issue though. I copied the code into SQL Manager as a new View and it worked perfectly!! AWESOME!!!!!

However, when I paste it into Access97 QBE grid in SQL view, I get an error "Syntax error in FROM clause".

Can you help?
Thanks much!!
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39798124
can you copy the SQL in Access 97  and post here.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:SOTA
ID: 39798137
Select
  C.MinShipDate,
  C.ProductModelNumber,
  C.ShipCountry
From
  InvoiceOrderEntry As A Inner Join
  (Select
    Min(B.ShipDate) As MinShipDate,
    B.ProductModelNumber,
    B.ShipCountry
  From
    InvoiceOrderEntry As B
  Group By
    B.ProductModelNumber, B.ShipCountry) As C On A.ShipDate = C.MinShipDate And
    A.ProductModelNumber = C.ProductModelNumber And A.ShipCountry =
    C.ShipCountry
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39798152
change this part

Select
  C.MinShipDate,
  C.ProductModelNumber,
  C.ShipCountry


with

Select
  A.MinShipDate,
  A.ProductModelNumber,
  A.ShipCountry


the whole query, copy and paste

Select
  A.MinShipDate,
  A.ProductModelNumber,
  A.ShipCountry
From
  InvoiceOrderEntry As A Inner Join
  (Select
    Min(B.ShipDate) As MinShipDate,
    B.ProductModelNumber,
    B.ShipCountry
  From
    InvoiceOrderEntry As B
  Group By
    B.ProductModelNumber, B.ShipCountry) As C On A.ShipDate = C.MinShipDate And
    A.ProductModelNumber = C.ProductModelNumber And A.ShipCountry =
    C.ShipCountry
0
 

Author Comment

by:SOTA
ID: 39798206
Still gives the same error.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39798241
can you run just this query

Select
  A.MinShipDate,
  A.ProductModelNumber,
  A.ShipCountry
From
  InvoiceOrderEntry As A


see if you will get results or error
0
 

Author Comment

by:SOTA
ID: 39798254
Nope as MinShipDate is not a valid column name.

This works:

Select
  A.ShipDate,
  A.ProductModelNumber,
  A.ShipCountry
From
  InvoiceOrderEntry As A
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39798276
how about this one

Select
    Min(B.ShipDate) As MinShipDate,
    B.ProductModelNumber,
    B.ShipCountry
  From
    InvoiceOrderEntry As B
0
 

Author Comment

by:SOTA
ID: 39798289
Nope, but this works:


Select
    Min(B.ShipDate) As MinShipDate,
    B.ProductModelNumber,
    B.Country
  From
    InvoiceOrderEntry As B
GROUP BY B.ProductModelNumber, B.Country;
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39798320
ok... this should work


Select
  A.MinShipDate,
  A.ProductModelNumber,
  A.ShipCountry
From
  InvoiceOrderEntry As A
INNER JOIN
(
Select
    Min(B.ShipDate) As MinShipDate,
    B.ProductModelNumber,
    B.Country
  From
    InvoiceOrderEntry As B
GROUP BY B.ProductModelNumber, B.Country
) AS C
On A.ShipDate = C.MinShipDate And
    A.ProductModelNumber = C.ProductModelNumber And A.ShipCountry =
    C.ShipCountry
0
 

Author Comment

by:SOTA
ID: 39798354
nope. sorry!

"Syntax error in FROM clause".
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39798388
this could be an Access97 problem.. try to use two queries

create this query and save as  "Q_Min"

Select
    Min(ShipDate) As MinShipDate,
    ProductModelNumber,
    Country
  From
    InvoiceOrderEntry
GROUP BY ProductModelNumber, Country


now create another query using the QBE grid,
add the table InvoiceOrderEntry and the Query  "Q_Min"

create a join on fields  Shipdate and MinShipDate
ProductModelNumber And ProductModelNumber
ShipCountry and ShipCountry

run the query
0
 

Author Comment

by:SOTA
ID: 39798521
Thanks!!! Yes, that did the trick. Now, having said that...Access97 takes a long time to run this query. So, I will instead make a VIEW in SQL Manager and run the query from the server-side for speed. I will then link the result via ODBC to Access97.

Thanks SO much for this!!!! Amazing!!!
:-)
0
 

Author Closing Comment

by:SOTA
ID: 39798524
Absolutely perfect!!!! Thanks!!!
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

685 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