Using lag and rank in the same query

Hi
I am trying to find out the difference in time between peoples memberships and also the order that these memberships are taken out in. So far I have added in a rank statement to work out the order the memberships were created in, but now want to look at the difference between the dates returned. The SQL I used is:
SELECT owner_party_id,
mem_number,
support_id,
mem_start_date,
RANK() OVER (PARTITION BY owner_party_id ORDER BY mem_start_date ASC) MEMBERSHIP_SEQUENCE
FROM membership_all
WHERE version_type = 'CUR'
AND owner_party_id IN ('65051', '65051', '65348', '65348', '65607', '65607', '65607')
to get:
"OWNER_PARTY_ID"|"MEM_NUMBER"|"SUPPORT_ID"|"MEM_START_DATE"|"MEMBERSHIP_SEQUENCE"
65051|318874751|8014747|01-MAR-10|1
65051|412311060|21502883|15-AUG-12|2
65348|308672459|3526913|01-MAY-10|1
65348|409951130|20950524|18-JUN-12|2
65607|315830192|7510133|17-MAY-10|1
65607|406448110|20024246|16-MAR-12|2
65607|409738130|20903556|14-JUN-12|3
Now I would like to calculate the difference between the start dates of each of the owner_party_id groups, so to get something like this:
OWNER_PARTY_ID|MEM_NUMBER     |SUPPORT_ID|MEM_START_DATE     |MEMBERSHIP_SEQUENCE|Diff
65051|318874751|8014747|01-Mar-10|1|     
65051|412311060|21502883|15-Aug-12|2|898
65348|308672459|3526913|01-May-10|1     
65348|409951130|20950524|18-Jun-12|2|779
65607|315830192|7510133|17-May-10|1     
65607|406448110|20024246|16-Mar-12|2|669
65607|409738130|20903556|14-Jun-12|3|90
I think that I need to use the Lag function in, but I am not too sure if it can be linkited to look at the data within a grouping of owner party id, as it would make no sense to calculate the difference in dates for two different owner party ids.
Any advice much appreciated.
Thanks
Edited by: 992871 on 09-Mar-2013 23:34

Couple notes:
1. You wrote you want to get order that these memberships are taken out in, however, both your and Etbin's queries calculate order within each owner_party_id and not across all members. If you want to get rank and difference in time regardless of member's owner_party_id remove PARTITION BY caluse.
2. You might want to use DENSE_RANK and not RANK depending how you want to display rank. If two people joined at the same time and were second in rank, analytic RANK will be:
RANK
1
2
2
4
5
.while DENSE_RANK:
DENSE_RANK
1
2
2
3
4
.SY.

Similar Messages

  • How to use a calculated column in the same query

    Hi All,
    I need some help with using a calculated column in the same query.
    For eq
    I am joining a couple of tables and some of the select columns are calculated based on the columns of the tables and i want a new column in the same query to use this calculated feild in some other calcualtion.
    something like this...
    select (12+3) as Sum1, (12-3) as Sum2, (Sum1 + Sum2 ) as Sum3
    from dual
    or
    select (12+3) as "Sum1", (12-3) as "Sum2", CASE WHEN ( "Sum1" / "Sum2" * 100 > 0 ) THEN 'Yes' ELSE 'No' END
    from dual
    Thanks

    user548171 wrote:
    select (12+3) as Sum1, (12-3) as Sum2, (Sum1 + Sum2 ) as Sum3
    from dual
    or
    select (12+3) as "Sum1", (12-3) as "Sum2", CASE WHEN ( "Sum1" / "Sum2" * 100 > 0 ) THEN 'Yes' ELSE 'No' END
    from dual
    ThanksWhat about just repeating the column values:
    select (12+3) as "Sum1", (12-3) as "Sum2", CASE WHEN ( (12+3) / (12-3)  * 100  > 0 )  THEN 'Yes' ELSE 'No'  END FROM DUAL

  • My Ipad mini does not see my printer. my wife's ipad2 does. We both use IOS8 and upgraded at the same time. How can I remedy this?

    My Ipad mini does not see my printer. my wife's ipad2 does. We both use IOS8 and upgraded at the same time. How can I remedy this?

    OK great info!
    I found 2 possible ethernet to USB adapters:
    By "Plugable":  USB 2.0 to ethernet LAN wired network adapter - seems this will work, but I'll contact Plugable.
    2. Apple USB Ethernet Adapter
    Easily connect your MacBook Air computer to an Ethernet network with the Apple USB Ethernet Adapter. Small and light, it connects to the USB 2.0 port of your MacBook Air and provides an RJ-45 connector that supports 10/100BASE-T performance.  BUT NOT SURE IF THIS WILL WORK ON ANYTHING BUT a MacBook Air…
    One person commented: 
    This little gem used to be the device of choice for adding a second Ethernet interface to a Mac Mini Server and I'd been using one that way for about a year. After doing a software update to 10.6.6 it stopped working. Forewarned is forearmed – it appears that Apple is finally enforcing the MacBook Air system requirement.
    Thanks for any additional comments.
    Best regards,
    Steve Schulte
    Sunday 6 January 2013

  • I cannot use data and voice at the same time.

    I cannot use data and voice at the same time.
    I am on Wifi (connected) and at the same time I am on a voice call. when I switch to my RSS feed reader it does not sync it comes out with a message cannot connect to internet, I thought it is a problem with the feed reader application, IE 10 behaves the same I cannot browse when I am on a voice call.
    This is a very basic issue, am I missing something? Please help.
    My Model is lumia 820 with the latest portico update.
    TIA
    Sandeep

    I am aware that packet data via EDGE and voice cannot happen at the same time, surprisingly when I am on voice call I cannot connect to internet via Wifi too

  • Can we use exceptions and conditions at the same time?

    can we use exceptions and conditions at the same time? Are there any dependencies between exceptions an conditions?

    Exceptions are used when there are some mistakes , or exceeds the supposed values, we can highligt that in different colours for enabling the validator to notice easily. In this we are giving conditions for considering the value as exceptions. suppose, if the values is out of the range -1 to +1 then exception.
    But conditions can be considered as the restrictions given in measure level also. So please elaborate what you meant by conditions

  • Use AO and AI in the same program

    Hello,
    I like write a program to use AO and AI in the same program.
    In this program write a timer a voltage to a AO port and after then it should read a voltage.
    I know how I write voltage to a AO port and read a voltage on a AI port, but not in a the same program.
    The read- and the write-Task, should work as soon as possible (<=1ms). 
    (My timer have 100us ticks) 
    what should I inilize for the PCI6229 card, to use AO and AI and in which order?
    Can I have a example or pseudocode?
    Best regards
    Heiko Mayer 

    Hi Heiko-
    There is very little overlap between the AO and AI examples, so you should be able to piece the code together pretty easily.  I would offer these suggestions:
    1.  These lines should only be executed once, and they should be done before any AI or AO specific initialization is done:
    configureTimebase (board);
    pllReset (board);
    analogTriggerReset (board); 2.  It does not matter which order you perform the AI or AO initialization, but I would recommend waiting to unground the AO reference (by   board->AO_Calibration.writeAO_RefGround (kFalse) until all of the AI and AO init is done.  This will help to avoid any glitching on the AO lines.  The only steps that should be saved until after ungrounding the reference are the Start operations for AI and AO (depending on the AO mode you use, there may not be a specific Start operation to call.  Consult the MHDDK examples for more info about the required steps).
    Hopefully this helps-
    Tom W
    National Instruments

  • Can WRT54G use WPA and WEP at the same time?

    I used to have a smc 802.11b wireless router and a dlink 802.11b wireless print server.
    In order to have WPA and a firewall, I replaced the wireless router with a linksys WRT54G.
    Then I realize the dlink print server, even though it has the "g" keyword in the model name, does not do WPA (DP-G321).
    Do I have to buy a 802.11g wireless print server? Or is there another solution?
    I don't supposed the linksys router can do both WPA and WEP at the same time?
    Connecting the print server to the router with a cable is not feasible. They are on different floors.

    both of your devices are has a wireless b technology settings, just set a wep encryption on your network rather than wpa because some wireless b devices don;t work with wpa. with regards to your query if wep and wpa can be used at the same time, the answer is no.

  • My SB Audigy 2 card can't use headset and musicamplifier at the same time

    Hi, i just bought a SB Audigy 2 [D400] and i have downloaded drivers and programs. I run Windows XP SP2.
    The soundcard works fine but it is very very annoying that i physically have to change the jack in the green=line out jack from headset to musicamplifier when i want to use my musicamplifier and change back the jack in the green=line out from musicamplifier to headset when i just want to use the headset/headphones .
    In the shop they told my that i can connect both amplifier and headset at the same time and then with creative software control whether there is sound in the headset or in the amplifier. But i can't simple get it work.
    I tried to use the green to the amplifier and the headset/speakers in the black (line out 2) and then with creative speaker settings softeware choose headphones but still the ampliefier receive the sound and no sound in the headphones. Why? In fact no matter what configuration i try the headphones doesn't work.
    But if i change the jacks so headset is in the green line out then only headset is working and the amplifier doesn't work no matter i put it in sort=line out 2 jack or orange=line out 3 jack.
    Can somebody here tell me if it is possible or not to connect both headset and external amplifier and control by software which to play? and how?
    please help, thx.
    Michael

    If you can have an ASIO compatible music playing system, then it might be possible to have system like you wish when using Creative Audigy 2 card. Since ASIO bypasses Windows Audio System, you can use both 'drivers' (ASIO - WDM/MME/DS) side by side.
    Say you want to play a game (using headset /w mic) and someone other like to listen music at the same time. With ASIO capable system you can output your music for example to "center L/R (stereo) channels" and same time output sounds from other source (using MME/DS/WDM drivers) to normal output channel (Front L/R). Only thing is, you need to set your speakersystem to 4 or 6 (depending which other channel you're using) (and for better 'balance' to adjust "angle" on THX console. You get the THX console by installing Audigy 2 Zs software).
    Try for example http://img332.imageshack.us/img332/463/djs9mo.jpg[/img] width="406" alt="Image Hosted by ImageShack.us" >
    Another option:
    Say you want to play music through external amplifier and play game online with your headset + microphone (eg. others but you listen the music, you play games)
    --> try free http://img332.imageshack.us/img332/4589/tbp7rz.jpg[/img] width="406" alt="Image Hosted by ImageShack.us" >
    If you use ASIO on BeatPort Player you can select L/R individually from the list(s). Same rules as described earlier.
    = You can play from 2 different sources simultaoneusly (ASIO + WDM/MME/DS) to two different outputs without disturbance. The other (WDM/MME/DS) source is set 'automatically' output to (Audigy) "green" port, and the other (ASIO) to "black" or "orange" port (as set).
    .jtpMessage Edited by jutapa on 09-5-2005 05:30 AM

  • Ipod nano (5th generation) - want to use headphones and charge at the same time

    I have an ipod nano (5th generation).  Do I need a special set of headphones to use the headphone jack and charge the ipod at the same time?

    The iPod nanos have a 30 pin dock connector and a headphone jack. When you charge the iPod you use the dock connector therefore you can use and charge at the same time. You cannot use and charge the iPod shuffle at the same time. You might have the two mixed up.

  • How to use ActionListener and ItemListener in the same class

    Why can't I use both ActionListener and ItemListener in the same Class?
    I want to use a drop down list and an entry box on the same form.
    How? I'm having trouble compling......
    Thanks!
    CODE:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Disp extends Applet implements ActionListener, ItemListener
    { TextField entry=new TextField(7);
    Button Enter=new Button("Enter");
    public void init()
    add(entry);
    add(Enter);
    entry.addActionListener(this);
    entry.requestFocus();
    Enter.addActionListener(this);
    public void actionPerformed(ActionEvent thisEvent)
    String answer=entry.getText();

    Simply get your Applet to implement both ActionListener and ItemListener.
    public class MyApplet extends Applet implements ActionListener, ItemListener
      public void init()
        itemWidget.addItemListener(this):
        actionWidget.addActionListener(this);
      public void actionPerformed(ActionEvent e)
        System.out.println("ACTION PERFORMED!");
      public void itemStateChanged(ItemEvent e)
        System.out.println("ITEM STATE CHANGED!");
    }Hope this helps.

  • I have an iPad 2 (wifi 3G). Is it correct to use wifi and 3G at the same time ? Or one at time depending on connectivity? Thanks.

    I have an iPad 2 (Wifi + 3G). Is it correct to use the Wifi and 3G at the same time, or one at a time, depending on connectivity? Thanks in advance.

    Your iPad will use wifi first if available. If wifi is not available, it will use 3G.
     Cheers, Tom

  • Using LIKE and IN at the same time

    Is it possible to use both LIKE and IN at the same time such as:
    select first_name from emp where user_id LIKE IN('1%','2%')
    Thanks in advance
    --Joshua                                                                                                                                                                                                                                                                                                                                   

    Or consider the IN - parameters as a string. Then you can do the following:
    SQL> SELECT ename
      FROM emp
    WHERE EXISTS (
              SELECT NULL
                FROM (SELECT RTRIM (SUBSTR (nums,
                                            INSTR (nums, ',', 1, iter.pos) + 1,
                                              INSTR (nums, ',', 1, iter.pos + 1)
                                            - INSTR (nums, ',', 1, iter.pos)
                                   ) nums
                        FROM (SELECT ',' || 'SCO,BLA,CLA' || ',' nums
                                FROM DUAL) csv,
                             (SELECT ROWNUM pos
                                FROM all_objects
                               WHERE ROWNUM < 50) iter
                       WHERE iter.pos <=
                                  (  (  LENGTH (csv.nums)
                                      - LENGTH (REPLACE (csv.nums, ','))
                                   / LENGTH (',')
                                - 1)
               WHERE ename LIKE '%' || nums || '%')
    ENAME    
    BLAKE    
    CLARK    
    SCOTT    
    3 rows selected.

  • Using EE and XE in the same application?

    After reading the license, it doesn't appear that there is any stipulation against using XE databases alongside an EE database within the same application.
    I have a business app which runs with a centralized database, but clients in many geographically dispersed locations. The idea has been bumped around lately of trying to cache data locally to speed things up. The approach is not so much replication, but of acquiring modification priveleges from the central database, making a copy of the relevant data to a local one, and working with it there for a time. Modifications are then copied back into the central database. This scheme would be aimed at improving response times, and also helping increase the scalability of the overall application. (And yes, our applicaiton adheres strongly to the principle of locality.)
    Thus far, we acquired licenses to BerkeleyDB in order to implement these local data repositories. Oracle XE seems like a more effective route because it presents the immediate benefit that the application would not need to have it's OCI code changed..
    Given that XE supports replication as a client, I hope that this sort of thing has already crossed the minds of Oracle and that it is an acceptable use of XE?

    I am not from Oracle but I cannot see anything in your use case that would violate the license agreement. The usage of XE is free for any purpose and you are allowed to use database links, export / import and even create materialized views from another oracle database directly.
    Regards,
    ~Dietmar.

  • Using counter and output at the same time

    Hi all,
    I'm using a NI 9401 high speed digital IO module (with a cDAQ 9174 chassis) that has 8 digital IO channels, to perform two tasks at once in Labview. I can get both of them to work independantly, but when I put them together to run simultaniousy in parallel, it does not work. My two tasks are simple: 1. Generate a continious pulse train square wave output using one of the counters linked to one of the output channels (PFI7) and 2. Output a 'high' (5V) with one of the other channels (PFI1)  when a certain logic condition is met.
    When I run my code (attached), the pulse train output works, but the output on the other channel doesn't work when the logic condition is met. I should note that the logic condition is that when RMS current exceeds 3A, I want to output a 'true'. I am measuring current using another analog input card connected to the same chassis and this part is all working well. I think the problem is that there is some sort of interference between the pulse train output which is linked to PFI7 and the other output which is PFI1.
    I know there is a limitation with the 9401 card that if the channels are in two groups, 0-3 and 4-7, and you must use a whole group as either all inputs or all outputs, but I don't think this should affect me, as I am using both my channels as outputs anyway, plus I am using channels 1 and 7 anyway.
    If anybody has any ideas as to what could be wrong with my code, I would greatly appreciate it.
    Attachments:
    DSDBI Vegetation testing program.vi ‏115 KB

    OK, you've shot yourself in the foot.
    It is a very bad idea to modify the shipping examples.  Like testing bungee cords by juming off a cliff-  sometimes you only get to try it once.
    Now why on earth did you not add the DI code following the example's method of wiring the error chain and closing the task after the loop?  You have duplicated code in both cases of your case structure the only difference.........(wait for it..........) There is a true constant in the true case where the false case contains a false constant.  A wait for task complete is unnecessary for a 1 sample DI task-  really the write just completes and the task stops all by itself.  the only needed thing here is a DI Write 1 Ch 1 Sample no case structure, and wire the compare output to data to write.
    You should probably add a control task vi with a "Reserve" input prior to each start task on the CO and  AI Tasks and after the Create Task on the DO.  Its a bit technical but the result is like taking vitamins- you'll just get better performance.
    Jeff

  • Is it possible to use SCCP and SIP at the same time?

    Our CCM is using v9.1 and all of our phones are SCCP. We are looking at a conference room phone that runs SIP. Would it be possible to use this in our current setup?

    Hi @bobdchambers,
    That's totally fine. They both co-exist in the same environment but make sure to not configure the same extension in different models because if you dial an extension that is configured in a SCCP phone and a SIP phone, just the SCCP phone rings.
    HTH.
    Rgrds,
    Martin, IT Specialist

Maybe you are looking for