Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 ORDER BY Rate Topic Display Mode Topic Options
Author
 Message
 Posted Tuesday, January 13, 2009 7:18 AM
 Say Hey Kid Group: General Forum Members Last Login: Wednesday, April 27, 2016 8:56 AM Points: 688, Visits: 1,203
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:

WO9901902
WO9901745
WO9901611
WO9901532
WO9901424

When in fact the last 5 orders appear in the middle?
WO014563
WO014377
WO014378
WO014440
WO014268

Will 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

Post Attachments
 OrderID.zip (7 views, 9.69 KB)
Post #635387
 Posted Tuesday, January 13, 2009 7:39 AM
 SSCrazy Group: General Forum Members Last Login: Tuesday, June 7, 2016 5:15 AM Points: 2,562, Visits: 2,643
 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
Post #635409
 Posted Tuesday, January 13, 2009 8:05 AM
 Say Hey Kid Group: General Forum Members Last Login: Wednesday, April 27, 2016 8:56 AM Points: 688, Visits: 1,203
 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
Post #635443
 Posted Tuesday, January 13, 2009 8:49 AM
 SSC Eights! Group: General Forum Members Last Login: Today @ 2:19 PM Points: 948, Visits: 7,148
 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`
Post #635509
 Posted Tuesday, January 13, 2009 10:15 AM
 SSChampion Group: General Forum Members Last Login: Today @ 3:08 PM Points: 10,808, Visits: 14,834
 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
Post #635617
 Posted Tuesday, January 13, 2009 2:08 PM
 Say Hey Kid Group: General Forum Members Last Login: Wednesday, April 27, 2016 8:56 AM Points: 688, Visits: 1,203
 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
Post #635831

 Permissions