ORDER BY

  • 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

  • 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


  • 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

  • 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

    &nbsp&nbsp&nbsp&nbspCASE

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWHEN SUBSTRING(OrderID, 3, 2) < '10'

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspTHEN '20' + RIGHT(OrderID, 7)

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspELSE '19' + RIGHT(OrderID, 7)

    &nbsp&nbsp&nbsp&nbspEND

  • I think Ken is on the right track, in that we need to know how the OrderId is contructed.

  • 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

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply