Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

ORDER BY Expand / Collapse
Author
Message
Posted Tuesday, January 13, 2009 7:18 AM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Thursday, September 11, 2014 4:01 AM
Points: 625, Visits: 1,084
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 PUR
FROM (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 (5 views, 9.69 KB)
Post #635387
Posted Tuesday, January 13, 2009 7:39 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, October 21, 2013 12:32 AM
Points: 2,551, Visits: 2,593
Philip Horan (1/13/2009)

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

How 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


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Thursday, September 11, 2014 4:01 AM
Points: 625, Visits: 1,084
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!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 10:23 AM
Points: 848, Visits: 5,523
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

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:23 PM
Points: 10,214, Visits: 13,161
I think Ken is on the right track, in that we need to know how the OrderId is contructed.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check 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 help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #635617
Posted Tuesday, January 13, 2009 2:08 PM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Thursday, September 11, 2014 4:01 AM
Points: 625, Visits: 1,084
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
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse