Please can someone help me to build a query

Please can someone help me to build a query for getting the following results. I work with Oracle 9.
My data is about this:
Projectid / Activitycode / Act.DS / Earlystart / ActualStart
{color:#ff0000}P001 / 110M / blabla / 1-1-08 / 1-1-08{color}
{color:#3366ff}P001 / 230M / fdsfds / 31-1-09 / null{color}
P001 / 450M / fsfsd / 1-4-09 / null
P002 / null / null / 1-12-08 / 1-12-08
{color:#3366ff}P002 / 110M / nhggh / 5-2-09 / null{color}
P002 / 750M / wdwdwd / 5-2-09 / null
P002 / 210M / plplplp / 31-12-08 / 31-12-08
{color:#ff0000}P002 / 550M / ewdwd / 5-1-09 / null{color}
I'd like to get one row for each Projectid with the {color:#3366ff}first next Early- or Actualstart{color} {color:#3366ff}after today{color} and one row with the {color:#ff0000}latest early-/actualstart before today{color}. When there are two or more rows with the same date then I want the first (minimum) Activitycode. This last condition makes it insoluble for me!
I've tryed SQL with nested Select-statements in the Where-clause. But I've got still 2 rows per projectid because I select the min(nvl(Earlystart ,ActualStart ) I've tryed SQL with an Select in the FROM-claus with Partion BY, but I can't get it work right.
Can someone show me the right way to solve my problem?

How's this?
with my_tab as (select 'P001' projectid,
                       '110M' activitycode,
                       'blabla' act_ds,
                       to_date('01/01/2008', 'dd/mm/yyyy') earlystart,
                       to_date('01/01/2008', 'dd/mm/yyyy') actualstart
                from   dual
                union all
                select 'P001' projectid,
                       '230M' activitycode,
                       'fdsfds' act_ds,
                       to_date('31/01/2009', 'dd/mm/yyyy') earlystart,
                       null actualstart
                from   dual
                union all
                select 'P001' projectid,
                       '450M' activitycode,
                       'fsfsd' act_ds,
                       to_date('01/04/2009', 'dd/mm/yyyy') earlystart,
                       null actualstart
                from   dual
                union all
                select 'P002' projectid,
                       null activitycode,
                       null act_ds,
                       to_date('01/12/2008', 'dd/mm/yyyy') earlystart,
                       to_date('01/12/2008', 'dd/mm/yyyy') actualstart
                from   dual
                union all
                select 'P002' projectid,
                       '110M' activitycode,
                       'nhggh' act_ds,
                       to_date('05/02/2009', 'dd/mm/yyyy') earlystart,
                       null actualstart
                from   dual
                union all
                select 'P002' projectid,
                       '750M' activitycode,
                       'wdwdwd' act_ds,
                       to_date('05/02/2009', 'dd/mm/yyyy') earlystart,
                       null actualstart
                from   dual
                union all
                select 'P002' projectid,
                       '210M' activitycode,
                       'plplplp' act_ds,
                       to_date('31/12/2008', 'dd/mm/yyyy') earlystart,
                       to_date('31/12/2008', 'dd/mm/yyyy') actualstart
                from   dual
                union all
                select 'P002' projectid,
                       '550M' activitycode,
                       'ewdwd' act_ds,
                       to_date('05/01/2009', 'dd/mm/yyyy') earlystart,
                       null actualstart
                from   dual)
-- above mimics your table; main query is below:
select mt2.projectid,
       mt2.activitycode,
       mt2.act_ds,
       mt2.earlystart,
       mt2.actualstart
from   (select mt.projectid,
               mt.activitycode,
               mt.act_ds,
               mt.earlystart,
               mt.actualstart,
               mt.date_col,
               mt.before_after_today,
               row_number() over (partition by mt.projectid, mt.before_after_today
                                  order by mt.date_col asc) rn_after,
               row_number() over (partition by mt.projectid, mt.before_after_today
                                  order by mt.date_col desc) rn_before
        from   (select projectid,
                       activitycode,
                       act_ds,
                       earlystart,
                       actualstart,
                       coalesce(actualstart, earlystart, to_date('01/01/4000', 'dd/mm/yyyy')) date_col,
                       case when coalesce(actualstart,
                                          earlystart,
                                          to_date('01/01/4000', 'dd/mm/yyyy')) <= trunc(sysdate) then 1
                            else 2
                       end before_after_today
                from   my_tab) mt) mt2,
        (select 1 id, 1 col_id from dual union all
         select 1 id, 2 col_id from dual) dummy
where  dummy.id = case when mt2.before_after_today = 1 then rn_before
                       else rn_after
                  end
and    mt2.before_after_today = dummy.col_id
PROJECTID     ACTIVITYCODE     ACT_DS     EARLYSTART     ACTUALSTART
P001     110M     blabla     01/01/2008     01/01/2008
P001     230M     fdsfds     31/01/2009     
P002     550M     ewdwd     05/01/2009     
P002     110M     nhggh     05/02/2009     

Similar Messages

  • I have an early 2011 MacBook Pro which has been running slow for a while. After looking at responses to similar problems I have downloaded and run EtreCheck and will post the output. Please can someone help me with what it all means.Thanks in advance

    I have an early 2011 MacBook Pro which has been running slow for a while. After looking at responses to similar problems I have downloaded and run EtreCheck. Please can someone help me with what it all means.
    Thanks in advance.
    EtreCheck version: 1.9.15 (52)
    Report generated 19 September 2014 08:07:14 GMT+8
    Hardware Information: ?
      MacBook Pro (13-inch, Early 2011) (Verified)
      MacBook Pro - model: MacBookPro8,1
      1 2.3 GHz Intel Core i5 CPU: 2 cores
      4 GB RAM
    Video Information: ?
      Intel HD Graphics 3000 - VRAM: 384 MB
      Color LCD 1280 x 800
    System Software: ?
      OS X 10.9.4 (13E28) - Uptime: 0 days 0:4:29
    Disk Information: ?
      Hitachi HTS545032B9A302 disk0 : (320.07 GB)
      S.M.A.R.T. Status: Verified
      EFI (disk0s1) <not mounted>: 209.7 MB
      Macintosh HD (disk0s2) / [Startup]: 319.21 GB (147 GB free)
      Recovery HD (disk0s3) <not mounted>: 650 MB
      MATSHITADVD-R   UJ-898 
    USB Information: ?
      Apple Inc. FaceTime HD Camera (Built-in)
      Apple Inc. BRCM2070 Hub
      Apple Inc. Bluetooth USB Host Controller
      Apple Inc. Apple Internal Keyboard / Trackpad
      Apple Computer, Inc. IR Receiver
    Thunderbolt Information: ?
      Apple Inc. thunderbolt_bus
    Gatekeeper: ?
      Mac App Store and identified developers
    Kernel Extensions: ?
      [not loaded] com.seagate.driver.PowSecDriverCore (5.2.4 - SDK 10.4) Support
      [not loaded] com.seagate.driver.PowSecLeafDriver_10_4 (5.2.4 - SDK 10.4) Support
      [not loaded] com.seagate.driver.PowSecLeafDriver_10_5 (5.2.4 - SDK 10.5) Support
      [not loaded] com.seagate.driver.SeagateDriveIcons (5.2.4 - SDK 10.4) Support
      [loaded] com.sophos.kext.sav (9.1.55 - SDK 10.7) Support
      [loaded] com.sophos.nke.swi (9.1.50 - SDK 10.8) Support
    Launch Daemons: ?
      [loaded] com.adobe.fpsaud.plist Support
      [loaded] com.microsoft.office.licensing.helper.plist Support
      [running] com.sophos.autoupdate.plist Support
      [running] com.sophos.configuration.plist Support
      [running] com.sophos.intercheck.plist Support
      [running] com.sophos.notification.plist Support
      [running] com.sophos.scan.plist Support
      [running] com.sophos.sxld.plist Support
      [running] com.sophos.webd.plist Support
      [running] com.trusteer.rooks.rooksd.plist Support
    Launch Agents: ?
      [loaded] com.divx.dms.agent.plist Support
      [loaded] com.divx.update.agent.plist Support
      [running] com.sophos.uiserver.plist Support
      [running] com.trusteer.rapport.rapportd.plist Support
    User Launch Agents: ?
      [loaded] com.adobe.ARM.[...].plist Support
      [running] com.amazon.music.plist Support
      [loaded] com.google.keystone.agent.plist Support
      [not loaded] jp.co.canon.Inkjet_Extended_Survey_Agent.plist Support
    User Login Items: ?
      iTunesHelper
      TomTomHOMERunner
      AdobeResourceSynchronizer
      Dropbox
    Internet Plug-ins: ?
      FlashPlayer-10.6: Version: 15.0.0.152 - SDK 10.6 Support
      DivX Web Player: Version: 3.2.1.977 - SDK 10.6 Support
      AdobePDFViewerNPAPI: Version: 11.0.09 - SDK 10.6 Support
      AdobePDFViewer: Version: 11.0.09 - SDK 10.6 Support
      Flash Player: Version: 15.0.0.152 - SDK 10.6 Support
      EPPEX Plugin: Version: 10.0 Support
      Default Browser: Version: 537 - SDK 10.9
      OVSHelper: Version: 1.1 Support
      QuickTime Plugin: Version: 7.7.3
      SharePointBrowserPlugin: Version: 14.4.4 - SDK 10.6 Support
      iPhotoPhotocast: Version: 7.0 - SDK 10.7
    Safari Extensions: ?
      Ultimate
    Audio Plug-ins: ?
      BluetoothAudioPlugIn: Version: 1.0 - SDK 10.9
      AirPlay: Version: 2.0 - SDK 10.9
      AppleAVBAudio: Version: 203.2 - SDK 10.9
      iSightAudio: Version: 7.7.3 - SDK 10.9
    iTunes Plug-ins: ?
      Quartz Composer Visualizer: Version: 1.4 - SDK 10.9
    3rd Party Preference Panes: ?
      Flash Player  Support
      Perian  Support
      Trusteer Endpoint Protection  Support
    Time Machine: ?
      Skip System Files: NO
      Auto backup: YES
      Volumes being backed up:
      Macintosh HD: Disk size: 297.29 GB Disk used: 160.38 GB
      Destinations:
      Data [Network] (Last used)
      Total size: 2 TB
      Total number of backups: 99
      Oldest backup: 2012-04-20 17:05:32 +0000
      Last backup: 2014-09-18 23:49:25 +0000
      Size of backup disk: Excellent
      Backup size 2 TB > (Disk size 297.29 GB X 3)
      Time Machine details may not be accurate.
      All volumes being backed up may not be listed.
    Top Processes by CPU: ?
          6% InterCheck
          5% iCalExternalSync
          3% WindowServer
          2% CalendarAgent
          2% SystemUIServer
    Top Processes by Memory: ?
      152 MB SophosScanD
      147 MB InterCheck
      106 MB SophosAntiVirus
      66 MB Dropbox
      57 MB com.apple.iTunesLibraryService
    Virtual Memory Information: ?
      161 MB Free RAM
      1.55 GB Active RAM
      1.41 GB Inactive RAM
      902 MB Wired RAM
      611 MB Page-ins
      0 B Page-outs

    Uninstall Trusteer software
    http://www.trusteer.com/support/uninstalling-rapport-mac-os-x
    Remove Sophos
    https://discussions.apple.com/message/21069437#21069437

  • My itunes wont work and wont let my uninstall and then reinstall the newer version as its asking me too! i keep getting an error message!...please can someone help?

    please can someone help me to unistall itunes from my laptop control panel, this means it also wont let me install the latest version!

    I also have Jolly's problem. I found the iMovie 9.0.9 folder and tried to launch the older version of iMove. It would not launch. I removed all of the iMovie preferences from the Preferences folder, removed iMove 10 from the applications folder, and restarted my Mac. iMove 9.0.9 still won't launch and I can't access my videos created with the older version of iMovie. Is there a way to uninstall iMovie 10 and reinstall iMovie 9.0.9?
    I am running Yosemitie on a  iMac.
    Paul

  • I cant install or uninstall my itunes. I get this error 'the feature you are trying to use is on a network resource that is unavilable' Now ive seen the solutions on here but im using windows 8.1. Ive tried all options so please can someone help?

    I cant install or uninstall my itunes. I get this error 'the feature you are trying to use is on a network resource that is unavilable' Now ive seen the solutions on here but im using windows 8.1 and none of them work for me. Ive tried all options so please can someone help?

    That doesnt work for me. I removed itunes through the windows cleaner method as shown in other posts and re-installed itunes. Now im getting this error
    iTunes was not installed correctly. Please reinstall iTunes
    Error 7 (Windows error 126)
    Also my Microsoft office has stopped working after i deleted Itunes which is really strange.
    Can somebody provide me a solution please....

  • HT1438 i keep getting SIM locked message on my iphone 4s and I need to unlock it please please can someone help me? The voice activation is on and when I type in what I think is the code It wont even put in the numbers

    I am trying to unlock my SIM from my iphone 4s, as I am getting a mesage SIM locked. How do I unlock this?Also I am typing in what I think my code is and it is not working. The voice activation wil not allow me to type in the numbers for some reason. I have turned it off and on about 7 times now. please please please can someone help me?

    If you bought the iphone in the US and it is locked to a specific carrier, then you can't use a different sim (from another carrier).  Is this your situation?
    Otherwise I'm not sure what you mean by "trying to unlock my SIM".  In which country did you buy the iphone?  Who's your carrier?

  • HT1926 Trying to re-install iTunes and the message "Apple mobile device failed to start. Verify that you have sufficient privileges to start system services." But I don't know what to do? Please can someone help, I'm not the most technologically advanced

    Trying to re-install iTunes and the message "Apple mobile device failed to start. Verify that you have sufficient privileges to start system services." But I don't know what to do? Please can someone help, I'm not the most technologically advanced person.

    Go to Control Panel > Add or Remove Programs (Win XP) or Programs and Features (later)
    Remove all of these items in the following order:
    iTunes
    Apple Software Update
    Apple Mobile Device Support (if this won't uninstall move on to the next item)
    Bonjour
    Apple Application Support
    Reboot, download iTunes, then reinstall, either using an account with administrative rights, or right-clicking the downloaded installer and selecting Run as Administrator.
    The uninstall and reinstall process will preserve your iTunes library and settings, but ideally you would back up the library and your other important personal documents and data on a regular basis. See this user tip for a suggested technique.
    Please note:
    Some users may need to follow all the steps in whichever of the following support documents applies to their system. These include some additional manual file and folder deletions not mentioned above.
    HT1925: Removing and Reinstalling iTunes for Windows XP
    HT1923: Removing and reinstalling iTunes for Windows Vista, Windows 7, or Windows 8
    tt2

  • Unable to update to ios 5 on windows 7. please can someone help me i have tried everything? i deleted itunes and downloaded the new version when i plug my iphone 4 in it does a back and then I try and update the software it extracts error 3194

    unable to update to ios 5 on windows 7. please can someone help me i have tried everything? i deleted itunes and downloaded the new version when i plug my iphone 4 in it does a back and then I then everything runs normal and it extracts and then i get an error code at the end 3197. had the iphone for two months and its new.
    Please can somene help me, i am currently on version 4.3.3 (8J2)
    Modem Firmware 04.10.01

    followed someones instructions and it has worked ios 5 is now working on my iphone 4

  • I'm unable to send mail please can someone help me fix it

    I have suddenly started to get an error message when sending some of my mail it is telling me to go to my "Temporary Directory Settings" as it's unable to send through C:\Users\Tina\AppData\Local\Temp\nsmail.tmp
    I have tried unticking my email attachments in my anti virus as I was told to do the last time I had a problem, but to no avail. Please can someone help me fix this and preferably in step by step form ?

    I suggest you contact McAfee for instructions. I don't use their product and a year after it started making life hard for Thunderbird users i am sure they can offer constructive workarounds.

  • My apple ID is showing someone elses email address and not mine so when i want to buy or update apps it brings this other email address up. please can someone help how do i put it back to my email address?

    my apple ID is showing someone elses email address and not mine so when i want to buy or update apps it brings this other email address up. please can someone help how do i put it back to my email address?

    First go here - https://appleid.apple.com/cgi-bin/WebObjects/MyAppleId.woa/ and login under the manage tab.  Check your information and be sure it is all correct.
    An AppleID is nothing more than an email address, so if you see another email address in the store, then someone has logged in with a different AppleID than yours.  Did you let someone else use your iPhone?

  • My ipod touch 5 turn its self off and wont come back on please can someone help

    my ipod touch turned its self off Last night had half of battery left and I cant turn it back on please can someone help cant live with out it

    Have you tried to reset it? Hold down the sleep and home keys for about 20 seconds. If you see the silver apple, let go and let it reboot and see if it comes back

  • My iPod touch isn't working correctly.it is displaying a white light and no matter what i try it isn't helping.tries holding the reset and power button together and tryed holding the power button in but it doesn't work. please can someone help?

    my iPod touch isn't working correctly.it is displaying a white light and no matter what i try it isn't helping.tries holding the reset and power button together and tryed holding the power button in but it doesn't work. please can someone help?

    Try the remaining items in the white-screen topic of.
    iPod touch: Hardware troubleshooting
    If not successful. let the battery fully drain and then charfe for an hour and try again.
    Frequently a white scree nis due to a hardware problem.

  • Please can someone help - I am trying to download flashplayer compatible with Windows8

    Please can someone help with FLashplayer trying to find the download compatible with WIndows 8

    Hello,
    There are two versions of Flash Player for Windows, ActiveX (for Internet Explorer) and Plug-in (for other browsers).  On Windows 8.x, Microsoft updates Flash Player ActiveX via Windows Update service.  For Plug-in, you can go to get.adobe.com/flashplayer (with the appropriate browser) and proceed with installation.  Note that Google Chrome also includes a seperate Flash Player, which Google updates via Chrome update.
    Maria

  • My MacBook desktop wallpaper has dissapeared and now its just a blue screen? and my toolbar at the bottom of the screen has dissapeared as well. Please can someone help !!?

    my MacBook desktop wallpaper has dissapeared and now its just a blue screen? and my toolbar at the bottom of the screen has dissapeared as well. Please can someone help !!?

    Try restarting your Mac.
    If that doesn't help, open a Finder window. Select your Home folder (it has a small house icon) in the Sidebar on the left.
    Now open the Library folder then the Prefernces folder.
    Move the com.apple.desktop.plist file to the Trash. Restart your Mac one more time.

  • Please can someone help me out. I initial configure my bluetooth stereo ear piece with my iphone 4 and it was working but I mistakenly forget the bluetooth device and I cannot reconfigure it again with my iphone 4.

    please can someone help me out. I initial configure my bluetooth stereo ear piece with my iphone 4 and it was working but I mistakenly forget the bluetooth device and I cannot reconfigure it again with my iphone 4?

    place the device in pairing mode and re-pair.
    If you don't know how to do this, go to the manufacturer's website and download the user guide for your device.

  • My Apple TV (2nd Generation) continually requests authorisation, even after my MacBook says I am authorised. Please can someone help?

    My Apple TV (2nd Generation) continually requests authorisation, even after my MacBook says I am authorised. Please can someone help?

    Welcome to the Apple Community.
    Are your Store location and other details set up correctly on the Apple TV. have you tried de/re-authorising your MacBook.

Maybe you are looking for

  • How to display icon in alv report output

    hi, my ewquirement in in alv report in one column to diaplay the icon(tickmark) based on some codition. how to achieve it?? condition is Affected (Locked on Current ECM) u2013 can use symbols    for affected and   for changing u2013 u2022     Lock AE

  • Can I go from OS 10.8.5 back to 10.8.2?

    I have an iMac Intel core i5 w/ 16G ram. Bought it used with lots of software. Upgraded from 10.8.2 to 10.8.5, and some of the extra software won't run now. Mainly I'm concerned with CS5.1. My use of CS is low-volume, I don't need cutting edge, and I

  • OAF Attachment Table is not visible

    Hi Experts, I am trying to add the standard attachment region to a page. Have created Attachment Region using Personalization and Entity name : custom entity name View : Standard VO used in the page Primary Key : Unique column from the VO When I adde

  • Extend Wi-fi with D'Link N300

    I'm new to BT and have the HH5 however the internet drops out loads around the house. Most of the time it kicks devices of the connection then tells me that the password is wrong to connect. However I am keen to try using my good old trusty DL N300 a

  • How do I clear out the messages storage content?

    When I look under storage I see 1.1 gig set for messages / deleted messages.  How do I free up this used and allocated space?