Get the two rows of customer with same amount value within consecutive year

Hi,
Below are sample data, what I would like to do is pull the customer along with their two finance details that have the same amount within the two consecutive year. Please note that there are many customers and not all customers are have these criteria, so the query should not include those customers.
Like in my sample data the result should be:
(first row)
165321 John King Magazine 01/03/2007 30/03/2011 10/02/2009 180.00
(second row)
165321 John King Magazine     01/03/2007 30/03/2011 06/12/2008     180.00
Customer Table:
ID     Name Product     Start Delivery     End Delivery
165321     John King     Magazine     01/03/2007     30/03/2011
165322     Bryan Lee     Magazine     15/04/2002     30/04/2011
Finance Table:
ID          Invoice Date     USD Amount     
165321          10/02/2009     180.00
165321          10/02/2009     120.00
165321          06/12/2008     180.00
165321          20/05/2007     99.00
165321          12/03/2007     150.00
165321          07/03/2007     180.00
165322          01/01/2003     124.00
165322          30/05/2002     160.00
165322          20/04/2002     99.00
Thank you..

Hi,
Depending on your exact requirements, you can do something like this:
WITH     got_this_yr     AS
     SELECT    id
     ,       MIN (invoice_date)               AS invoice_date
     ,       usd_amount
     ,       EXTRACT (YEAR FROM invoice_date)     AS this_yr
     FROM       finance
--     WHERE       ...     -- any filtering goes here
     GROUP BY  id
     ,            usd_amount
     ,       EXTRACT (YEAR FROM invoice_date)
,     got_neighbor_values     AS
     SELECT     id
     ,     invoice_date
     ,     usd_amount
     ,     this_yr
     ,     LAG (this_year)  OVER ( PARTITION BY  id
                                ,           usd_amount
                            ORDER BY      this_year
                          )           AS prev_year     
     ,     LEAD(this_year)  OVER ( PARTITION BY  id
                                ,           usd_amount
                            ORDER BY      this_year
                          )           AS next_year
     FROM     got_this_yr
SELECT       n.id
,       c.name
,       c.product
,       c.start_delivery
,       c.end_delivery
,       n.invoice_date
,       n.usd_amount
FROM       got_neighbor_values     n
JOIN       customer          c  ON     c.id     = n.id
WHERE       n.this_year     IN ( n.prev_year + 1
                    , n.next_year - 1
ORDER BY  n.id
,            n.usd_amount
,       n.invoice_date
;If you'd care to post CREATE TABLE and INSERT statements for the sample data, then I could test this.
This used the analytic LAG and LEAD functions to find the previous and next years when the same amount occurred with the same id. Since we want to use this value in a WHERE clause, it has to be computed in a sub-query (got_neighbor_vlues) first; analytic functions are always computed after the WHERRE clause is applied.
In the case of the very first year, LAG will return NULL, and in the case of the very last year, LEAD will return NULL. That's okay for our purposes; we just need something such that the WHERE clause of the main query will be TRUE if and only if one of the adjacent rows was a consecutive year.
Why don't you want a row for 2007 in the results? It looks like the same customer had the exact same amount in 2007, 2008 and 2009.
What happens if the same customer has the exact same amount 2 (or more) times in the same calendar year? The query above uses the earliest one in each year, and ignores the others. If that's not what you want, then post the results you do want
Edited by: Frank Kulash on Nov 15, 2010 10:36 AM
Included cutomer table

Similar Messages

  • How to get the inserted row primary key with out  using select statement

    how to return the primary key of inserted row ,with out using select statement
    Edited by: 849614 on Apr 4, 2011 6:13 AM

    yes thanks to all ,who helped me .its working fine
    getGeneratedKeys
    String hh = "INSERT INTO DIPOFFERTE (DIPOFFERTEID,AUDITUSERIDMODIFIED)VALUES(DIPOFFERTE_SEQ.nextval,?)";
              String generatedColumns[] = {"DIPOFFERTEID"};
              PreparedStatement preparedStatement = null;
              try {
                   //String gen[] = {"DIPOFFERTEID"};
                   PreparedStatement pstmt = conn.prepareStatement(hh, generatedColumns);
                   pstmt.setLong(1, 1);
                   pstmt.executeUpdate();
                   ResultSet rs = pstmt.getGeneratedKeys();
                   rs.next();
    //               The generated order id
                   long orderId = rs.getLong(1);

  • When our iPad is in use on the internet, my iMac internet connection goes off. I can't get the two devices to work at the same time with the AirPort Express. Is there a setting somewhere I may have missed?

    When our iPad is in use on the internet, my iMac internet connection goes off. I can't get the two devices to work at the same time with the AirPort Express. Is there a setting somewhere I may have missed?

    When two or more devices conflict it typically means that the DHCP service is either misconfigured or is not running at all. DHCP provides IP addresses to local network clients.
    Is your AirPort Express base station the only router in your current network configuration? That is, is it connected directly to an Internet modem and not to another router upstream of it?

  • When using Firefox I can no long get the two finger enlargement to work on my Apple. The same site will enlarge using Safari, so I assume it has to do with Firefox

    When using Firefox I can no long get the two finger enlargement to work on my Apple. The same site will enlarge using Safari, so I assume it has to do with Firefox

    Some gestures have been removed in Firefox 4 and later.
    You can restore the zoom feature by changing the values of the related prefs on the <b>about:config</b> page.
    browser.gesture.pinch.in -> <b>cmd_fullZoomReduce</b>
    browser.gesture.pinch.in.shift -> <b>cmd_fullZoomReset</b>
    browser.gesture.pinch.out -> <b>cmd_fullZoomEnlarge</b>
    browser.gesture.pinch.out.shift -> <b>cmd_fullZoomReset</b>
    browser.gesture.pinch.latched -> <b>false</b>
    To open the <i>about:config</i> page, type <b>about:config</b> in the location (address) bar and press the "<i>Enter</i>" key, just like you type the url of a website to open a website.<br />
    If you see a warning then you can confirm that you want to access that page.<br />
    *Use the Filter bar at to top of the about:config page to locate a preference more easily.
    *Preferences that have been modified show as bold(user set).
    *Preferences can be reset to the default or changed via the right-click context menu.

  • Name of the table where can get the two fields Valid-From and Valid-to for

    Hi gurus
    I want name of the table where I can get the two fields Valid-From and Valid-To and their relation ship with the header table in BOM
    Regards
    Kaisar

    You can only get the Valid from date from the table STKO.
    To get the valid to date, you have to take one day less than the valid-from date of the next record for the same BOM.
    Alternatively use the Function module:
    CSAP_MAT_BOM_READ
    It will give both valid from and valid to dates in the tables parameter: T_STKO
    Regards,
    Ravi
    Edited by: Ravi Kanth Talagana on Jul 2, 2008 4:37 PM

  • Single Vendor Multiple customer with same address

    Hello Guru,
    I have one scenario in retail business.ie I have one single vendor with two different customer but the address of that customer is same in both of them.i.e the vendor address will be same(applicable) in both the customer.But the VAT number is different in both the customer when ever i create the vendor master system should ask the different VAT number.
    Regards,
    Sukdev.

    Yes. I could have laid odds that this would be the first response :-)
    I know it is unusual, and I am aware of the inherent issues with doing something like this. I have read the multipathing guide, but it wasn't that helpful, because I want the same IP address, not just the same subnet. It is NOT for load balancing, or failover support. I'm afraid I can't give too much detail at the moment because of the nature of research project. What I will add is that I want to do this on an 802.1q enabled NIC, on the same physical port, with different logical interfaces for each VLAN having the SAME IP address.
    We can already do this on Linux, and as I said in the original post, the route man page suggests that it can probably be done on Solaris. What I really need to know is how?
    Any takers?
    AJB

  • Getting the current row in a datatable

    Hi,
    I am using datatable. How do I get the current row in the datatable
    and use it for my processing in the JSP page
    or I want to just output that value with out using any jsf tags.
    Examples
    <h:dataTable styleClass="dataTable" id="table1" border="0"                    value="#{myTrainingHandler.myMediaTraining}" var="dataIt">
    <h:column id="column1">
    <f:facet name="header">
    <h:outputText styleClass="outputText" value="Course Name"id="text1"></h:outputText>
    </f:facet>
    <!---------------------------------------------- how do I get the value of #{dataIt.name} and use it in the JSP way
    Reason: I just want to output the value. If I am using h:outputText then it inserts <span>... </span>
    or, I want to transfer that value to another another attribute in the session or something like that.
    ---------------------!>
    <h:outputText styleClass="outputText" id="text2" value="#{dataIt.name}"></h:outputText>
    </h:column>
    </h:dataTable>
    -Aswath

    It looks like good idea!Unfortunately this is just workaround. Indeed your
    suggestion is real JSF-feeling solution but due to the
    bug (or rather pure design of RI implementation???) it
    is dangerous.
    I had some doubt about is it bug or my incompetence.
    Unfortunately this seems is really bug.
    Hope this problem will be solved in the next releases.from blueprints catalog:
    Component bindings (e.g. <h:inputText binding="#{MyBean.lastNameComponent}" />) should always be stored in request scope since their value is no longer valid after a request has been processed anyway. The component tree is reconstituted during each incoming request, changing the physical instances these component bindings will point to.
    as you see there is really problem in using session backing bean with component binding
    so that means idea with parameter looks better now because we don't need to use table binding.
    Or probably we can define some kind of pattern like couple of beans with request scope and session scope.
    for example:
    Catalog - session scope
    CatalogView - request scope
    Catalog will keep session data and CatalogView will provide some presentation logic (including some stricly visual oriented data) and access to business methods of Catalog class.
    I will send you example by e-mail (on friday) if you not mind.
    >
    Thank you for your suggestion!
    Probably I will use your idea in my application.
    By the way I'm from Kiev also.Hello from Kiev :-)
    I have one suggestion.
    I'm working on JSF form that suppose to add, editand
    delete records from table.
    Also I wanna to use scrolling by page and sorting.
    I'm trying to make this form somehow clean and
    universal as much as possible.Seems I have same tasks for now.Great! I will send some code soon.
    And maybe we can discuss every aspect how to do good JSF based application including config oriented information and utilities classes.
    -how do you work with tables without primary keyI dont' have tables without primary key :)
    -how do you make pagination by universal wayWe need just make good component for that.
    We already started in this direction.

  • Is there any way to identify the two classes are compiled by same vm

    Is there any way to identify the two classes are compiled by same vm?
    Thank's a lot.

    I think this is the better forum than java compiler. The answer to the question you asked is no.
    But that question is not the best way to address your problem.
    If this were me, I'd use a tool like ASM (http://asm.objectweb.org/) to read in that file, and replace the getToday() method, with one like this
    long getToday() {
        return 20080101;
    }and replace the class file with this new one. (keep the old one, they might be using a class loader that verifies class signatures or something)
    (They said the code you are looking at could have been changed by you - so take the hint and change it back).
    If they've ripped you off, this will restore justice much more cheaply and effectively than involving the lawyers. If they then sue you for breaching the license agreement, that would probably force them to disclose their underhand tactics.
    OTOH, if you're lying to us about the purchase, and needing to pay for upgrades to fix the performance issue, and instead are trying to crack some trial software, then I hope they are using a custom class loader that checks the integrity of the classes.
    If you are being ripped off, why are you not telling us the name of the company? I am not sure who is being ripped off here.
    Bruce

  • I am using an Airport Extreme for wireless and a Netgear VPN Switch for wired connections.  How do I get the two networks to connect?

    I am using an Airport Extreme for wireless and also have a Netgear VPN Switch (FV5318) for internal wired ethernet connections.  How do I get the two networks to connect to each other?

    Tesserax, I would like to keep the Airport Extreme in nplace before the Netgear FVS318 switch because I am using all of the 8 ethernet outputs distributed to 8 differerent locationsl  This way I can just use one of the LAN outputs from the Airport to feed the FVS318.  Here is what I think I am hearing you say:
    Tne reason why the wired and wireless devices are not communicating is because the FVS318G is also a DHCP server and in conflict with the AEBS.
    To remedy the situastion here is probably what I need to do in order:
    1. Change the LAN IP Address of the Netgear FVS318G to be the same subnet of the Airport Extreme.
    2. Then, disable the Netgear FVS318G as a DHCP server.
    3. Make sure the ethernet cable from the LAN port of the Airport Extreme connectis to the LAN port of the Netgear FVS318G.
    4. Restart everything.
    Can you provide me the steps I need to take to get the right IP addresses from the Airport Extreme to put into the Netgear to fix the conflict?  I have the Airport Utility.  I also have two airport express hot spots, and two Apple TVs in addition to computers attached to the 8 ethernet lines.
    Thank you.
    Steve

  • Hi, I have a late 2011 MacBook Pro but cant get the mirroring function to work with Apple TV.  I've got the most recent Software 10.7.5 but cant see the mirroring icon or turn this on.  I'm able to mirror from my Ipad but not my Mac, any thoughts?

    Hi, I have a late 2011 MacBook Pro but cant get the mirroring function to work with Apple TV.  I've got the most recent Software 10.7.5 but cant see the mirroring icon or turn this on.  I'm able to mirror from my Ipad but not my Mac, any thoughts?

    Welcome to the Apple Support Communities
    AirPlay Mirroring is supported on your Mac, but it needs OS X Mountain Lion (10.8). See > http://support.apple.com/kb/HT5404
    You have two possibilities:
    1. Make a backup, open App Store and purchase Mountain Lion to upgrade to this OS X. Then, you will find the AirPlay Mirroring icon at the top right of the display, on the menu bar, or on System Preferences > Displays.
    2. Keep OS X Lion and use AirParrot > http://www.airparrot.com
    Both options work properly, but I recommend AirPlay Mirroring as it doesn't need a third-party program

  • When I try to sign in to my account on iTunes, I get the above error message, along with "Please review your account information". When I then click on "Review"

    When I try to sign in to my account on iTunes, I get the above error message, along with"Please review your account information".
    When I then click on "Review", it comesup with the page "Create an Apple Account for the iTunes Store"and presents me with the Terms Of Service.
    When I click "Agree",  It is disabled.
    Can anyone tell me why this has happened and how to resolve it?
    Please, please, please help.

    Count me in as having the same problem. I have been leaving messages in the iTunes for Mac forum where others in there also are having problems. I have been unable to access my account since 11/7/2007. E-mails with Apple have not worked and now I haven't heard back from them since Saturday. I have tried both on a Mac and Windows machine and keep receiving the same error message that:
    This Apple ID has not yet been used with iTunes.
    I last purchased music with this account on 10/30/2007. I even tried resetting my password, changing my account info, trying on a computer with iTunes 7.4, etc. I have money in that account and 150 songs and 5-6 tv shows that I cannot access. I also just purchased a new computer and cannot sync my iPod with this computer since these songs will not transfer.
    Apple really needs a phone number for technical support. Having to deal with e-mails back and forth (and waiting a day for each e-mail) is not a good business practice. Hopefully they will have a phone number in the future.
    Either way, count me in on getting annoyed that a week later, this issue has not been fixed.

  • Getting the first row for each group

    Hi Everyone,
    I have a query which returns a number of rows, all of which are valid. What I need to do is to get the first row for each group and work with those records.
    For example ...
    client flight startairport destairport stops
    A fl123 LGW BKK 2
    A fl124 LHR BKK 5
    B fl432 LGW XYZ 7
    B fl432 MAN ABC 8
    .... etc.
    I would need to return one row for Client A and one row for Client B (etc.) but find that I can't use the MIN function because it would return the MIN value for each column (i.e. mix up the rows). I also can use the rownum=1 because this would only return one row rather than one row per group (i.e. per client).
    I have been investigating and most postings seem to say that it needs a second query to look up the first row for each grouping. This is a solution which would not really be practical because my query is already quite complex and incorporating duplicate subqueries would just make the whole thing much to cumbersome.
    So what I really new is a "MIN by group" or a "TOP by group" or a "ROWNUM=1 by group" function.
    Can anyone help me with this? I'm sure that there must be a command to handle this.
    Regards and any thanks,
    Alan Searle
    Cologne, Germany

    Something like this:
    select *
    from (
       select table1.*
       row_number() over (partition by col1, col2 order by col3, col4) rn
       from table1
    where rn = 1In the "partition by" clause you place what you normally would "group by".
    In the "order by" clause you define which will have row_number = 1.
    Edit:
    PS. The [url http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/functions004.htm#i81407]docs have more examples on using analytical functions ;-)
    Edited by: Kim Berg Hansen on Sep 16, 2011 10:46 AM

  • Check box als column in a standard table, how to get the selected row

    Dear experts,
    I habe standard tablt with check box as column. Now I want to get the current selected row structure and do some changes. How could I solve this problem? till now I just know to get the structure via lead selection.
    lo_node->get_element().
    lo_element = lo_node->get_static_attributes ( static_attributes = ls_row).
    How could I get the element through check-box in stead of lead selection. Many thanks!

    check this code
    To get the selected row number
    data: lr_element type ref to if_wd_context_element,
              lv_index type i.
      lr_element = wdevent->get_context_element( name = 'CONTEXT_ELEMENT'  ).
      lv_index = lr_element->get_index( ).
    Thanks
    Bala Duvvuri

  • Is it possible in iTunes 11 to get the old album list view, with covers and track listings like in 10?

    Is it possible in iTunes 11 to get the old album list view, with covers and track listings like in 10?
    If not, then to me that is a huge retrograde step

    No, the old album list view is not an option in iTunes 11...
    You can restore much of the look & feel of the previous version with these shortcuts:
    ALT to temporarily display the menu bar
    CTRL+B to show or hide the menu bar
    CTRL+S to show or hide the sidebar
    CTRL+/ to show or hide the status bar (won't hide for me on Win XP)
    Click the magnifying glass top right and untick Search Entire Library to restore the old search behaviour
    Use View > Hide <Media Kind> in the cloud or Edit > Preferences > Store and untick Show iTunes in the cloud purchases to hide the cloud items. The second method eliminates the cloud status column (and may let iTunes start up more quickly)
    If you don't like having different coloured background & text in the Album view use Edit > Preferences > General and untick Use custom colours for open albums, movies, etc.
    If you still feel the need to roll back to iTunes 10.7 first download a copy of the 32 bit installer or 64 bit installer as appropriate, uninstall iTunes and supporting software, i.e. Apple Application Support & Apple Mobile Device Support. Reboot. Restore the pre-upgrade version of your library database as per the diagram below, then install iTunes 10.7.
    See iTunes Folder Watch for a tool to scan the media folder and catch up with any changes made since the backup file was created.
    tt2

  • I changed a few settings and now cant get the iPad to shut off with the shut off switch and can't back to the settings icon to change it back. Any ideas?

    I changed a few settings on my iPad and now cant get the iPad to shut off with the shutoff button and the button to get to all the icons won't let me get back to settings to change the settings back.  Can anyone help?

    Have you tried resetting your iPad?
    Reset: Press the Home and On/Off buttons at the same time and hold them until the Apple logo appears (about 10-15 seconds).
    No data will be lost.

Maybe you are looking for