## ORDER BY

 Author Message 2Tall Ten Centuries Group: General Forum Members Points: 1164 Visits: 1203 Hi. I posted previously and NULL kindly provided a solution. Part of the code was as follows:`SELECT ProductId ,COUNT(1) OrderCount ,CASE WHEN COUNT(1) = 1 THEN SUM(MAN)/MANCount ELSE AVG(MAN) END MAN ,CASE WHEN COUNT(1) = 1 THEN SUM(PUR)/PURCount ELSE AVG(PUR) END PURFROM (SELECT DENSE_RANK() over (partition by ProductId order by OrderId desc) as OrderRank`Part of the solution was to return the average cost for the last 5 orders of a Product. However sort desc the last 5 orders listed are:WO9901902WO9901745WO9901611WO9901532WO9901424When in fact the last 5 orders appear in the middle?WO014563WO014377WO014378WO014440WO014268Will it be possible to return the last 5 orders with the absense of a date field? Sample data attached for a single product with multiple orders.Thanks,Phil. -------------------------------------------------------------------------------------A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge." Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... ' Tommy Cooper Attachments OrderID.zip (12 views, 9.00 KB) Ramesh Saive Hall of Fame Group: General Forum Members Points: 3468 Visits: 2643 Philip Horan (1/13/2009)When in fact the last 5 orders appear in the middle?WO014563WO014377WO014378WO014440WO014268How could you tell that these are the last 5 orders?Will it be possible to return the last 5 orders with the absense of a date field? Off course yes, but you have tell SQL that this is the column that identifies the order of the records. If you meant that the order it was entered into the database then you have to know that rows in a table does not have a specific order. --Ramesh 2Tall Ten Centuries Group: General Forum Members Points: 1164 Visits: 1203 Hi Ramesh. Having reveiwed the orders from within the existing system it came to light that what I thought was the last OrderId was not in fact the case, they just appeared to be........I can see of no way to achieve what I have asked but I asked anyhows (lack of date field does not help!).Thanks for taking a look.Phil. -------------------------------------------------------------------------------------A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge." Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... ' Tommy Cooper Ken McKelvey SSCommitted Group: General Forum Members Points: 1671 Visits: 7894 In the data you have provided the OrderId looks as though it may contain a 2 digit year.If this is the case then something like the following may work:`ORDER BY    CASE        WHEN SUBSTRING(OrderID, 3, 2) < '10'        THEN '20' + RIGHT(OrderID, 7)        ELSE '19' + RIGHT(OrderID, 7)    END` Jack Corbett SSCoach Group: General Forum Members Points: 18474 Visits: 14895 I think Ken is on the right track, in that we need to know how the OrderId is contructed. Jack CorbettApplications Developer Don't let the good be the enemy of the best. -- Paul FlemingAt best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at workCheck out these links on how to get faster and more accurate answers: Forum Etiquette: How to post data/code on a forum to get the best helpNeed an Answer? Actually, No ... You Need a QuestionHow to Post Performance ProblemsCrosstabs and Pivots or How to turn rows into columns Part 1Crosstabs and Pivots or How to turn rows into columns Part 2 2Tall Ten Centuries Group: General Forum Members Points: 1164 Visits: 1203 Thanks guys. I will need to check with the app vender. It has come to light that I may be able to get what I am after by extracting from a second dataset (logs stock movements). The dataset in question lists ProductId multiple times with a date the stock moved (some products moved multiple times on the same day). How might I construct a query that returns the last 5 dates per product?The dataset in question has 200000 records (lots of repatition).I would name the table StockMovements and it would ahve 2 fields 'ProductId', 'DateMoved'.Many Thanks,Phil. -------------------------------------------------------------------------------------A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge." Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... ' Tommy Cooper