User Profile

Collapse

Profile Sidebar

Collapse
FLANDERS
FLANDERS
Last Activity: Jan 19 '11, 09:53 AM
Joined: Jul 5 '08
Location:
  •  
  • Time
  • Show
  • Source
Clear All
new posts

  • FLANDERS
    replied to Convert correlated subqery to join?
    OK I think I have this sorted now. The below query has gone from 17 minutes to 22 seconds on my machine:
    Code:
    -- Fills in the blanks of the items table by joining each item change record
    -- to the previous valid record for the same item, to allow retrival of last "real" price change
    select * from (	
    	select 
    	a.id,
    	coalesce(a.priceChange, b.priceChange) as priceChange,
    ROW_NUMBER() OVER (PARTITION
    ...
    See more | Go to post

    Leave a comment:


  • FLANDERS
    replied to Convert correlated subqery to join?
    From tweaking your link, the following seems to work as required on the "updated" sample date:

    Code:
    select id, priceChange, 
    coalesce((
       select priceChange 
       from YourTable b 
       where a.rn > b.rn and a.id = b.id and b.priceChange is not null and a.priceChange is null order by b.rn desc limit 1 ), 
       a.priceChange
    ) tmp, 
    cMonth
    from YourTable a order by id, rn
    ...
    See more | Go to post

    Leave a comment:


  • FLANDERS
    replied to Convert correlated subqery to join?
    Hey,
    Thanks for the input. However, I think my sample data wasnt quite complete. Its not always valid to use
    y1.rn = y2.rn + 1, the previous "real" value may be a few rows back. This is why I used the max() function, rather than what you did. A complete sample data would be

    Code:
    [B]ID    priceChange    Month       rn[/B]
    1     100      March 2010         1
    1     null     April  2010        2
    ...
    See more | Go to post

    Leave a comment:


  • FLANDERS
    replied to Convert correlated subqery to join?
    Jeez dont be apologising, any help is great!
    See more | Go to post

    Leave a comment:


  • FLANDERS
    replied to Convert correlated subqery to join?
    That doesnt run. The ON clause of the INNER JOIN is incorrect it appears:
    Code:
    ON tI.ID = sIi.ID AND tI.RN > sIi.RN
    , since you are referring to an alias (tI) that isnt available at that point:
    The multi-part identifier "tI.ID" could not be bound
    See more | Go to post

    Leave a comment:


  • FLANDERS
    replied to Convert correlated subqery to join?
    OK, I used sample tables/columns in first example. There scenario is I have a table of item changes over time, basically ID, priceChange and a calculated row number. Some items may not change in a particular month and so I have a situation where I end up with a table like
    Code:
    [B]ID    priceChange    Month       rn[/B]
    1     100      March 2010  1
    1     null     June 2010   2
    1     200      July 2010   3
    2     520      March
    ...
    See more | Go to post

    Leave a comment:


  • FLANDERS
    started a topic Convert correlated subqery to join?

    Convert correlated subqery to join?

    I have a query that does a self join following by correlated subquery. I want to be able to select from "this row" or the latest row before "this row", based on the "rn" column, where column1 is populated. Its currently working using a correlated subquery as follows:

    Code:
    select 
    a.id,
    coalesce(a.typeName, b.typeName) as typeName
    FROM table1 a, table1 b
    where a.id = b.id AND b.rn
    ...
    See more | Go to post

  • FLANDERS
    started a topic Declare result set variable in procedural SQL
    in DB2

    Declare result set variable in procedural SQL

    Hi all,
    Is it possible to declare a SQL type of result set or similar? I want to do use the IN predicate like you can in a non-procedural SQL like this:
    Code:
    UPDATE TABLE1 SET COL1 = 123 WHERE COL2 IN (SELECT COL3 FROM TABLE2)
    I want to do this in procedural SQL, but I only want to use one cursor. So to do this I have declared a cursor for selecting COL2 from TABLE1 into a variable v_COL2 and am using a loop...
    See more | Go to post
No activity results to display
Show More
Working...