Click here to Skip to main content
15,921,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a query that tries to give a serial number the changes of price in a table of articles. This is in order to be able to filter later for example the last 3 prices that a certain article has had and the dates in which they were changed:

SQL
SELECT tblPriceHistory.Article, tblPriceHistory.Date, tblPriceHistory.Price, (SELECT Count(*) FROM tblPriceHistory AS Temp WHERE (Temp.Article=tblPriceHistory.Article) AND (Temp.Date <= tblPriceHistory.Date)) AS ItemNo
FROM tblPriceHistory;


But as soon as I introduce a WHERE clause in the sub-query Access crashes... any hints?

TheTable has 30.121 registers could it be too much for this type of recursive sub-queries?

Thanks for any help,

JBB
Posted
Updated 16-Mar-11 4:49am
v2
Comments
Wendelius 16-Mar-11 12:46pm    
Few additional questions:
- adding the WHERE (Temp.Article=tblPriceHistory.Article) AND (Temp.Date <= tblPriceHistory.Date) causes the query to crash or some other WHERE consition?
- do you get any error message?
jbbmovil 17-Mar-11 8:37am    
Adding ANY where clause makes Access crash.
The error message is the clasical: MS Access has detected a problem and must close... please send a problem report to MS.

I have finally soved it with a variant of the code that can be found in :
http://easyweb.easynet.co.uk/~trevor/AccFAQ/queries.htm

Thanks,

JBB

1 solution

Ok, still don't know why the access crashes since your scalar query is just fine from syntax point of view. Could it be that it doesn't like the Temp-alias, doesn't understand the correlation etc.

However, having a closer look at your query, could you use just a simple group by-query. SOmething like:
SQL
SELECT a.Article, 
       a.Date, 
       a.Price, 
       count(*)
FROM tblPriceHistory a left join tblPriceHistory b
     on (a.Article=b.Article and b.Date <= a.Date)
group by a.Article,
       a.Date,
       a.Price
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900