Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Access 97: How to write this query efficiently?

Posted on 2014-01-21
15
Medium Priority
?
280 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

705 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