Can't figure out the correct syntax for this select statement

Hello,
The following statement works great and gives the desired results:
prompt
prompt Using WITH t
prompt
with t as
   select a.proj_id,
          a.proj_start,
          a.proj_end,
          case when (
                     select min(a.proj_start)
                       from v b
                      where (a.proj_start  = b.proj_end)
                        and (a.proj_id    != b.proj_id)
                    is not null then 0 else 1
          end as flag
     from v a
    order by a.proj_start
select proj_id,
       proj_start,
       proj_end,
       flag,
       -- the following select statement is what I am having a hard time
       -- "duplicating" without using the WITH clause
        select sum(t2.flag)
          from t t2
         where t2.proj_end <= t.proj_end
       ) s
  from t;As an academic exercise I wanted to rewrite the above statement without using the WITH clause, I tried this (among dozens of other tries - I've hit a mental block and can't figure it out):
prompt
prompt without with
prompt
select c.proj_id,
       c.proj_start,
       c.proj_end,
       c.flag,
       -- This is what I've tried as the equivalent statement but, it is
       -- syntactically incorrect.  What's the correct syntax for what this
       -- statement is intended ?
        select sum(t2.flag)
          from c t2
         where t2.proj_end <= c.proj_end
       ) as proj_grp
  from (
        select a.proj_id,
               a.proj_start,
               a.proj_end,
               case when (
                          select min(a.proj_start)
                            from v b
                           where (a.proj_start  = b.proj_end)
                             and (a.proj_id    != b.proj_id)
                         is not null then 0 else 1
               end as flag
          from v a
         order by a.proj_start
       ) c;Thank you for helping, much appreciated.
John.
PS: The DDL for the table v used by the above statements is:
drop table v;
create table v (
proj_id         number,
proj_start      date,
proj_end        date
insert into v values
       ( 1, to_date('01-JAN-2005', 'dd-mon-yyyy'),
            to_date('02-JAN-2005', 'dd-mon-yyyy'));
insert into v values
       ( 2, to_date('02-JAN-2005', 'dd-mon-yyyy'),
            to_date('03-JAN-2005', 'dd-mon-yyyy'));
insert into v values
       ( 3, to_date('03-JAN-2005', 'dd-mon-yyyy'),
            to_date('04-JAN-2005', 'dd-mon-yyyy'));
insert into v values
       ( 4, to_date('04-JAN-2005', 'dd-mon-yyyy'),
            to_date('05-JAN-2005', 'dd-mon-yyyy'));
insert into v values
       ( 5, to_date('06-JAN-2005', 'dd-mon-yyyy'),
            to_date('07-JAN-2005', 'dd-mon-yyyy'));
insert into v values
       ( 6, to_date('16-JAN-2005', 'dd-mon-yyyy'),
            to_date('17-JAN-2005', 'dd-mon-yyyy'));
insert into v values
       ( 7, to_date('17-JAN-2005', 'dd-mon-yyyy'),
            to_date('18-JAN-2005', 'dd-mon-yyyy'));
insert into v values
       ( 8, to_date('18-JAN-2005', 'dd-mon-yyyy'),
            to_date('19-JAN-2005', 'dd-mon-yyyy'));
insert into v values
       ( 9, to_date('19-JAN-2005', 'dd-mon-yyyy'),
            to_date('20-JAN-2005', 'dd-mon-yyyy'));
insert into v values
       (10, to_date('21-JAN-2005', 'dd-mon-yyyy'),
            to_date('22-JAN-2005', 'dd-mon-yyyy'));
insert into v values
       (11, to_date('26-JAN-2005', 'dd-mon-yyyy'),
            to_date('27-JAN-2005', 'dd-mon-yyyy'));
insert into v values
       (12, to_date('27-JAN-2005', 'dd-mon-yyyy'),
            to_date('28-JAN-2005', 'dd-mon-yyyy'));
insert into v values
       (13, to_date('28-JAN-2005', 'dd-mon-yyyy'),
            to_date('29-JAN-2005', 'dd-mon-yyyy'));
insert into v values
       (14, to_date('29-JAN-2005', 'dd-mon-yyyy'),
            to_date('30-JAN-2005', 'dd-mon-yyyy'));

Hi, John,
Not that you asked, but as you proabably know, analytic functions are much better at doing this kind of thing.
You may be amazed (as I continually am) by how simple and efficient these queries can be.
For example:
WITH     got_grp          AS
     SELECT     proj_id, proj_start, proj_end
     ,     proj_end - SUM (proj_end - proj_start) OVER (ORDER BY  proj_start)     AS grp
     FROM     v
SELECT       ROW_NUMBER () OVER (ORDER BY grp)     AS proj_grp
,       MIN (proj_start)                         AS proj_start
,       MAX (proj_end)               AS proj_end
FROM       got_grp
GROUP BY  grp
ORDER BY  proj_start
;Produces the results you want:
  PROJ_GRP PROJ_START  PROJ_END
         1 01-Jan-2005 05-Jan-2005
         2 06-Jan-2005 07-Jan-2005
         3 16-Jan-2005 20-Jan-2005
         4 21-Jan-2005 22-Jan-2005
         5 26-Jan-2005 30-Jan-2005This is problem is an example of Neighbor-Defined Groups . You want to GROUP BY something that has 5 distinct values, to get the 5 rows above, but there's nothing in the table itself that tells you to which group each row belongs. The groups are not defined by any column in hte table, but by relationships between rows. In this case, a row is in the same group as its neighbor (the row immediatly before or after it when sorted by proj_start or proj_end) if proj_end of the earlier row is the same as proj_start of the later row. That is, there is nothing about 03-Jan-2005 that says the row with proj_id=2 is in the first group, or even that it is in the same group with its neighbor, the row with proj_id=3. Only the relation between those rows, the fact that the earlier row has end_date=03-Jan-2005 and the later row has start_date=03-Jan-2003, that says these neighbors belong to the same group.
You're figuring out when a new group starts, and then counting how many groups have already started to see to which group each row belongs. That's a prefectly natural procedural way of approaching the problem. But SQL is not a procedural language, and sometimes another approach is much more efficient. In this case, as in many others, a Constant Difference defines the groups. The difference between proj_end (or proj_start, it doesn't matter in this case) and the total duratiojn of the rows up to that date determines a group. The actual value of that difference means nothing to you or anybody else, so I used ROW_NUMBER in the query above to map those distinct values into consecutive integers 1, 2, 3, ... which are a much simpler way to identify the groups.
Note that the query above only requires one pass through the table, and only requires one sub-query. It does not need a WITH clause; you could easily make got_grp an in-line view.
If you used analytic functions (LEAD or LAG) to compute flag, and then to compute proj_grp (COUNT or SUM), you would need two sub-queries, one for each analytic function, but you would still only need one pass through the table. Also, those sub-queries could be in-line views; yiou would not need to use a WITH clause.

Similar Messages

  • Can't figure out the correct xPath for this...

    Hi,
    I'm using Oracle Service Bus and got into a problem with xPath.
    I need to get two parameters - userId and password - from this XML.
    * All I managed to do is get them with _$body//*:userId/text()_ but then I get a string that contacs both occurrences of this parameter on this XML and I only need one!
    * I also tried getting the exact path using _$body/Butterfly/errorData/UserData/userId/text()_ but got nothing. When trying to add the NS (like bas:userId) It's doesn't comply with validation.
    Here's my XML. I would really appreciate your help here!
    <soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <Butterfly xmlns="http://xml.netbeans.org/schema/Butterfly" xmlns:msgns="http: //j2ee.netbeans.org/wsdl/EquipmentController" xmlns:ns1="http://xml.netbeans.org /schema/Butterfly" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <errorData xmlns="">
    <equ:UserData xmlns:equ="http://xml.netbeans.org/schema/EquipmentSchema" x="" mlns:swap="http://xml.netbeans.org/NewDetails" xmlns:bas="http://xml.netbeans.org /schema/BaseSchema">
    <bas: password >1</bas:password>
    <bas: userId >1</bas:userId>
    <bas:FirstIssue>1</bas:FirstIssue>
    </equ:UserData>
    <operation>CloseArgs</operation>
    <service>FinishProcess_Proxy</service>
    <project>MajorProcess</project>
    <request_data_xml>
    <equ:NewDetailsParams xmlns:equ="http://xml.netbeans.org/schema/Equipment Schema">
    <equ:UserData>
    <bas: password xmlns:swap="http://xml.netbeans.org/NewDetails" xmlns:b="" as="http://xml.netbeans.org/schema/BaseSchema">1</bas:password>
    <bas:FirstIssue xmlns:swap="http://xml.netbeans.org/NewDetails" xm="" lns:bas="http://xml.netbeans.org/schema/BaseSchema">1</bas:FirstIssue>
    <bas: userId xmlns:swap="http://xml.netbeans.org/NewDetails" xmlns:bas="http://xml.netbeans.org/schema/BaseSchema">1</bas:userId>
    </equ:UserData>
    <equ:NewDetailsSetDT>
    <equ:Data>
    <swap:Code xmlns:swap="http://xml.netbeans.org/NewDetails">110</swap:Code>
         <swap:Reason xmlns:swap="http://xml.netbeans.org/NewDetails">60</swap:Reason>
    </equ:Data>
    </equ:NewDetailsSetDT>
    <equ:Off>1</equ:Off>
    </equ:NewDetailsParams>
    </request_data_xml>
    </errorData>
    </Butterfly>
    </soapenv:Body>
    Edited by: kobyssh on 03:28 04/02/2010

    Hi gentleman,
    you need the text of userid and username,I think you need add the namespace which contains tow parts:one is the wsdl defination namespace and the other is
    the namespace which you defination just as bas:http://xml.netbeans.org /schema/BaseSchema.
    by the way you can use the OSB XQuery expression which don't need to write the xpath,you can drag it from variable structrue.
    Rgds,
    Jacky(Yang Yi.)

  • What's the correct syntax for this?

    Hey guys
    If...
    trace(rowsHolder.getChildByName("mc"+newStr).y);  
    ...correctly outputs the y coordinate of (in this case) mc001, how do I output the y coordinate of a movieClip called item_base_mc that's within the timeline of mc001?
    trace(rowsHolder.getChildByName("mc"+newStr).item_base_mc.y);
    ...gives me a display object error.
    I've tried permutations of the above with [] brackets in various places, but haven't cracked the code.
    What would be the correct way to write it?
    Thanks for taking a look.
    Shaun

    I don't know how to really explain this, but from documentation you will notice  -   getChildByName()  -  is actually a function from  -  DisplayObject  - not MovieClip.
    Link here provides a better explanation!
    http://curtismorley.com/2007/06/13/flash-cs3-flex-2-as3-error-1119
    GOOD LUCK

  • The correct syntax for a recursive chmod command in Terminal

    I need to set recursive permissions on an external HDD to '777' or 'all read&write'.
    I have tried all of the following commands in Terminal:
    sudo chmod 777 -r /Volumes/...
    sudo chmod -r 777 /Volumes/...
    sudo chmod a+rw -r /Volumes/...
    sudo chmod -r a+rw /Volumes/...
    ...all of which being back a 'no such file or directory' error, e.g.
    sudo chmod 777 -r /Volumes/232Gb
    chmod: -r: No such file or directory
    Can anyone confirm the correct syntax for this command?
    Thanks in advance...
    Message was edited by: ChippyDeluxe

    If there were a solution that didn't involve using Terminal, I would prefer that.
    The reason I ask is, since my old iMac died and I connected my iMac's external HDD to my MacBook, thousands of folders on my external HDD have 'stop signs' on them and require me to go to
    Get Info -> Sharing & Permissions
    ...and add my MacBook username and set the privilege as "read and write". A bigger problem is, no backup software I've tried will back up this external HDD onto another external HDD any more, so I no longer have an up-to-date backup, which is very worrying.
    I am looking for a way of fixing this, and the chmod command appears to be the answer.
    Ideally, I don't want any permission restrictions on my external HDD at all, so I can connect it to any machine without problems.

  • I just bought a MAC and am trying  to figure out the correct version

    I just bought a MAC and am trying  to figure out the correct version of LR4 to install on the Mac.  Someone in support (LR) gave the link for the downloads, and I chose the LR4.4 (but, it said LR5---kind of confusing).  I don't have a DVD drive, so I am not sure if this one will work.  I have the LR icon that says Light Room 4.4 on the screen, but LR does not open when I click it.....????? How do I open this?  Thanks!!!

    I called my ISP to set it to Bridge mode and they said that they couldn't do that (Linksys help desk gave me instructions on the switch to Bridge mode).  However, they then tried to upsell me their own wireless router for $79.99......
    Can I make this change myself or do they need to do it from their end?
    Thanks!

  • How can i figure out the answers to my security questions if i forgot the answers

    how can i figure out the answers to my security questions if i forgot the answers

    Welcome to the Apple Community.
    Start here, and reset your password, you will receive an email with your new password, then go tomanage your account > Password and Security and change your security questions.
    If that doesn't help you might try contacting Apple through iTunes Store Support

  • My MacBook pro mid 2012 doesn't seem to have a "save as" function, so I can't seem to save multiple page pdf's together after moving thumbnails into the sidebar. It only saves the first page. I can't figure out the rename function either. Help?

    My MacBook pro mid 2012 doesn't seem to have a "save as" function, so I can't seem to save multiple page pdf's together after moving thumbnails into the sidebar. It only saves the first page.
    I do have a 2008 version of Microsoft Office installed to save money, but that shouldn't affect things. I can't figure out the "rename" function either. Help?

    Oh, and I also reset my NVRAM, my computer hasn't frozen again yet, so I'm not sure if it has made a difference.

  • I can't download any apps, it keeps telling me "There is a billing problem with a previous purchanse"!  I've checked all my account info and can't figure out the prob??

    I can't download any apps, it keeps telling me "There is a billing problem with a previous purchanse"!  I've checked all my account info and can't figure out the prob??

    Contact Itunes support and ask them

  • If my iPhone was stolen and wiped clean can I figure out the exact time it was wiped and the last GPS location before it was wiped?

    If my iPhone was stolen and wiped clean can I figure out the exact time it was wiped and the last GPS location before it was wiped?

    Sorry, no.

  • Getting message : The result set for this selection was empty on sm20

    Hi,
       I assigned below parameters in rz10 :
    rsau/user_selection                                 1
    rsau/max_diskspace/per_day                  1950M
    rsau/max_diskspace/per_file                 650M
    rsau/enable                                               1
       I activated security audit profile in sm19 also.But when iam going to sm20 for analysis of security audit log iam not getting report iam getting this message  :     The result set for this selection was empty.
    Gudie me for the same.
    Thanku

    Hi,
    The result set for this selection was empty.
    I think your configuration is OK except one thing...
    Check in SM19, if you have selected "Filter Active" check box in "Filter 1 and Filter 2" screen and Also "Audit Classess".
    Even though security audit is enabled in SM19, without selecting filter it will not log the events and give you the above message.
    Regards.
    Rajesh Narkhede

  • I have aol for mac.  I can't figure out the mail or how to retrieve folders or mailboxes

    First, I don't know if I have a mac intel or a ppc.  I don't know what that means.  Anyway, I have had a pc and aol since the dinosaur days.  Just the other day I got a mac.  I have had iphones since they began and my boys have macs so I thought what the heck.  Well, it is so confusing I am pulling my hair out.  My husband says icloud is on my phone, ipad and mac.  I am having trouble keeping all my photos from automatically moving to my phone.  I don't want 1400 photos on my phone, only some of them and can't figure out how to make it do that.
    This question though is about the mail.  I loaded aol onto the mac.  I wanted all my mail to transfer over like it always has when changing pc's.  But it did not give that option with the mac, so it just brought over my account but not my mail, saved folders and all.  I have folders for bills, shopping, insurance, christmas, many many things.  Probably 12-15 of them and when I get email in that I need to keep, I put it in one of those folders and can go back to it at any time.  However with the apple, they don't have folders, they are called mailboxes.  Now I finally figured out how to make new mailboxes for each subject.  However, they do not show up on the left under the mail stuff.  You have to go to toolbar once you highlight the email and go to view, then to move to or something like that, then you see the folders.  If you click on one, supposedly the email goes in there.  However, there is no way to tell for sure because you cannot go to them at all otherwise.  And you can't open the folder cuz it is not in bold unless you are putting something in there.  Then it goes away.  So I don't know if it went in there so I can't delete it and it just keeps piling up.  Also, my sent mail doesn't save automatically, nor does anything else.  Right now my email has a dash out where it says how many emails you have incoming, but it was never there before and I don't think I got any of my mail today that I got now that I am on the pc so how do I take that off.
    Also on the on the mac list it says it has like 80 something there, but it won't let me double click or see what it is, so I don't understand.  I just want to get it organized.
    Thank you so much for your time.  I appreciate all the help I can get.

    Hi Janice
    Fortunately, AOL keeps a copy of mail and folders on their server, so you can be reassured that the email is there, you just have to find a way to access it.
    There are three ways to read AOL mail on your Mac.
    - You can open a browser such as Safari, log into http://aol.com, and read mail there (ie "webmail");
    - You can install the AOL Desktop software, which is probably what you had on the PC. I don't tend to recommend this, as there's usually no need to install extra software, and I've found it very sluggish on a Mac. However, for people who are very used to it and dont want to change, the option is there;
    - You can read AOL email in the Apple Mail application.
    If you have added your AOL to Apple Mail, your mailboxes/folders should be available to you under a heading called "AOL" (you may have named your account differently). The mailboxes are on the left side - as mentioned above, you can hide and show the mailbox panel. (There's a "Show" button on the top left to reveal the mailboxes, which changes to "Hide" when you click it. If you already see "Hide", then you're fine.)
    The server mailbox heading will be at the bottom of the mailbox panel on the bottom left. If you move your cursor over it, and you see the word "Show" appear, click that word. That will reveal the mailboxes within that server account.
    In the example above, my server mail account is called "Google", but the principle is the same. Clicking "Show" will reveal all the mail folders held on the server. The same thing should work for your AOL account.
    Hope that helps.
    Matt

  • What is the correct syntax for Sumif when condition is for a text string?

    B2 and b3 both contain the text "Grc".
    C2 contains 1 and C3 contains 2.
    F2 contains a Sumif that reads as: =SUMIF(B2:B10, "=Grc", C2:C10)
    The result is "0" when it should yield 3
    I can't figure out what happened.

    Hi ishii,
    Your screen shots make no sense to me. I am using Numbers'09 on a MacBook Pro running OS X. Please tell us what device, operating system and app you are using. You still have not told us.
    This forum is for Numbers on a Mac running OS X. Is your screen shot from an iOS device?
    I don't know anything about Numbers for iOS.
    Or are you using Excel?
    I can not help you. If you are using an iPad, you will get a better answer in this forum:
    https://discussions.apple.com/community/app_store/iwork_for_ios?view=discussions
    Regards,
    Ian.

  • Figuring out the correct icloud account

    We had to "clear"out our sons iphone 4 (which is not connected to service) because he forgot his password.  He's using it like an ipod.  When we did this and tried to set it up it is asking us to conncect to an icloud account - but we can't figure out which one.  None of our other 3 devices are set up to the one it's prompting us to nor will it recognize the icloud account that it was origanally linked to.  Any suggestions?

    Thanks Roger this is uncouraging. I went into my iDisk and I found two things. A folder called Istvan's site that opened to a folder called Blog to a file called rss.xml. This file would not open after I downloaded it. The other thing in the same location was a file called sites.rss that I was able to open. I'm hoping that is the one I need to change. Is it?
    The very start of the code has the channel and item tags like you mention in your instructions. Here it is:
    <?xml version="1.0"?>
    <rss xmlns:admhp="http://www.apple.com/DotMac/Homepage" version="2.0">
    <channel>
    <title>i.am.istvan</title>
    <description></description>
    <link>http://web.mac.com/i.am.istvan/iWeb</link>
    <item>
    <title>Blog</title>
    If in fact this is the file I need to alter do I replace the three lines between the two tags with my new location? Or do I do something else? Why do I feel like I'm about to cut the wrong wire? Thanks Roger.

  • How to I find out the correct password for wifi ipod?

    I had to reset my ipod to get rid of the waiting apps that had dl. When I did that I tried to go into the internet but says the password is wrong. I've gone into my router and reset the password used it still says wrong password. I have no idea how to get the correct password for the wifi or where to look.  TIA mary

    Find the name and model number of the router and go to the manufacturer's support site and download the manual.  It will tell you what the default password is.  Sometimes the default passwoard is written on the router.

  • Can someone tell me the correct driver for the fuji xerox docuprint p205b for os x  as the one on the fuji site does not work thank you!

    I am desperate to find the correct driver for the p205b docuprint priner i know that the one on the fuji site is wrong and you need to install a driver for anothe rmodel as I found this in a forum last year and successfully installed on another mac but now I don't know where I got the info from, help !!!  =)

    I have the same printer and I'm using Lion 10.7.4.
    I'm facing problems getting the mac to print via wireless.
    I've installed the driver from http://support.apple.com/kb/DL904 as suggested by sig.
    I've linked up the printer to the router via usb. It works like a charm with usb on Lion and wireless on windows.
    Could anybody help ? I've included photos for more information. Many thanks.

Maybe you are looking for

  • HT4859 How to move songs back up to iCloud?

    I have iTunes Match and had my entire music library stored in iCloud. Recently downloaded a few songs from iCloud music library back to my computer.  Now I want to put them back up in iCloud. How do I do that?  Because if I try to include any "downlo

  • How to get the same period last year value using Fiscal Calendar?

    Hi there, I am using DAX in a Tabular Model project but I am getting stuck trying to get the following: We are using a Fiscal Calendar (from 01 April to 31 March).  Previous Period Value        Value 2012 April 15 May 10 Jun 20 2013 April 15 30 May 1

  • Advise - from dev to prod -

    Hi, We finished playing with OWB and have to move the application to our production environment. Igor Machin said that it can be handy to just have two different runtime environments. So that i can deploy to my production system if the development fa

  • Exception while commiting Tx Currently there are 3 such requests

    Hi, I have a service ( ejb session ) exported to etx. Randomically the client tha invoke this receive the follow exception i work on weblgoic server 8.1 sp6 can anyone help me? thnks for help Error invoking VMOperation in OperationNode 'call_server'.

  • Built-in iSight not detected by iMovie HD

    iMovie HD 6.0.3 doesn't detect my MacBook Pro's built-in iSight camera, despite the fact that it works fine with iChat AV, Flash, and WengoPhone. It displays the message "No Camera Attached" and only "Time Lapse..." appears on the camera selection li