Solved

Access 97: How to write this query efficiently?

Posted on 2014-01-21
15
275 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

831 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