Rows for which the final quantity is equal to the initial_quantity-1

Hello

I have a table with the following columns: account_id, country_code, initial amount, final amount, unit.
For example:
unit of measure final account_id country_code transitional amount
EN 1 5 1 555
EN 6 8 1 555
EN 11 12 1 555

I want lines that the final amount is equal to the initial_quantity-1.

For this, I have the following query
Select a.country_code,
a.account_ID,
a.initial_quantity,
a.final_quantity,
a.Unit,
of tableX one
inner join tableX b
on a.final_quantity + 1 = b.initial_quantity and a.unit = b.unit
where
and a.country_code = b.country_code
and a.account_id = b.account_id

Run the query, the result is
EN 1 5 1 555

But I want it to appear also row:
EN 6 8 1 555

Because I want to eventually merge this in one line
EN 1 8 1 555

Any ideas?

Thanks in advance.

Kind regards

Hi Frank,.

I'm not sure that you can count on:

ORDER BY initial_quantity

Original quantity can be not necessarily increase or decrease. It can be both:

initial final
------- -----
1       5
3       4
6       2
11     12

Then LAG/LEAD will not work:

INSERT INTO tablex (account_id, country_code, initial_quantity, final_quantity, unit)
       VALUES         (555,     'FR',           1,          5,           1);
INSERT INTO tablex (account_id, country_code, initial_quantity, final_quantity, unit)
       VALUES         (555,     'FR',           6,          2,           1);
INSERT INTO tablex (account_id, country_code, initial_quantity, final_quantity, unit)
       VALUES         (555,     'FR',           3,          4,           1);
INSERT INTO tablex (account_id, country_code, initial_quantity, final_quantity, unit)
       VALUES         (555,     'FR',           11,          12,           1);
INSERT INTO tablex (account_id, country_code, initial_quantity, final_quantity, unit)
       VALUES         (555,     'DE',           1,          2,            1);
INSERT INTO tablex (account_id, country_code, initial_quantity, final_quantity, unit)
       VALUES         (555,     'DE',           3,          5,            1);
INSERT INTO tablex (account_id, country_code, initial_quantity, final_quantity, unit)
       VALUES         (555,     'DE',           6,          8,            1);
INSERT INTO tablex (account_id, country_code, initial_quantity, final_quantity, unit)
       VALUES         (555,     'DE',           11,          12,           1);
COMMIT;

SQL> select * from tablex
  2  /

ACCOUNT_ID COUNTRY_CODE INITIAL_QUANTITY FINAL_QUANTITY       UNIT
---------- ------------ ---------------- -------------- ----------
       555 FR                          1              5          1
       555 FR                          6              2          1
       555 FR                          3              4          1
       555 FR                         11             12          1
       555 DE                          1              2          1
       555 DE                          3              5          1
       555 DE                          6              8          1
       555 DE                         11             12          1

8 rows selected.

SQL> WITH   got_grp_start AS
  2  (
  3   SELECT account_id, country_code, initial_quantity, final_quantity, unit
  4   , CASE
  5     WHEN  initial_quantity =
  6           LAG (final_quantity) OVER ( PARTITION BY  account_id
  7                      ,      country_code
  8           ,  unit
  9           ORDER BY initial_quantity
 10         ) + 1
 11     THEN  0
 12     ELSE  1
 13    END AS grp_start
 14   , CASE
 15     WHEN  final_quantity =
 16           LEAD (initial_quantity) OVER ( PARTITION BY  account_id
 17                         ,         country_code
 18              ,     unit
 19              ORDER BY    initial_quantity
 20            ) - 1
 21     THEN  0
 22     ELSE  1
 23    END AS grp_end
 24   FROM tablex
 25  -- WHERE ...  -- Any filtering goes here
 26  )
 27  SELECT account_id, country_code, initial_quantity, final_quantity, unit
 28  FROM got_grp_start
 29  WHERE grp_start = 0
 30  OR grp_end  = 0
 31  ;

ACCOUNT_ID COUNTRY_CODE INITIAL_QUANTITY FINAL_QUANTITY       UNIT
---------- ------------ ---------------- -------------- ----------
       555 DE                          1              2          1
       555 DE                          3              5          1
       555 DE                          6              8          1

SQL> 

I think that hierarchical query may be a better approach:

SELECT  DISTINCT *
  FROM  tablex
  START WITH ROWID IN (
                       SELECT  a.ROWID
                         FROM  tablex a,
                               tablex b
                         WHERE b.account_id = a.account_id
                           AND b.country_code = a.country_code
                           AND b.unit = a.unit
                           AND b.initial_quantity = a.final_quantity + 1
                      )
  CONNECT BY NOCYCLE account_id = PRIOR account_id
                 AND country_code = PRIOR country_code
                 AND unit = PRIOR unit
                 AND initial_quantity = PRIOR final_quantity + 1
  ORDER BY account_id,
           country_code,
           unit,
           initial_quantity
/

ACCOUNT_ID COUNTRY_CODE INITIAL_QUANTITY FINAL_QUANTITY       UNIT
---------- ------------ ---------------- -------------- ----------
       555 DE                          1              2          1
       555 DE                          3              5          1
       555 DE                          6              8          1
       555 FR                          1              5          1
       555 FR                          3              4          1
       555 FR                          6              2          1

6 rows selected.

SQL> 

SY.

Tags: Database

Similar Questions

  • I document for which the security of the document does not allow me to assemble documents, copy of content, page extraction, form filling, signature & creating the model. Buy adobe acrobat won't solve all?

    I document for which the security of the document does not allow me to assemble documents, copy of content, page extraction, form filling, signature & creating the model. Buy adobe acrobat won't solve all?

    The result is that Adobe does not provide any software to defeat the security of a PDF file. If the creator of the PDF file set to this level of security, intentionally or not, that's what it is! The solution to your problem is to go to the creator of the document and ask the password or an unprotected PDF file version.

    -Dov

  • How cleanly uninstall a program for which the INSTALLATION. There is no such thing as a LOG file?

    I stupidly tried to load the RFViewer software to watch the 1881 census. Failed to load but it appear in the list under Control Panel/uninstall a program. However I can't uninstall it because there is no Install.log file that requires that the program uninstaller.  Is it possible to cleanly uninstall this software?

    Try the free version of http://www.revouninstaller.com/

  • Create a table of values for which a condition is met

    I have exactly this problem, however connected solution is no longer valid: Re: table to automatically generate values if the condition is met,

    Basically I have a table (named 'Master Sheet') that has two columns, one with project names with a status ('Active' or 'pending'). I want a separate table that automatically fills the project names of the leaf of the master for which the status is 'Active '.

    For example, from this table:

    Project

    Status

    Red

    PENDING

    Orange

    ASSETS

    Yellow

    ASSETS

    Green

    ASSETS

    Blue

    PENDING

    I want to automatically generate this table, which contains only values whose status is 'Active' (not 'pending' and without values null):

    Project

    Status

    Orange

    ASSETS

    Yellow

    ASSETS

    Green

    ASSETS

    Please note that using the filter function does not work in this case, as I need to be able to freely add new lines to the two tables.

    Hi Kath,

    Here is a pair of similar solutions.

    That on background uses a writing ('ACTIVE') of the value in the formula in the master-1 table to create the index in column C.

    Above who gets the value of the cell in row 1 (A1) of the table of the single column and lists him only the names of any State were brought to the project.

    Index columns can be hidden.

    Formulas:

    Master: C2 IF (B = C$ 1, ROW (), 999)

    Table 1::A3: IF (SMALL (Master: $C, LINE () −2) = MAX (Master: $C),"", INDEX (Master: $A:$ B, SMALL (Maste r: $C, ROW (−2)), COLUMN ()))

    Master - 1:C2: IF (B = "ACTIVE", ROW (), 999)

    Table 1 - 1:A2: IF (SMALL ("Master-1': C, LINE (−1) ') = MAX ('Master-1': C),' ', index (" Master-1': b, SMA LL('Master-1'::C,ROW()−1),COLUMN()))) ")")

    All forms filled up to the last line of their respective columns.

    Filled with formulas in column A of the table of two columns to right of column B.

    Single column table must be no more than a longer column of its master table.

    Double table column must be as long as his master table or less.

    Kind regards

    Barry

  • Remove unwanted in table 2D lines not knowing the index for which you want to remove

    Hello!

    I'm new on Labview and I hope someone can help me with my problem.

    First import an Excel worksheet into a 2D-string table. For each row in the table, then I want to compare the data in a specific column (which is a date (timestamp)) with the current date. Lines containing a date with number less than today's date, I want to put in a new table and the rest I have no need.

    I managed to create a time stamp for today so that it matches the timestamp of Excel. But for comparison and the creation of a new part of table, I have no idea how do. I tried several ways with no luck at all (probably because I don't understand all the screws that I use).

    Happy for all the help I can get!

    You are on the right track! (although your attempt lists only the datetime values)

    What you need to add/change, it is the resulting table needs to be connected to the loop as a Shift register (with an initial empty table), and in the case you use either Build table to add the current line or send the non modified through.

    You'll probably want to send all through and not only the datetime value.

    You will probably need some tweak to use only the date, but similar to this:

    /Y

  • I'm looking for which generation of an iPad that I, or when it was built. In the "Settings" option, I can only find model number - MDIILL/A

    I'm looking for which generation of an iPad that I, or when it was built. In the "Settings" option, I can only find model number - MDIILL/A

    Click here and look it up.


    (141071)

  • The appleid on my iphone is an old email address that I no longer have access and for which I don't remember the password. My ID apple implemented through my pc is my new e-mail address. How can I change the appleid on my iphone to match my new email addr

    The appleid on my iphone is an old email address that I no longer have access and for which I don't remember the password. My ID apple implemented through my pc is my new e-mail address. How can I change the application on my iphone to match my new email address

    Here is the procedure to change the Apple ID:

    Change your Apple - Apple Support ID

    If bad comes to worse, use the link below to contact the Apple Support.

  • When bookmarking a page, I get the full list of files in the drop-down list to select any folder in which the bookmark page. What you set for complete list of folders in the bookmarks falling?

    When bookmarking a page, I can not get the list of files to drop down to select any folder in which the bookmark page. The "folder:" bar was showing "Bookmarks Menu". The arrow on the bar descends 5 recent folders which pages were saved, but pop arrow to the right of the bar down opens just to show the three categories and the button "new folder". What you set for complete list of folders in the bookmarks falling?

    Start Firefox in Firefox to solve the issues in Safe Mode to check if one of your modules is causing your problem (switch to the DEFAULT theme: Tools > Modules > themes).

    See the extensions, themes and problems of hardware acceleration to resolve common troubleshooting Firefox problems and troubleshooting questions with plugins like Flash or Java to solve common Firefox problems

    See also http://kb.mozillazine.org/Bookmarks_history_and_toolbar_buttons_not_working_-_Firefox

  • How to cancel the composition of Apple's music for which I do not use and therefore do not want to pay for?

    How to cancel the composition of Apple's music for which I don't use, so don't want to pay for?

    Thank you!

    Click here, follow the instructions and wait until it expires. If you must cancel immediately, or wanting a refund of already paid money, Contact Apple.

    (136676)

  • Spyware can be installed in an Internet café, for which all the machines are in working groups

    I have an Internet café and all of the machine are not in the same workgroup, I want to ask if it is possible for spyware to be installed in cyber, because I had the problem of spyware for which all the machine has deep freeze.

    As long as systems are on the same local network and do not allowed of individual firewalls (that is, they rely on the firewall of the router), if the malware gets on one, all are at risk.

  • Purchase a new sealed copy of the upgrade of Windows Vista Edition diskette Home Premium. I found one on which the label says "for distribution in the United States / Canada only. Will this work without problems on a PC in the United Kingdom

    "U.S. / Canada Distribution only" disk for use in the United Kingdom?

    I want to buy a new copy sealed the Windows Vista Edition upgrade disk Home Premium. I found one on which the label says "for distribution in the United States / Canada only. Will this work without problems on a PC in the United Kingdom please? Thank you.

    Hello

    Yes, it will work. However I recommend to buy the disk to upgrade of the United Kingdom.

    The Windows Vista DVD upgrade that you get in the United Kingdom is different from what they get in the United States.

    Please follow the link menitoned.
    Choice of installation for consumer versions of Windows Vista (32-bit only)
    http://support.Microsoft.com/kb/932616
     
    I hope this helps.
  • SQL query to get the items for which transactions have not been saved

    Hi people,

    Have a small doubt I want to compile a SQL query in the inventory where I get these items for what transactions do not have been recorded during a period of at least one specified number of days.

    The days might be 30 days or 2 months depends on. If I want to get those items for which no transactions were reported for lets say 30 days. Could someone give me an idea of how to go about this thing? I use r12. I came up with the following query, but it gives a lot of records. The commented parts of this query is only commented on


    SELECT DISTINCT msi.segment1,

    MSI. Description,

    MSI.primary_uom_code,

    MSI.inventory_item_id

    MSI mtl_system_items_b / *,.

    mtl_material_transactions mmt * /.

    WHERE / * msi.inventory_item_id = mmt.inventory_item_id

    AND msi.organization_id = mmt.organization_id

    AND NVL ((SELECT SUM (transaction_quantity)

    OF mtl_onhand_quantities

    WHERE inventory_item_id = msi.inventory_item_id),

    0) = 0

    AND TRUNC (mmt.transaction_date) < = SYSDATE - & D

    AND * / NOT EXISTS (SELECT *)

    OF mtl_material_transactions mmt

    WHERE msi.inventory_item_id = mmt.inventory_item_id

    AND msi.organization_id = mmt.organization_id

    AND TRUNC (mmt.transaction_date) < SYSDATE - & D);

    Hi fatimakhellil

    Try the following code and mark it as correct if your problem be resolved.

    SELECT DISTINCT msi.segment1,

    MSI. Description,

    MSI.primary_uom_code,

    MSI.inventory_item_id,

    MSI.organization_id,

    Ms.secondary_inventory_name,

    (select max (transaction_date) in the mtl_material_transactions where msi.inventory_item_id = inventory_item_id

    AND msi.organization_id = organization_id) last_transaction_date

    OF mtl_system_items_b msi.

    Ms. mtl_secondary_inventories

    WHERE ms.organization_id (+) = msi.organization_id

    AND msi.source_subinventory = ms.secondary_inventory_name (+)

    AND NOT EXISTS (SELECT *)

    OF mtl_material_transactions mmt

    WHERE msi.inventory_item_id = mmt.inventory_item_id

    AND msi.organization_id = mmt.organization_id

    AND mmt.subinventory_code = ms.secondary_inventory_name

    AND mmt.organization_id = ms.organization_id

    AND TRUNC (mmt.transaction_date) BETWEEN TRUNC (SYSDATE - & d) AND TRUNC (SYSDATE))

  • How to resize a photo for which fills right up to the whole composition window?

    How to resize a photo for which fills right up to the whole composition window?

    Twirl in the transformation of the layer properties and resize it to the top. You can consider using the scale detail Preserving effect if the difference in size is important. Otherwise, its largest size in Photoshop.

    If you don't know how to scale a layer, you should really start here: starting After Effects

  • Order in which the rows are returned from an external table

    Hello
    Anyone know if I can count on the rows returned in the order with a selection of Ext_table.
    For example:

    SELECT * FROM MY_EXT_TAB

    Where MY_EXT_TAB is a file read
    1
    2
    3

    The query will return to the top of the file down, that is to say 1-3.

    Thank you very much.

    Hello

    To get the number of the record since the beginning of the file, you can use the MASS_ADDITION_ID parameter in the external Table definition:

    http://download.Oracle.com/docs/CD/B19306_01/server.102/b14215/ldr_field_list.htm#sthref1264

    You define a column:

     RECNUM
    

    Then, in your application, you can use the operator order of this column.

    In this way, you ensure that you always comply with the registration order in the file.

    Hope this helps.
    Best regards
    Jean Valentine

  • InDesign CS6: Is it possible to create a pdf file in which the version for printing is different from that on the screen?

    Hello

    Everything is in the title!

    The idea is to make a pdf that I send to the customers by e-mail, very graphic with colored backgrounds, but when you print, the background becomes white to save the ink. Similarly for changing the color of the text.

    For example, on the screen when I open a pdf file, I see white text on a blue background, but the impression we see black text on white background.

    Thank you

    Convert the background of a button and set it to appear on the screen, but unprintable.

Maybe you are looking for