Is this type of query possible (select with rows stopping based on match)

I'm trying to write a SQL query and suspect it can't be done. Or at least, I can't mentally grasp it, despite various stabs with subqueries. I've simplified it by changing the metaphor here a little.
Let's say I have a truck in some city (say, Chicago). There are various roads leading out of that city, with routes we'll label according to compass points (N, NE, SW, etc.) Each truck can carry only one shipment. It starts going along its road, and as soon as it comes to a city that needs its shipment, it stops.
Yes, a cooked example, but it's similar to my problem.
Here is a table:
TABLE: truck_routes
scenario (integer - different possible scenarios)
route (NE, SW, etc. - 8 compass directions)
destination (cities along that road)
sequence_number (order in which to go to those cities)
So if I have a trucks in Chicago, I can say
SELECT route, sequence_number, destination
FROM truck_routes
WHERE scenario = 10 (or whatever)
ORDER BY road_out, sequence_number;
And I'd get a report like this:
SE 1 Gary
SE 2 Valparaiso
SE 3 Plymouth
SE 4 Warsaw
NE 1 Michigan City
NE 2 Kalamazoo
NE 3 Grand Rapids
NE 4 Cadillac
NE 5 Traverse City
So far so good. I have another table with a list of cities that need to receive the shipment:
TABLE: cities
scenario (integer - different possible scenarios)
route (NE, SW, etc. - 8 compass directions)
destination (same keys as in truck_routes)
e.g.:
10 SE Valparaiso
10 SE Warsaw
10 NE Cadillac
10 NE Traverse City
Now...the tricky part...how do I rewrite the query above so that after a truck hits a city that needs a shipment (in that scenario) it stops? In other words, I want a report (for all routes) of what cities the truck will stop at - and as mentioned, it stops at the first one it makes a delivery. I don't want to see rows beyond that. So the report would look like this:
SE 1 Gary
SE 2 Valparaiso
NE 1 Michigan City
NE 2 Kalamazoo
NE 3 Grand Rapids
NE 4 Cadillac
Gary shows up because it doesn't require a delivery. Valparaiso (SE route, sequence_number 2) shows up because it takes the delivery. The rest of the Southeast route doesn't show up. Same thing for the NE route.
There are scenarios where I'd actually want to stop at the city just prior to the delivery destination, etc., and of course in the real world it's more complicated, but I think if I could get this model working I could flesh it out into a real-world query.
I can certainly code something in PL/SQL to do this, or do it in application logic - but I'm wondering if there is a way to do it in a single SQL statement? I suspect there's something that could be built with subqueries and sorting, but...I can't quite reach it ;-)

WITH report AS(
    SELECT 'SE' route, '1' seq_num, 'Gary' dest  FROM DUAL UNION ALL
    SELECT 'SE' route, '2' seq_num, 'Valparaiso' dest FROM DUAL UNION ALL
    SELECT 'SE' route, '3' seq_num, 'Plymouth' dest FROM DUAL UNION ALL
    SELECT 'SE' route, '4' seq_num, 'Warsaw' dest FROM DUAL UNION ALL
    SELECT 'NE' route, '1' seq_num, 'Michigan City' dest FROM DUAL UNION ALL
    SELECT 'NE' route, '2' seq_num, 'Kalamazoo' dest FROM DUAL UNION ALL
    SELECT 'NE' route, '3' seq_num, 'Grand Rapids' dest FROM DUAL UNION ALL
    SELECT 'NE' route, '4' seq_num, 'Cadillac' dest FROM DUAL UNION ALL
    SELECT 'NE' route, '5' seq_num, 'Traverse City' dest FROM DUAL 
cities AS (
    SELECT '10' scenario, 'SE' route, 'Valparaiso' city FROM DUAL UNION ALL
    SELECT '10' scenario, 'SE' route, 'Warsaw' city FROM DUAL UNION ALL
    SELECT '10' scenario, 'NE' route, 'Cadillac' city FROM DUAL UNION ALL
    SELECT '10' scenario, 'NE' route, 'Traverse City' city FROM DUAL
need_a_shipment AS (
      SELECT r.*
          , CASE WHEN EXISTS( SELECT 'Y' FROM cities c WHERE c.route = r.route AND c.city = r.dest )
                 THEN 'Y'
                 ELSE 'N'
            END need_a_shipment
    FROM report r
SELECT  route
      , seq_num
      , dest
      , need_a_shipment
FROM need_a_shipment n
WHERE seq_num <= (  SELECT MIN(seq_num) FROM need_a_shipment
                    WHERE route = n.route AND need_a_shipment = 'Y' )
ORDER BY route DESC, seq_num
ROUTE SEQ_NUM DEST          NEED_A_SHIPMENT
SE    1       Gary          N              
SE    2       Valparaiso    Y              
NE    1       Michigan City N              
NE    2       Kalamazoo     N              
NE    3       Grand Rapids  N              
NE    4       Cadillac      Y

Similar Messages

  • ViewObject with rows not based on a query

    Hello,
    I have an ViewObject with rows not based on a query. For example, if in activate method from my application module I insert a row, that row will not show when I test my app module. Please do not answer with a link for SRDemo ADF+BC as i've read it and didn't find any answers there.

    Yes,thanks, I found that, but I don't know how to do a ViewLink that have as destination a viewobject like that. I get all rows from destination for any row from source. There is a way to do that?

  • This type of report ,possible in APEX 3.1(URGENT)

    EMPNO: 1234
    ENAME: X7C7
    DESCRIPTION: DSLFJDSL SDFSDF DFKDSLFJ
    SDLFJSDLF DSFSDLFJS FDKFDF
    ADDRESS: 23/567/9B
    This is a Form like Report, can we create this type of report in
    APEX 3.1

    David,
    that's a good one ! - you are right ! one does not expect that layout as per that name of the template !
    I'll go for it !
    Meanwhile I tried a "workaround" by creating an own template from scratch - but that is quite timeconsuming as per this recommendation of Larry Linnemeyer:
    How to Copy Vertical Report Template
    Thanks to Larry !
    THXS.
    Bernhard

  • SQL Query updateable report with row selector. Update process.

    I have a SQL Query updateable report with the row selector(s).
    How would I identify the row selector in an update process on the page.
    I would like to update certain columns to a value of a select box on the page.
    Using the basic:
    UPDATE table_name
    SET column1=value
    WHERE some_column=some_value
    I would need to do:
    UPDATE table_name
    SET column1= :P1_select
    WHERE [row selector] = ?
    Now sure how to identify the [row selector] and/or validate it is checked.
    Thanks,
    Bob

    I don't have the apex_application.g_f01(i) referenced in the page source...In the page source you wouldn't find anything by that name
    Identify the tabular form's checkbox column in the page(firebug/chrome developer panel makes this easy)
    It should be like
    &lt;input id=&quot;...&quot; value=&quot;&quot; type=&quot;checkbox&quot; name=&quot;fXX&quot; &gt;we are interested in the name attribute , get that number (between 01 and 50)
    Replace that number in the code, for instance if it was f05 , the code would use
    apex_application.g_f05
    --i'th checked record' primary keyWhen you loop through a checkbox array, it only contains the rows which are checked and it is common practice to returns the record's primary key as the value of the checkbox(available as the the i'th array index as apex_application.g_f05(i) , where i is sequence position of the checked row) so that you can identify the record.

  • Illustrator CS5.5 type not editable, not selectable with type tool

    Hi folks, Working on a poster in Illustrator. Have added area type, using the type tool. Now I'd like to increase the size of one line. I can't select or edit any of the type with the type tool. I moved the type to a new layer, saved, closed Illustrator, restarted my Mac (OS X 10.7.4). Looked in preferences, in case there's an issue there. No luck.
    By the way, I also don't see handles when in the selection tool any more. Don't know if that's related.

    Thanks, Monika! Of course, something simple. It was Show Edges. Show Bounding Box was correctly set.

  • Lead selection with a tree-based table.

    I've created a recursive context node to display contents of a KM folder.  The structure has been mapped to a table, which works just fine.  I can expand nodes of the tree and display the contents of sub-folders.  Great.  However, I need to know which backing KM object has been selected via the table.  Since this is a recursive structure, using
    wdContext.currentXXXX()
    does not work, since that's the top of the tree.  Parameter mapping will only give you the column and/or row of the item selected in the table, not the object itself.
    I'm missing something obvious here, but I can't figure out how to get the context element behind the selected row in a table which is displaying a recursive structure.
    I can do this with a Tree structure, since you can map the 'path' parameter to the actual context object.  However, the Table UI doesn't offset that information (a severe oversight, at least in my mind).
    Suggestions?

    Thanks Armin, that works great.
    Had I finished reading this page:
    http://help.sap.com/saphelp_nw70/helpdata/en/60/1f1f056f057d4d962375efd3c92ed0/frameset.htm
    I would have found the correct documentation.

  • Selection with color range based on a channel

    I want to select highlights from a channel with the color range ...
    I select "hightlights" on the color range window.
    Then, which values of the fuzziness and range sliders should I use for selecting highlights equal to the selecting them with ctrl + click on that channel?
    Thanks ...

    lost,
    As I understand this problem, you have a series of numbers in column B and you want to add the last n items (number shown in column A of current row) to the current row. In column C of the current row place the formula:
    =ADDRESS(ROW()-A7+1,2)&":B"&ROW()
    then in column D your result: =IF(ISBLANK(C),"",SUM(INDIRECT(C)))
    pw

  • Firefox keeps asking me what program to use to open file types even though I have selected one and checked "do this for all files of this type" going foward

    I am working in Windows and every time I open an attachment, Firefox asks what program to use to open files of this type. I always select the proper program and then check the box that says use this program for all future files of this type. In spite of that, the program continues to ask for every file. How do I turn off this feature and just have the program remember the correct program to use for each type of file extension?

    Regardless of your download preferences, web servers can trigger a download prompt by sending either the ''Content-Type: application/octet-stream'' or the ''Content-Disposition: attachment'' header. You can check if that's the case by examining the server response headers in the [https://developer.mozilla.org/docs/Tools/Web_Console Web Console].
    For the latter problem install the InlineDisposition add-on and see if it helps.
    * https://addons.mozilla.org/firefox/addon/inlinedisposition/
    If the problem persists, post back here with either an example link or a screenshot of the relevant response headers in the Web Console.
    * [[How do I create a screenshot of my problem?]]

  • FS00 Matchcode selection with " * "

    Hello Guys,
    I'm working on ECC6 and I am facing the following issue: I launch the FS00 transaction, I want to display all the GL account starting with 4 so I type 4* in the matchcode, I press enter.... nothing happens ! It would normally display the list of the selected account.
    Did anyone already face this issue of the the selection with the " * " character ?
    Thanks in advance.

    Dear Moulin,
    It should display all the records starting with 4... if you give 4*. <b>The popup will ask you for the confirmation. You would need to press enter.</b>
    It could be that there are no GL Accounts starting with 4 in the chart of account. Anyways, try searching with 4.
    However, you can test the searchhelp in SE11 as well.
    Regards,
    Naveen.

  • Select top row in Single Query?

    Hi
    Can somebody help me to write a query to get the first row after order by clause using single query alone.
    Example:
    I can write following query to select first row
    select * from (selec * from t order by col1) where rownum = 1;
    But here I should not use inline view to get the result. Because my original requirement needs to use this query in select list and it needs to use a column (of a table from the FROM clause) in the where clause of inline query. Because there is restriction that we can not use the column (of a table from the FROM clause) more than one level of inline query.
    Please help me.

    Raghav.786 wrote:
    Hi
    Can somebody help me to write a query to get the first row after order by clause using single query alone.
    Example:
    I can write following query to select first row
    select * from (selec * from t order by col1) where rownum = 1;
    But here I should not use inline view to get the result. Because my original requirement needs to use this query in select list and it needs to use a column (of a table from the FROM clause) in the where clause of inline query. Because there is restriction that we can not use the column (of a table from the FROM clause) more than one level of inline query.
    Please help me.
    What Oracle version are you?
    If you have 12c you can use
    select col1,...
      from t
    order by col1
    fetch first 1 row only;
    If less than 12c, you have can't do it without a subquery.
    What are you actually trying to do? Read Re: 2. How do I ask a question on the forums?
    and follow the advice there by giving example create table and insert sample data statements and
    explaining clearly what you are trying to do. Then we can help more.

  • Warning Query has exceeded 200 rows.

    Hello gurus,
    I am currently getting a warning message when I run a page and click on a tab more than once.
    The page I am running is a customer html form at the site level. The name of the page is ArAcctSiteOverviewPG.
    There are 7 tabs on this page. When I click on the Site Details tab more than once I get this warning message: "Query has exceeded 200 rows. Potentially more rows exist, please restrict your query."
    If I click on the other tabs more than once I do not get a warning message.
    Anyone encountered this error before?
    This is a standard oracle html form, no customizations has been made. Running R12.1.3.
    Thanks in advanced!

    Query has exceeded 200 rows
    Re: Query has exceeded 200 rows
    Thanks
    --Anil
    http://oracleanil.blogspot.com/

  • Reports with prompting stop working when moved from Crystal10 to crystal 11

    Issue:
    We are having trouble with Crystal Reports XI. It appears that the Crystal 10 runtime included the ability to process reports that used prompting. If we run the same report with the Crystal XI runtime, it fails.
    I can send a sample of the report.
    Error Dialog:
    Title: Internet Explorer Script Error
    Error Details:
    An error has occurred in the script on this page
    Line: 1
    Char: 1
    Error: invalid character
    Code: 0
    URL: file: /// C:/Program%20Files/Business%20Objects/3.0/crystalreportsviewers11/prompting/js/promptengine_strings_en.js
    Do you want to continue running scripts on this page?
    Yes/No
    Our Configuration:
    We use Merge Modules that install Crystal Reports through the browser via a CAB file that we created. It runs a setup executable
    The setup executable project that installs CR XI uses the following Merge Modules:
    CrystalReports11_RDC_Reportengine.msm
    CrystalReports11_RDC_Runtime.msm
    CrystalReports11_HotFix_Merge.msm (This is contrived by us from one of your hot fixes)
    Observation:
    When the install is finished, the folder structure under u2026. Crystalviewers11/prompting do not have any files like they do if we install the development environment.
    Question:
    What do we need to do to correct this situation?
    *why the reports with*
    *prompting stops working when it is moved from a Crystal 10 to Crystal 11*
    *environment.  I believe that it has something to do with Merge Modules*
    *because the files under the prompting folder do not populate with the*
    *merge module like they do with the full install of Crystal Reports.*
    Please help.
    Best Regards,
    Peter Freeman
    Edited by: Vivek Kumar Sharma on May 19, 2009 3:43 PM

    We have JDK 1.6 installed.  The License Merge Module is not related to
    this issue.
    The files under \Program Files\Common Files\Business
    Objects\3.0\crystalreportviewers11\prompting  are not being installed.
    If we copy files, into this folder structure the reports work.
    One of the Business Object's Merge Modules should install the files
    there rather than just an empty folder structure.
    I got was able to figure out how to enter a customer support message.
    They said that they could not support us because we were not on CRXIR2
    (Crystal Reports 11 Release 2).
    I found a download for CRXIR2SP4 (same thing service pack 4).
    I installed it the Merge Module names changed to include a "_5"
    Was:
    CrystalReports11_RDC_Reportengine.msm
    CrystalReports11_RDC_Runtime.msm
    CrystalReports11_HotFix_Merge.msm (This is contrived by us from one of
    your hot fixes)
    Now:
    CrystalReports11_5_RDC_Reportengine.msm
    CrystalReports11_5_RDC_Runtime.msm
    CrystalReports11_5_RDC_License.msm (should have been included)
    *CrystalReports11_HotFix_Merge.msm (not needed anymore)
    I will try to put a installer together with these.  If this work there
    will be some complexity around uninstalling the old one and installing
    the new one.

  • Is it possible to make this type of network with router i have?

    I have Linksys WRT54GC router at home and i share internet to all my PCs through the router.
    Is it possible to share internet firstly through my PC, and then through this router to all other pcs? If it is possible how i should configure my router? If not - do i have to buy some kind of switch to make this type of network?
    Example scheme:
    (Net)
    ---------------------------- PC1 (with 2 NIC)
    (routing) /
    router ------/
    | |___ PC2
    \___ PC3
    I have now this configuration of my PC's and router, but internet on PC2 is not working, but PC2 and router see each other. But ping between PC1 and router (192.168.0.1 192.168.0.2) says "no response":
    MY FIRST PC:
    http://www.improx.info/mypc.JPG
    http://www.improx.info/mypc_2.JPG
    Router:
    http://www.improx.info/router_1.JPG
    http://www.improx.info/router_2.JPG
    http://www.improx.info/router_3.JPG
    PC2:
    http://www.improx.info/pc2.JPG
    or i tried this:
    http://www.improx.info/pc2_var2.JPG
    http://www.improx.info/router_var2.JPG
    Sorry for bad english.
    Best regards,
    Arnas
    Message Edited by imuk on 12-19-2007 07:24 AM

    It is not clear to me what the exact problem is. The screenshots show that everything is correct. The second PC should have internet with this setup. PC1 won't be able to ping the router on 192.168.0.2 if you block ICMP (anonymous WAN requests) on the router. That setting should be on the Security tab.
    I would suggest to set up the WRT as simple wireless access point. That way you still have full LAN connectivity between all your computers. To do that, you have to temporarily wire a computer to a LAN port of the router. Unplug the gateway computer from the router! There is only a single computer connected to the WRT at the moment.
    Open http://10.10.10.1/ or whatever LAN IP address you have set on the WRT at the moment. On the Setup tab, change the internet connection type to "Automatic/DHCP", change the LAN IP address from 10.10.10.1 to 192.168.0.2, disable the DHCP server. Now press the save settings button. You'll loose the connection to the router. That's OK.
    Now rewire the router. Connect a LAN port of the router to the gateway computer. Do not use the internet port on the WRT! That is the basic setup.
    Now wired computers connected to the remaining 3 LAN ports on the WRT should have LAN and internet access. In the same way, wireless clients connect into the ICS LAN and have internet.

  • Is a single Query possible for this?

    Hi everyone,
    I have a requirement to get a list of 10 free (non-assigned) ids from a table. The ids range from say 1-32000. The table contains only the assigned ids. So the idea is - if say we have 15 records already in this table like 2,7,23,4,65,22,665... etc (random, non-repeated, unsorted values), then I need to get the first 10 numbers in the range 1-32000 which are not there in the table e.g. 1,3,4,5,6,8,...etc. One idea is to have a table2 with a single column having values 1-32000 i.e. 32000 rows. Then we can give the query like -
    select col1 from table2 where rownum<=10 and col1 not in (select id from table1);
    Is there a better way to get this output without having a separate table with 32000 rows? Or, is it possible to form a faster query even with the proposed solution?
    Thanks,
    Regards
    Sanchayan
    null

    <BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by [email protected]:
    Hi everyone,
    I have a requirement to get a list of 10 free (non-assigned) ids from a table. The ids range from say 1-32000. The table contains only the assigned ids. So the idea is - if say we have 15 records already in this table like 2,7,23,4,65,22,665... etc (random, non-repeated, unsorted values), then I need to get the first 10 numbers in the range 1-32000 which are not there in the table e.g. 1,3,4,5,6,8,...etc. One idea is to have a table2 with a single column having values 1-32000 i.e. 32000 rows. Then we can give the query like -
    select col1 from table2 where rownum<=10 and col1 not in (select id from table1);
    Is there a better way to get this output without having a separate table with 32000 rows? Or, is it possible to form a faster query even with the proposed solution?
    Thanks,
    Regards
    Sanchayan<HR></BLOCKQUOTE>
    Sanchayan,
    Your problem can be solved by a single query.
    Try the following query:
    (THIS WILL GIVE AN IDEA")
    select * from (
    select rownum from (select numero from srm_test)
    minus
    select numero from srm_test)
    where rownum < 10
    order by 1
    YOU WILL GET:
    ROWNUM
    1
    3
    6
    8
    10
    11
    12
    7 rows selected.
    The contents of srm_test are:
    2, 7, 23, 4, 65, 22, 665, 90, 87, 200, 9, 5
    null

  • ORA-20001: Query must begin with SELECT or WITH

    Hi,
    On a page we have a hidden item :P3_SQL in which we save the dynamic sql query.
    We have a report region of SQL QUERY type whose source is this item :P3_SQL.
    This used to work fine in apex 3.*. We have upgraded our application to 4.0.1 and now we get this error.
    ORA-20001: Query must begin with SELECT or WITH
    I have a workaround for this, but just wanted to make sure somebody recognizes this as a bug in our latest apex version.
    Thanks
    Knut

    Hello Knut,
    I'm not sure if you encountered a bug or an improved validation of your query. If you had just ":P3_SQL" as source, this obviously is neither a correct query nor is it a function returning a query. I guess your workaround is actually encapsulating ":P3_SQL" in a block that returns the value. This is the intended way, not a bug. You just were lucky it was not validated and worked in previous versions.
    If my assumption was wrong, please post both working and non-working solution and a more precise description on when this error actually occurs.
    Thanks,
    Udo

Maybe you are looking for