Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6913261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:11:44+00:00 2026-05-27T09:11:44+00:00

I’m using Oracle 10g. I have a table with a number of fields of

  • 0

I’m using Oracle 10g. I have a table with a number of fields of varying types. The fields contain observations that have been made by made about a particular thing on a particular date by a particular site.

So:

ItemID, Date, Observation1, Observation2, Observation3...

There are about 40 Observations in each record. The table structure cannot be changed at this point in time.

Unfortunately not all the Observations have been populated (either accidentally or because the site is incapable of making that recording). I need to combine all the records about a particular item into a single record in a query, making it as complete as possible.

A simple way to do this would be something like

SELECT
    ItemID,
    MAX(Date),
    MAX(Observation1),
    MAX(Observation2)
    etc.
FROM
    Table
GROUP BY
    ItemID

But ideally I would like it to pick the most recent observation available, not the max/min value. I could do this by writing sub queries in the form

SELECT
    ItemID,
    ObservationX,
    ROW_NUMBER() OVER (PARTITION BY ItemID ORDER BY Date DESC) ROWNUMBER
FROM
    Table
WHERE
    ObservationX IS NOT NULL

And joining all the ROWNUMBER 1s together for an ItemID but because of the number of fields this would require 40 subqueries.

My question is whether there’s a more concise way of doing this that I’m missing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:11:45+00:00Added an answer on May 27, 2026 at 9:11 am

    Create the table and the sample date

    SQL> create table observation(
      2    item_id number,
      3    dt      date,
      4    val1    number,
      5    val2    number );
    
    Table created.
    
    SQL> insert into observation values( 1, date '2011-12-01', 1, null );
    
    1 row created.
    
    SQL> insert into observation values( 1, date '2011-12-02', null, 2 );
    
    1 row created.
    
    SQL> insert into observation values( 1, date '2011-12-03', 3, null );
    
    1 row created.
    
    SQL> insert into observation values( 2, date '2011-12-01', 4, null );
    
    1 row created.
    
    SQL> insert into observation values( 2, date '2011-12-02', 5, 6 );
    
    1 row created.
    

    And then use the KEEP clause on the MAX aggregate function with an ORDER BY that puts the rows with NULL observations at the end. whatever date you use in the ORDER BY needs to be earlier than the earliest real observation in the table.

    SQL> ed
    Wrote file afiedt.buf
    
      1  select item_id,
      2         max(val1) keep( dense_rank last
      3                              order by (case when val1 is not null
      4                                             then dt
      5                                             else date '1900-01-01'
      6                                          end) ) val1,
      7         max(val2) keep( dense_rank last
      8                              order by (case when val2 is not null
      9                                             then dt
     10                                             else date '1900-01-01'
     11                                          end) ) val2
     12    from observation
     13*  group by item_id
    SQL> /
    
       ITEM_ID       VAL1       VAL2
    ---------- ---------- ----------
             1          3          2
             2          5          6
    

    I suspect that there is a more elegant solution to ignore the NULL values than adding the CASE statement to the ORDER BY but the CASE gets the job done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I have two tables with like below codes: Table: Accounts id | username |
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.