Re: Dynamic Comma delimited output

From: Steve Kass (skass_at_drew.edu)
Date: 12/14/04


Date: Tue, 14 Dec 2004 12:48:18 -0500

Adam,

  Ok, I think I've gone on a useless tangent. First of all, RANK
returns a value for each row, and is a different kind of aggregate than
one returning one row per grouping, so I shouldn't be trying to use that
example. We're thinking more about something like this:

SELECT CustomerID, CONCAT_AGG(OrderID) OVER (ORDER BY OrderID desc) as
OrderList
FROM Orders
GROUP BY CustomerID

The OVER syntax in SQL for

SELECT OrderID, RANK() OVER (ORDER BY CustomerID, OrderID desc) as R
FROM Orders

isn't relevant here, since there is no aggregating going on. Better
would be to look at something like

SELECT CustomerID, FIRST(OrderID) OVER (ORDER BY EmployeeID desc) as R
FROM Orders
GROUP BY CustomerID

if that were something that already exists.

The first OrderID for each CustomerID in EmployeeID desc order can be
found without sorting, so I think I'm still on track, but let me think
some more and get back to you...

SK

Adam Machanic wrote:

>"Steve Kass" <skass@drew.edu> wrote in message
>news:uqf%23xjf4EHA.4004@tk2msftngp13.phx.gbl...
>
>
>> I don't have my 2005 box running at the moment, but the sort in the
>>plan doesn't invalidate what I said. rank() has a set-theoretical
>>definition, and can be calculated by aggregating the CASE expression I
>>gave without a sort. The reason you see a sort is because the optimizer
>>chooses to generate the rank() values by sorting first, not because
>>sorting is required. For all I know, no alternative option for RANK()
>>
>>
>
>
>Okay, at this point I'm in over my head, as I don't really understand how
>the windowing that you're referring to actually works (specifically, "Just
>evaluate case when s in window and s <= x then 1 else 0 end for each row and
>keep a running sum.") So I'm going to go track down some resources on that
>(if you have any recommendations, please send them my way) and then I'll
>hopefully either agree with you or have something more to add to this
>discussion at a later date. :)
>
>I can't imagine how such an algorithm could possibly work efficiently
>without sorting the rows, as it could have no knowledge of the other rows'
>placement/numbers otherwise... e.g. if I ask for a rank based on a
>descending sort on one column, how can an algorithm identify a given row
>without knowing which rows preceed it? One way, and perhaps this is what
>you're referring to, is that it could scan the intermediate rowset to count
>the rows for each group, similar to what we do now when we use COUNT(*) to
>generate rankings. That would not require intermediate sorting, but is
>incredibly expensive and certainly much less efficient than an intermediate
>sort would be. So I'm assuming that the type of windowing operation you're
>referring to does something else that's both efficient and doesn't require
>sorting?
>
>
>
>



Relevant Pages

  • Re: Ranking numbers AND text
    ... No, not really sorting, I don't want to have to sort them manually, but rank ... > use a derived second column as the sort key. ...
    (microsoft.public.excel.worksheet.functions)
  • Re: Dynamic Comma delimited output
    ... > gave without a sort. ... > chooses to generate the rank() values by sorting first, ... > sorting is required. ... you're referring to, is that it could scan the intermediate rowset to count ...
    (microsoft.public.sqlserver.programming)
  • Re: Ranking Without Sorting
    ... >> contradicted in my first post where I gave an example of how to rank ... > It seems quite clear to me that your 'ranking' array contains the exact ... An unsorted array contains the same information as the same array after sorting. ... > the same operation as the insertion loop in insertion sort... ...
    (comp.programming)
  • Re: Ranking numbers AND text
    ... >No, not really sorting, I don't want to have to sort them manually, but rank ... but if the formula above could be used to sort the ... The specific formula above gives the ranking of cell A1 ...
    (microsoft.public.excel.worksheet.functions)
  • Re: Ranking Without Sorting
    ... you have just reinvented the rank sort. ... >> 'rank' array you want. ... >> Doing a 'heap sort', for example, where the heap holds the index into ...
    (comp.programming)