Note that there are some explanatory texts on larger screens.

plurals
  1. POLooking for DB2 SQL query
    primarykey
    data
    text
    <p>I am not very familiar with SQL and I hope some expert here can show me suitable and efficient query for what I want to achieve. I am using DB2 by the way.</p> <p>Below is a screenshot of a sample data. What I need is for a given year, select the record with distinct ID1+ID2+Name columns and maximum (most recent) effective date (in YYYYMMDD format, stored as integer), with the above year being in between YearFrom and YearTo range.</p> <p><img src="https://i.stack.imgur.com/WXTiA.jpg" alt="enter image description here"></p> <p>FOr anyone that cant see a screenshot:</p> <pre><code>NAME YearFrom YearTo ID1 ID2 EffDate item1 2002 2005 AB 10 20091201 item1 2009 2013 AB 10 20100301 item2 2001 2004 XX 20 20050103 item2 2002 2009 XX 20 20060710 item2 2007 2013 XX 20 20090912 item3 2005 2010 YY 30 20110304 </code></pre> <p>I hope I explained it well. For example if user is looking for available items in year 2011, item1 (with eff. date 20100301) and item 2 (with eff. date 20090912) will be returned.</p> <p>If someone is looking for items available in year 2008: item2 (with eff. date 20090912) and item 3 will be returned. Item 1 will not be returned in this case because the most recent record for item 1 has range of 2009-2013.</p> <p>I <em>think</em> I have the first part of the query right, but I dont know how to select the valid records from that results based on the year in one query.</p> <pre><code>select name,id1,id2,max(effdate) from [table] group by name,id1,id2 </code></pre> <p>Any help would be much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload