Compare and get the data in the tables (see post for details)

I have two tables TableA and TableB. I struggle to write a query to get the dates of TableB (instead of TableA dates) where TableA dates don't coincide with the tableB (beginning and end).

Example 1: For account A1234,.

TableA got 2 rows 1/31/2014-3/3/2014 (which corresponds to TableB row), but TableA got another rank 31/01/2014 - 31/01/2014 that corresponds with the date of TableB Begin. In this case, I'm looking for output as below,

Use TableB start and end date and combine number two rows from TableA for this account

ACCOUNTTableB_BEGINTableB_ENDAMOUNT
A123431/01/201403/03/2014100.0000000000

Example 2: For the B7777 account.

TableA end date aligns with the end dates of TableB for this account, in this case I want out put as,.

Use TableB start and end date and get the amount of TableA

ACCOUNTTableB_BEGINTableB_ENDAMOUNT
B777705/04/201306/05/2013200.0000000000

Example 3: On behalf of D5555,.

Even a TableA line corresponds with TableA, there are two other rows in TableA matching start date with TableA and correspondence with the end date with TableA, in this case, that I put as,.

Use TableB start and end date and combine number three rows from TableA for this account.

ACCOUNTTableB_BEGINTableB_ENDAMOUNT
D555508/08/201410/09/20141100.0000000000

Example 4: To account E6666.

Table corresponds to a row with TableB and no additional lines in TableA, just display the data in A table

Tables and data:

create table TableA
(
  Account varchar2(10) not null,
  Begin   date not null,
  End     date not null,
  Amount  number(19,10) not null
)
;


create table TableB
(
  Account varchar2(10) not null,
  Begin   date not null,
  End     date not null,
  Amount  number(19,10) not null
)
;


TableA Data:


insert into tablea (ACCOUNT, BEGIN, END, AMOUNT)
values ('A1234', to_date('31-01-2014', 'dd-mm-yyyy'), to_date('31-01-2014', 'dd-mm-yyyy'), 0.0000000000);


insert into tablea (ACCOUNT, BEGIN, END, AMOUNT)
values ('A1234', to_date('31-01-2014', 'dd-mm-yyyy'), to_date('03-03-2014', 'dd-mm-yyyy'), 100.0000000000);


insert into tablea (ACCOUNT, BEGIN, END, AMOUNT)
values ('B7777', to_date('18-04-2013', 'dd-mm-yyyy'), to_date('06-05-2013', 'dd-mm-yyyy'), 120.0000000000);


insert into tablea (ACCOUNT, BEGIN, END, AMOUNT)
values ('C6666', to_date('25-06-2014', 'dd-mm-yyyy'), to_date('08-07-2014', 'dd-mm-yyyy'), 10.0000000000);


insert into tablea (ACCOUNT, BEGIN, END, AMOUNT)
values ('D5555', to_date('08-08-2014', 'dd-mm-yyyy'), to_date('16-08-2014', 'dd-mm-yyyy'), 1000.0000000000);


insert into tablea (ACCOUNT, BEGIN, END, AMOUNT)
values ('D5555', to_date('08-08-2014', 'dd-mm-yyyy'), to_date('10-09-2014', 'dd-mm-yyyy'), 0.0000000000);


insert into tablea (ACCOUNT, BEGIN, END, AMOUNT)
values ('D5555', to_date('16-08-2014', 'dd-mm-yyyy'), to_date('10-09-2014', 'dd-mm-yyyy'), 100.0000000000);


insert into tablea (ACCOUNT, BEGIN, END, AMOUNT)
values ('E6666', to_date('01-01-2014', 'dd-mm-yyyy'), to_date('01-02-2014', 'dd-mm-yyyy'), 100.0000000000);


TableB Data:


insert into tableb (ACCOUNT, BEGIN, END, AMOUNT)
values ('A1234', to_date('31-01-2014', 'dd-mm-yyyy'), to_date('03-03-2014', 'dd-mm-yyyy'), 100.0000000000);


insert into tableb (ACCOUNT, BEGIN, END, AMOUNT)
values ('B7777', to_date('05-04-2013', 'dd-mm-yyyy'), to_date('06-05-2013', 'dd-mm-yyyy'), 200.0000000000);


insert into tableb (ACCOUNT, BEGIN, END, AMOUNT)
values ('C6666', to_date('06-06-2014', 'dd-mm-yyyy'), to_date('08-07-2014', 'dd-mm-yyyy'), 10.0000000000);


insert into tableb (ACCOUNT, BEGIN, END, AMOUNT)
values ('D5555', to_date('08-08-2014', 'dd-mm-yyyy'), to_date('10-09-2014', 'dd-mm-yyyy'), 1100.0000000000);


insert into tableb (ACCOUNT, BEGIN, END, AMOUNT)
values ('E6666', to_date('01-01-2014', 'dd-mm-yyyy'), to_date('01-02-2014', 'dd-mm-yyyy'), 100.0000000000);




SELECT A.ACCOUNT,
       A.BEGIN,
       A.END,
       A.AMOUNT,
       B.ACCOUNT,
       B.BEGIN,
       B.END,
       B.AMOUNT
  FROM TABLEA A
  LEFT JOIN TABLEB B
    ON A.ACCOUNT = B.ACCOUNT

Hello

SeshuGiri wrote:

Hi Frank,.

Your query/solution works very well, but I forgot to mention something in the first post...

Please insert these additional lines and try the request again.

TableA Additional lines:

  1. Insert into TABLEA (ACCOUNT, BEGIN, END, QUANTITY)
  2. values ('F9999', to_date (January 2, 2014 ',' dd-mm-yyyy ""), to_date (3 January 2014 ', 'dd-mm-yyyy'), 999.0000000000);
  3. Insert into TABLEA (ACCOUNT, BEGIN, END, QUANTITY)
  4. values ('A1234', to_date (March 3, 2014 ',' dd-mm-yyyy ""), to_date (4 March 2014 ', 'dd-mm-yyyy'), 999.0000000000);

TableB Additional lines:

  1. Insert into TABLEb (ACCOUNT, BEGIN, END, QUANTITY)
  2. values ('A1234', to_date (March 3, 2014 ',' dd-mm-yyyy ""), to_date (4 March 2014 ', 'dd-mm-yyyy'), 999.0000000000);

Question 1:

The table has a rows for A1234 account (i.e. the time period different than the ranks for the same account)

one is A1234 31/01/2014-03/03/2014, A1234 03/03/2014-03/04/2014

Your query that returns two rows for A1234 account (which is what I want), but the amount is messed up.

ACCOUNT BEGIN END TOTAL_AMOUNT
1 A1234 31/01/2014 03/03/2014 1100
2 A1234 03/03/2014 03/04/2014 1100

Except:

ACCOUNT BEGIN END TOTAL_AMOUNT
1 A1234 31/01/2014 03/03/2014 101
2 A1234 03/03/2014 03/04/2014 999

Question 2:

In some cases TableA will have an account (F9999), but the TableB don't. I can just this line by making the Left Join right join?

I don't get the results with additional data. I get 1099 for two lines where account = 'A1234 '.  I get 1100 as the amount on the line with the account = "D5555.  You did it other changes to data?

Except:

ACCOUNT BEGIN END TOTAL_AMOUNT
1 A1234 31/01/2014 03/03/2014 101
2 A1234 03/03/2014 03/04/2014 999

Still, I don't see why you want to 101 for the amount of the first row.  Why not 100?

How can you know which rows from tablea should get attached to what rows from tableb, when the account is not unique?

Maybe you want something like this:

SELECT a.account

b.begin

b.end

SUM (a.amount) AS total_amount

FROM tablea a

ON a.account = b.account JOIN tableb B

AND a.begin BETWEEN b.begin

AND b.end

AND a.end BETWEEN b.begin

AND b.end

GROUP OF a.account, b.begin, b.end

ORDER BY a.account

;

but I guess just to your needs, and guessing is not a very good or reliable way to solve problems.

Question 2:

In some cases TableA will have an account (F9999), but the TableB don't. I can just this line by making the Left Join right join?

Yes, it looks that you want an outer join.  What happened when you tried?  As always, post your code, the exact results you want from the given sample data, as well as an explanation of how you get these results from these data.

Tags: Database

Similar Questions

  • Compare and get the number of days since the same date field

    Hello

    I need to get the results in days when comparing the same field, date

    For example,.

    Primary_key_field Date_field
    100000002 1 January 10
    100000004 February 1st, 10
    100000005 30 April 10
    100000006 April 18, 10
    100000007 29 April 10
    100000008 May 1st, 10

    extract the first two date_field records based on the primary_key_field. (January 1, 2010 and February 1, 2010) and compare and find the difference and who appear in number of days, the same fetch two disks and do the same math... How can I achieve this... is it possible to make the SQL thro? Please help me solve this problem...

    Thank you and best regards,
    KBG.

    This is the query using functions oracle Analytics

    Primary_key_field Date_field
    100000002 1 January 10
    100000004 February 1st, 10
    100000005 30 April 10
    100000006 April 18, 10
    100000007 29 April 10
    100000008 May 1st, 10

    Select rn, date_field, next, next - date_field "Diff".
    de)
    ROW_NUMBER() SELECT over (ORDER BY primary_key_field) rn,
    date_field, LEAD (date_field) OVER (ORDER BY date_field) next
    FROM table_name)
    /

  • I am facing some problems with the installation of data protection. Please see below for details

    We use the data protection in our project.  The research that I have done in protecting data, I realized that the FDM adds the data in protected intersections to load the file that is created by FDM via the import process. The problem is that there are actions given in HFM and I get an error that says: "error: load the file cannot contain action data according to user option." I'm pretty sure that some user option (it would be great if you have an idea where I can find this user option) prevents data already present of shares to be loaded via FDM. Have faced this kind of error before? Is there a work around on this?

    Thank you

    Rohit

    Hello

    The user option which is the origin of the problem is 'File Contains Data property'. You must make sure that this load option is enabled when you start loading in FDM process if the dataset that you present must contain data of shares.

    Kind regards

    Simon

  • Starting from two data tables, how do you get the values in two columns using values in a column (values get col. If col. A is not null values and get the pass. B if col. A is null)?

    Two tables provided, how you retrieve the values in two columns using values in a column (the pass get values. If col. A is not null values and get the pass. B if col. A is null)?

    Guessing

    Select nvl (x.col_a, y.col_b) the_column

    from table_1 x,.

    table_2 y

    where x.pk = y.pk

    Concerning

    Etbin

  • How to set vm-description/notes and get the name of the data store, where the virtual machine

    Hello guys,.
    I have vCenter Orchestrator 4.1.1 build 733 installed and it works fine, but I need your help for the following two issues:
    (1) I want to put the description/notes of a virtual machine using a workflow. But I have not found any API useful to create this workflow (I don't want custom attributes, see attachment for details).

    (2) how can I get the name of the data store, where the virtual machine? I need this name for a workflow.
    I need your help.
    Thanks in advance!

    With regard to the notes of the VM, the following code (see enclosed package) can do this:

    var oldNotes = vm.summary.config.annotation;
    If (oldNotes == null) {oldNotes = ' ' ;}}
    System.log ("Notes of VM current:" + oldNotes);
    Now put the new notes:
    Start by creating a context
    Context of var = new VcVirtualMachineConfigSpec();
    Update the annotation with the new value property
    configSpec.annotation = notes;
    launch the task to reconfigure the virtual machine with the new context
    NOTE: This is sure to apply with a virtual machine under tension
    var task = vm.reconfigVM_Task (configSpec);

    And, in what concerns the VM information, take a look at the workflow of the library: \Library\vCenter\Virtual Machine management\Others\Extract virtual machine information

  • Problem with DVD player and get the error code 19 Device Manager

    Original title: TSSTcorp CDDVDW SH - 224DB ATA device

    Windows cannot start this hardware device because its information of configuration (in the registry) is incomplete or damaged. (Code 19)

    How to fix it please Message Please please how to fix it

    Hi Emerald,

    Please contact Microsoft Community. I understand that you have a problem with the CD\DVD drive and get the error code 19 Device Manager. I've surely you will help solve this problem.

    To better understand the issue, I would like to know if you have any recent software or hardware on the computer changes before this problem?

    This problem might have occurred because of these reasons:

    1. Install or uninstall the CD or DVD recording programs.
    2. Uninstall Microsoft Digital Image.

    Try the steps listed here and see if it helps:

    I suggest you try the procedure described in article which will correct corrupted registry entries. Check if it helps.

    Your CD-ROM or DVD drive is not recognized by Windows or other programs
    http://support.Microsoft.com/kb/314060/en-us

    Note: This section, method, or task contains steps that tell you how to modify the registry. However, serious problems can occur if you modify the registry incorrectly. Therefore, make sure that you proceed with caution. For added protection, back up the registry before you edit it. Then you can restore the registry if a problem occurs. For more information on how to back up and restore the registry, click on the number below to view the article in the Microsoft Knowledge Base:

    Hope this information helps. Reply to the post with an up-to-date report of the issue so that we can help you further.

  • Subtract two time_clock and get the result minutes

    Hi Experts,

    Nicely, I would subtract two type of time_clock that varcahr2 to the database and get the minutes of the result.

    Data:

    Select ' 08:00 ' as start_time_clock, 15:00 ' as the double end_time_clock

    Union of all the

    Select ' 09:00 ', 16:30 ' of the double

    Expected result:

    start_time_clock end_time_clock count_minutes

    ----------------           --------------              -------------

    08:00                      15:00                       420

    09:00                      16:30                       450

    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - production


    Thanks in advance

    WITH dataset

    AS (SELECT ' 08:00 ' AS start_time_clock, 15:00 ' AS end_time_clock)

    OF THE DOUBLE

    UNION ALL

    SELECT ' 09:00 ', 16:30 ' DOUBLE)

    SELECT (to_date (end_time_clock,'HH24:MI')-to_date(start_time_clock,'HH24:MI')) * 24 * 60))

    Of THE dataset

  • How do I get the table cells?

    It's my get code that any object in propagation.

    Int32 childCount = parent-> GetChildCount();

    for (int32 /IndexEnfant = 0; childIndex < childCount; /IndexEnfant ++)

    {

    InterfacePtr < IHierarchy > child (parent-> QueryChild (childIndex));

    ChildUIDRef = UIDRef: GetUIDRef (child);

    TextFrame table

    }

    How do I get the table cells?

    You can access the table using ITableModel and GridAddresscell.

    But to get to the ITableModel since ITextModel implies some complexity.

    Few things to understand:

    -L'interface that corresponds to the written document is ITextModel

    -The recommended method to iterate tables in a story done by ITextStoryThreadDictHier

    -Get the ITextStoryThreadDictHier on the history and the main thread as ITextStoryThreadDictHier::NextUID()

    -Now get ITextStoryThreadDict for UID and query for ITableModel. If you get ITableModel, then you have found the table.

    Once you have ITableModel you can get ITableCell as follows:

    ITableModel:QueryCellContentBoss(GridAddress); Give ITableCell

    You can refer to SnipperRunner (SnpManipulateTextModel).

    Download text template table model:

    InterfacePtr textModel (storyUIDRef, UseDefaultIID());

    InterfacePtr textStoryDictHier (textModel, UseDefaultIID());

    NextUID UID = sourceStory.GetUID (); / / Download the main thread dictionary

    Now loop the dicthier to iterate through all the threads dicts.

    InterfacePtr textStoryThreadDict (sourceStory.GetDataBase (), nextUID, UseDefaultIID());

    InterfacePtr tableModel (textStoryThreadDict, UseDefaultIID());

    If (tableModel) / / the storythreaddict represents a table

    {

    You are now in a table

    }

  • I can't clear the permission and get the following error message: failed to clear approval. Please try again after some time.

    I can't clear the permission and get the following error message:

    Cannot clear permission. Please try again after some time.

    I want to be able to allow so I can transfer files to my torchlight Nook more

    Macintosh:

    1. exit the Adobe Digital Editions software.
    2. Navigate to / Users / / Library/Application Support/Adobe/Digital Editions and drag the activation.dat file to the trash.
      If you use 10.7, see library access hidden files. Mac OS 10.7 and later.
    3. Open Adobe Digital Editions and reauthorize.

    Windows:

    1. Close all applications.
    2. Click Start > run.
    3. Open, type regedit in the text box and press ENTER. The Registry Editor opens.
    4. In the left pane of the registry editor, locate the following registry key: HKEY_CURRENT_USER\Software\Adobe\Adept
    5. Select the key of the follower.
    6. Choose file > export.
    7. In the export registry file dialog box, select the branch selected under export range. Enter a name and location for the backup registry key, and then click Save.
    8. Right click on the key to the follower, and then choose Remove.
    9. In the dialog box confirm the key deletion, click OK.
    10. Close the registry editor.
    11. Open Adobe Digital Editions and reauthorize.
  • Can not good press the mouse on the name of the file and get the option of e-mail

    Can not good press the mouse on the name of the file and get the option of e-mail

    Hello! If I understand your question, you want to right-click on a file and attach it in Mail. To do this, click on the file with two fingers on the touchpad, or control + click on the file, select 'Share', then 'mail '.

    I hope this helps!

  • I can't sync my phone to my computer and get the message that my phone doesn't have the latest version of Itunes. I downloaded the latest version on my mac, but what I do on my phone. I'm used to be able to synchronize the two.

    I am more able to sync my I phone on my Mac and get the message that my phone cannot be used because it requires a newer version of iTunes. He tells me to go to www.itunes.com to download the latest version of iTunes... I downloaded the latest version on my mac, but what I do on my phone? I used to be able to sync the two and now I can't even download the photos from my phone on my mac...

    iOS9 on a mobile device requires iTunes 12.3 or higher, which in turn requires a computer running OSX 10.8.5 or higher.  Update of the system only checks the updates for the current version of the system you run, but that itself can be updated.  It may or may not be possible to upgrade your computer to the system requirements. Find your computer on the web site of http://www.everymac.com model and near the bottom of the specification of the system section, he will tell what versions of the operating system, it is able to run. If you can not run a newer system, you will not be able to sync this phone to your current computer. If she can run 10.8.5 or higher, you can either buy a download for Apple OSX 10.8 Mountain Lion code online, or you can try to install the free El Capitan OSX 10.11.  El Capitan can run slower on older machines and require the additional purchase of RAM.  Making a big jump in versions of system is also more likely to affect the old software.

    At el capitan Snow Leopard, it will make my macbook is slow?  - https://discussions.apple.com/thread/7412959

    Mountain Lion 10.8 purchase link United States of America - http://www.apple.com/shop/product/D6377Z/A/os-x-mountain-lion

    Mountain Lion 10.8 purchase link U.K. - http://www.apple.com/uk/shop/product/D6377ZM/A/os-x-mountain-lion

    Course OSX Upgrade General information, including configuration required - http://www.apple.com/osx/how-to-upgrade/

  • photos has not been loaded for weeks (usually the browser chrome on PC windows at work).  I tried now on some other computers and get the same error message and I report every time.  can I do or just wait for someone to fix it

    photos has not been loaded for weeks (usually the browser chrome on PC windows at work).  I tried now on some other computers and get the same error message and I report every time.  is there anything I can do or just wait for someone to fix it?

    If you want any help here, you'll have to tell us what the error message.

    Which report you errors to?

  • How can I get the six digit code for my iPad Pro? I have two other iPads and an iPod but I enter the codes I get and the Pro is just off!

    How can I get the six digit code for my iPad Pro? I have two other iPads and an iPod but I get the codes I get and the Pro is just disabled for longer and longer whenever he rejects these codes!

    If you have forgotten the password for your iPhone, iPad or iPod touch, or your device is disabled - Apple supports

  • Today cell 9.7 Pro wifi iPad receipt. No SIM card installed. I guess I can go to the carrier and get the SIM on their part is there a reason I would need the Apple SIM card?

    I got my Pro iPad with wifi and cell phone. There was no SIM card. I'll go to the carrier and get the card SIM on their part is there a reason I need Apple SIM card?

    Apple SIM card to activate the iPad with more companies that I believe except Sprint - if you can get a SIM card from your service provider, then you would not fail the card SIM Apple

  • Outlook Express keeps trying to compact my messages. I'm unable to post messages and get the Error 0x800ccc0d code and I can not remove outlook Express

    Outlook Express keeps trying to shoot my messages several times per day.
    I'm unable to view messages in Outlook Express and get the code of Error 0x800ccc0d
    I'm unable to remove Outlook Express from my computer, I did not need it that I use Windows live mail.
    Any ideas on how to stop this nuisance.

    Windows Search is installed?

    You must tell Windows Search to stop indexing OE.

    In the control panel. Indexing Options. Change. Clear the check box for Outlook Express.

Maybe you are looking for

  • Cannot load MOV files within PDF documents in Firefox

    Tell me this has nothing to do with XP, but my problem report on the Firefox support site, you can find at the following link, https://support.mozilla.com/en-US/questions/831712#answer-199161, gave no solution, then maybe you can suggest a. And what

  • I get this error Message After you have uninstalled the HP software

    Recently, I uninstalled the HP 8600 Officejet Pro software from my computer because I have another printer that I use more regularly and I needed to make room on my SSD.  I have used HP for uninstalling do.  The problem is every time that I reboot, t

  • BlackBerry 10 data transfer

    How to transfer data (name and phone number) of curve 9300 curve 9320 p.s. both my phone will not connect to internet Thank you very much irlanri

  • Can't add that a network shared printer HP laserjet 4 p to my new Windows 7 Enterprise edition.

    When I ask W7 X 64 to search for printers on the network it detects the printer HP laserjet 4 p on my other XP pro machine, but then gives me an error message of not being able to find the driver file when I try to install. When I go to HP site, it i

  • How to uninstall kaspersky

    Im trying to upgrade to windows 7, kaspersky security 8 is not let me uninstall it, tells you that there need a password to be removed, which I don't know, like his is not compatible with 8, how do I install 8? Kind regards!