Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2015
    Posts
    31

    Unanswered: select last field where is not null

    Hello,
    I have to select for a person , all the non null field order by the last date.

    example Date Phone Name Address
    person-1 day-10 333 null street-1
    person-1 day-07 444 Jon null
    person-1 day-03 444 null null

    My output should be

    Phone Name Address
    person-1 444 Jon street-1


    Do you know if there is a way to do this ?

    Thnak you -

  2. #2
    Join Date
    Mar 2015
    Posts
    31
    maybe one way to do this is using a stored procedure ( or something recursive )
    I tried to used the OLAP function but I don't see how to do this.




    while read record from table where person= person_1 order by date desc

    $field_1_back = if field_1 is not null then field_1 else $field_1_back
    $field_2_back = if field_2 is not null then field_2 else $field_2_back

    done

    output ( $field_1_back , $field_2_back )

  3. #3
    Join Date
    Jul 2013
    Location
    Moscow, Russia
    Posts
    666
    Provided Answers: 55
    Hi,

    try this:
    Code:
    with t (example, Date, Phone, Name, Address) as (values
      ('person-1', current date-10 days, '333', cast(null as varchar(10)), 'street-1')
    , ('person-1', current date-07 days, '444', 'Jon', null)
    , ('person-1', current date-03 days, '444', null, null)
    )
    select distinct
      example
    , first_value(phone,   'IGNORE NULLS') over (partition by example order by date desc RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as phone
    , first_value(name,    'IGNORE NULLS') over (partition by example order by date desc RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as name
    , first_value(address, 'IGNORE NULLS') over (partition by example order by date desc RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as address
    from t
    
    EXAMPLE  PHONE NAME       ADDRESS 
    -------- ----- ---------- --------
    person-1 444   Jon        street-1
    Regards,
    Mark.

  4. #4
    Join Date
    Mar 2015
    Posts
    31
    thank you..

    just one more question, I have an error with this option , the ignore null function

    select
    (phone, 'IGNORE NULLS') as phone
    from STG.gius_test1;


    Syntax error: Encountered "," at line 2, column 8.

  5. #5
    Join Date
    Apr 2006
    Location
    Belgium
    Posts
    2,514
    Provided Answers: 11
    do not mix different question in same forum entry
    IGNORE NULLS : ?? is this a db2 syntax : this is oracle syntax
    check coalesce in infocenter
    Best Regards, Guy Przytula
    Database Software Consultant
    Good DBAs are not formed in a week or a month. They are created little by little, day by day. Protracted and patient effort is needed to develop good DBAs.
    Spoon feeding : To treat (another) in a way that discourages independent thought or action, as by overindulgence.
    DB2 UDB LUW Certified V7-V8-V9-V9.7-V10.1-V10.5 DB Admin - Advanced DBA -Dprop..
    Information Server Datastage Certified
    http://www.infocura.be

  6. #6
    Join Date
    Jul 2013
    Location
    Moscow, Russia
    Posts
    666
    Provided Answers: 55
    'IGNORE NULLS' is not a function. It's a parameter of a number of OLAP functions including first_value function as in my example above.
    You try to use it incorrectly.
    Regards,
    Mark.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •