|
I am trying to select in a join/subquery the last time an item had an increased price. this is somewhat of a challenge for me - can anyone lend a hand?
(comments are locked)
|
|
Ok, here are my takes, pretty similar. First one is valid if And the second, which does away with that assumption by projecting a sequence over them: Hmm. I forgot to document that assumption!
Mar 26 '11 at 07:27 AM
ThomasRushton ♦
(comments are locked)
|
|
Your title and your actual question seem to be a little at odds. If you want the row with the highest price for that item, then this should work: If you want the last time the price was increased then you may want something like: Of course, in the sample data you provided, these will each return exactly the same results.
(comments are locked)
|
|
This seems to me to be the sort of thing where a nice handy ROW_NUMBER partitioned function would come in handy: Replace your SELECT statement with the following: will return the following: If, on the other hand, you want the most recent set of prices, then you would replace the with oh, and if you want the most recent maximum price (in case something has had two goes at the highest price), then you would do: OK, so as you want the most recent price rise, then things are a little more complicated. NB: Any errors are entirely due to lack of sleep; however, I believe this does show the way forward for your problem... Also, it's worth noting that given your sample data, this returns the same set of records as the query that returns the highest prices...
(comments are locked)
|
|
This will give you the record for the highest price, but that doesn't mean that it's the last time the price was raised. The second example will give you the last time the price was updated, but I am not sure it's the most efficient, but at least it's not using any cursor. Hakan - you are correct - i do need the last time the price was raised, not necessarily the max price. the other challenge i am facing in this example - i can have multiple date entries with the same price. so item prc date 1234 10 1/1/10 1234 11 1/2/10 1234 11 1/9/10 1234 11 1/31/10 this query is nested and the standard for this company seems to be indexed temp tables vs cte's
Mar 25 '11 at 01:37 PM
siera_gld
(comments are locked)
|
|
Hakan - you are correct - i do need the last time the price was raised, not necessarily the max price. the other challenge i am facing in this example - i can have multiple date entries with the same price. so this query is nested and the standard for this company seems to be indexed temp tables vs cte's My second query should be able to handle dates with the same price, because it only selects records where the next record in the date sequence have a higher price. When the price is the same, the record will be excluded. My code is neat, but I am not sure about the performance impact on large datasets. If you try it, let me know how it performs.
Mar 25 '11 at 11:39 PM
Håkan Winther
(comments are locked)
|
1 2 next page »

