How to get the desired result when subsequent month is not available

WITH T1
     AS (SELECT 'A' COL1,
                'B' COL2,
                'C' COL3,
                '01-Jan-2015' DT,
                10 QTY
           FROM DUAL
         UNION
         SELECT 'A' COL1,
                'B' COL2,
                'C' COL3,
                '01-feb-2015' DT,
                20 QTY
           FROM DUAL
         UNION
         SELECT 'A' COL1,
                'B' COL2,
                'C' COL3,
                '01-mar-2015' DT,
                30 QTY
           FROM DUAL
         UNION
         SELECT 'A' COL1,
                'B' COL2,
                'C' COL3,
                '01-may-2015' DT,
                40 QTY
           FROM DUAL
         UNION
         SELECT 'A1' COL1,
                'B1' COL2,
                'C1' COL3,
                '01-mar-2015' DT,
                40 QTY
           FROM DUAL
         UNION
         SELECT 'A1' COL1,
                'B1' COL2,
                'C1' COL3,
                '01-may-2015' DT,
                40 QTY
           FROM DUAL)
SELECT *
  FROM t1

Current output:

COL1COL2COL3DTQTY.
ABCJanuary 1, 201510
ABCFebruary 1, 201520
ABC01-mar-201530
ABC01-may-201540
A1B1C101-mar-201540
A1B1C101-may-201540

Expected results

COL1COL2COL3DTQTY.DTREQ_VAL
ABC01/01/20151001/01/201520
ABC01/02/20152001/02/201530
ABC01/03/20153001/03/20150
ABC01/05/20154001/05/20150
A1B1C101/03/20154001/03/20150
A1B1C101/05/20154001/05/20150

Logic:

If we COL1, COL2, COL3 combination for A, B, C, we have the quantity for 1 January, 1 February, 1 March, 1 may, and we don't have quantity for April 1 and June 1. I need following QUANTITY value as another column. If I use the lead, I'll get the value from May 1st to March 1st, but I want to value 0 because it is after March either.

What I've tried so far:

SELECT *
  FROM (WITH T1
AS (SELECT 'A' COL1,
'B' COL2,
'C' COL3,
'01-Jan-2015' DT,
10 QTY
FROM DUAL
UNION
SELECT 'A' COL1,
'B' COL2,
'C' COL3,
'01-feb-2015' DT,
20 QTY
FROM DUAL
UNION
SELECT 'A' COL1,
'B' COL2,
'C' COL3,
'01-mar-2015' DT,
30 QTY
FROM DUAL
UNION
SELECT 'A' COL1,
'B' COL2,
'C' COL3,
'01-may-2015' DT,
40 QTY
FROM DUAL)
        SELECT COL1,
 COL2,
 COL3,
TO_DATE (T1.DT) DT,
NVL (QTY, 0) QTY,
 MONTHS.DT ALL_DATES,
 LEAD (NVL (QTY, 0), 1, 0) OVER (ORDER BY MONTHS.DT) REQ_VAL
          FROM T1,
(    SELECT ADD_MONTHS (TO_DATE ('01-jun-2015'), LEVEL - 6) DT
FROM DUAL
CONNECT BY LEVEL <= 12) MONTHS
         WHERE MONTHS.DT = T1.DT(+))
WHERE DT ISNOTNULL;





My query works for a set of COL1, COL2, COL3, am looking how we can achieve multiple COL1, COL2, COL3 (or) otherwise better query writing.

Thanks for your suggestions in advance.

Thank you

Now, to understand your condition: your additional column must indicate the amount of the next month, or zero if it is not a record for the month. How about this?

WITH T1

AS (SELECT 'A' COL1,)

'B' COL2,

COL3 'C ',.

To_date('01-Jan-2015','dd-mon-yyyy') DT,

QTY 10

OF THE DOUBLE

UNION

SELECT 'A' COL1,

'B' COL2,

COL3 'C ',.

To_date('01-Feb-2015','dd-mon-yyyy') DT,

QTY. 20

OF THE DOUBLE

UNION

SELECT 'A' COL1,

'B' COL2,

COL3 'C ',.

To_date('01-Mar-2015','dd-mon-yyyy') DT,

QTY 30

OF THE DOUBLE

UNION

SELECT 'A' COL1,

'B' COL2,

COL3 'C ',.

To_date('01-May-2015','dd-mon-yyyy') DT,

QUANTITY 40

OF THE DOUBLE

UNION

SELECT 'A1' COL1,

COL2 "B1."

COL3 "C1"

To_date('01-Mar-2015','dd-mon-yyyy') DT,

QUANTITY 40

OF THE DOUBLE

UNION

SELECT 'A1' COL1,

COL2 "B1."

COL3 "C1"

To_date('01-May-2015','dd-mon-yyyy') DT,

QUANTITY 40

THE DOUBLE)

X.Col1, x.col2, x.col3, x.dt, x.qty, NVL(y.qty,0) SELECT req_val

THE t1 x

LEFT OUTER JOIN t1 y

ON y.col1 = x.col1

AND y.col2 = x.col2

AND y.col3 = x.col3

AND y.dt = add_months(x.dt,1)

ORDER BY 1,2,3,4

My query works for a DFU, am looking how we can achieve multiple UTD

What is a DFU?

Tags: Database

Similar Questions

  • How to get the desired result

    I have an accmaster say table where each record has detailts on an acct as actno, curr_bal, branch, acct_type I want something as below a
    branch_no - Sum (curr_bal) where acct_type like 1% ' as sb - sum (curr_bal) where acct_type like 2%'s fd of the Group table by branch in a single line as shown below

    00001 550000 65000000
    00002 75909000 2568229867

    Please tell how to do the above operation.

    Hello

    This is called a Pivot , and here's a way to do it:

    SELECT       branch_no
    ,       SUM (CASE WHEN acct_type LIKE '1%' THEN curr_bal END)     AS total_1
    ,       SUM (CASE WHEN acct_type LIKE '2%' THEN curr_bal END)     AS total_2
    FROM       accmaster
    GROUP BY  branch_no
    ;
    

    This will work in any version of Oracle, from 8.1, but starting in Oracle 11.1, you can also use the SELECT... Function PIVOT.

    I hope that answers your question.
    If not, post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all tables and also post the results desired from these data.
    Explain, using specific examples, how you get these results from these data.
    Always tell what version of Oracle you are using.

    Furthermore, your table is called
    accmaster (where, I guess, ACC means 'account') and it contains called columns
    ACTNO ( Act means 'account') and
    acct_type ( acct means 'account')
    Do you really need 3 different ways to shorten "account"? How do you recall when you used a way and when you have used another or when you used an underscore after the abbreviation character, and when you do not have? Even if you never get confused by these things, someone trying to help you, and one that should keep your code in the future, will probably. Do not use consistent, such as namespace
    acct_master
    Acct_No and
    acct_type
    ?

  • How to get the desired result of the post

    Hi all.
    I use APEX 4.0.
    Now, I have two pages: A, B.
    Transfer the student_id from page A to page B.
    There is an article questioned B: student_name page select the CT where student_id =: student_id;
    At the same time, I want to get the name of the student with the title of the region, I handle it like this: student name: & Student_Name. (with the '.') ;
    If the bases of the student_name on column DB, the title work. After I have change student_name based on the query, it does not work.

    How can I solve it. Thank you.

    Hi Andy,.

    It depends on If the value is defined in this field. If you set the source of the element, it is probably too late for value must be used in the title of the region.

    In general, I find that it is better to create a calculation that is running "Before Header" to set values in the page that I need to use when the page is loaded.

    Andy

  • How to get the second Monday of each month in a given date range?

    In Oracle forms, how to get the second Monday of each month in a given date range?

    I tried below using the query WITH the Clause, but it seems that WITH Clause does not work in Oracle forms. So is there another way to do this in Oracle forms?

    WITH month_range AS

    (

    SELECT TO_DATE ('Dec 2013', 'Mon YYYY') AS first_month

    , TO_DATE ('Mar 2014', 'Mon YYYY') AS last_month

    OF the double

    )

    SELECT NEXT_DAY (6 + ADD_MONTHS (first_month

    , LEVEL - 1

    )

    , 'MONDAY '.

    ) AS second_monday

    OF month_range

    CONNECTION OF LEVEL < = 1 + MONTHS_BETWEEN (last_month, first_month)

    ;

    Thanks in advance.

    Good fishing, when the first day of the month is Thursday... So I changed the query accordingly... Try the below

    SELECT CASE WHEN TO_CHAR (ADD_MONTHS (TRUNC(startdate,'MM'),(LEVEL-1)), 'DY') = 'game '.

    THEN NEXT_DAY (ADD_MONTHS (TRUNC(startdate,'MM'),(LEVEL-1)), 'THU')

    Of OTHER NEXT_DAY (ADD_MONTHS (TRUNC(startdate,'MM'),(LEVEL-1)), 'Game') + 7

    END AS second_day

    FROM (SELECT SYSDATE startdate,

    SYSDATE + 300 enddate

    THE DOUBLE)

    CONNECT BY LEVEL<=>

  • How to get the desired TableCell element of TableView?

    Hello
    I implement an editable table view.
    I put the cell factory to generate editable cells in each column. Double click on any cell, the 'startEdit()' of my Tablecell method is called and a textfield appears.
    My current role is on the text box TAB, the next cell in the column of the row must be in editable form. (that is to say I have to call the method startEdit() of the next collection of the TableCell element).

    But I don't get how to get the element of the next column TableCell object.

    Can someone help me get this behavior. ? If there is also another way to get this feature please let me know. ?

    Here is the code I am trying to get with on the text field.
    textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
                @Override public void handle(KeyEvent t) {
                    if (t.getCode() == KeyCode.TAB){
                         System.out.println("Focus on next column....");
                         //getTableRow().getAlignment();
                         //getIndex();
                         //getTableView().getItems().get(getTableRow().getIndex());
                         //TableCell cell = (TableCell)getTableRow().getChildrenUnmodifiable().get(1);
                         
                    }
                }
            });
    Thanks in advance.
    SAI

    Maybe, I'm not entirely understand your question, but could you not call tableView.edit (line, column) of your article and have this as the StartEdit method call for you?

    -Jonathan

  • How to get the last day of a month for every 2 months for a given period?

    Hello

    Can is it some please let me know how to get the last day, last day of the week, the weekend last day, last Monday, one month for every 2 months for a given period?

    Thanks in advance.

    Try the below

    SELECT LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2))) lastday.

    BOX WHEN TO_CHAR (LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2))), 'DY') = 'SAT '.

    SO LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2)))-1

    WHERE TO_CHAR (LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2))), 'DY') = 'Sun '.

    THEN LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2)))-2

    Of OTHER LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2)))

    END as lastweekday,

    BOX WHEN TO_CHAR (LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2))), 'DY') IN ('Sam', 'SUN')

    THEN LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2)))

    Of OTHER LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2)))

    -(TO_NUMBER (TO_CHAR (LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2))),' from)) - 1).

    END as lastweekendday,

    BOX WHEN TO_CHAR (LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2))), 'DY') = 'MY

    THEN LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2)))

    Of OTHER NEXT_DAY (LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2))), "LUN")-7

    END AS lastmonday

    FROM (SELECT SYSDATE startdate,

    SYSDATE + 300 enddate

    THE DOUBLE)

    CONNECT BY LEVEL<=>

  • Failed to get the desired result

    Hi gurus

    Don't know why I get the following output:

    Query

    SOME jobs,

    COUNT (*),

    COUNT (CASE job WHEN 'CLERK' THEN 1 ELSE 0 END) cnt

    WCP

    Working GROUP;

    The query result

    WORK, COUNT (*), CNT

    3 3 CLERKS

    4 4 SELLER

    THE PRESIDENT 1 1

    MANAGER 3 3

    1 1 ANALYST

    According to my understanding, the result should be as below:

    Result must be

    WORK, COUNT (*), CNT

    3 3 CLERKS

    SELLER 4 0

    THE PRESIDENT 1 0

    MANAGER 3 0

    ANALYST 1 0

    Thanks in advance.

    Concerning

    Shu

    Hello

    Shuumail wrote:

    Hi gurus

    Don't know why I get the following output:

    Query

    SOME jobs,

    COUNT (*),

    COUNT (CASE job WHEN 'CLERK' THEN 1 ELSE 0 END) cnt

    WCP

    Working GROUP;

    The query result

    WORK, COUNT (*), CNT

    3 3 CLERKS

    4 4 SELLER

    THE PRESIDENT 1 1

    MANAGER 3 3

    1 1 ANALYST

    According to my understanding, the result should be as below:

    Result must be

    WORK, COUNT (*), CNT

    3 3 CLERKS

    SELLER 4 0

    THE PRESIDENT 1 0

    MANAGER 3 0

    ANALYST 1 0

    Thanks in advance.

    Concerning

    Shu

    COUNT (*) tells you how many lines there are.

    COUNT (exp) tells you how many lines have a value not NULL in exp

    Your CASE expression never returns null; It always returns a value of 0 or 1, but never NULL.  So, if exp is never NULL, so each line has a value not NULL and if COUNT (*) and COUNTY (exp) are always the same.

  • How to get the checkbox value when value list changed in the classic report

    Hello
    I worked with apex 4.2 and I create normal classic report with a checkbox column and a single column change to select list (named loved) now, when I want change to user list
    take the value of the checkbox element and display it in the message.

    SQL for the report

    {
    SELECT
    "" "< INPUT TYPE ="checkbox"NAME ="f01"VALUE =" "
    || SEQ
    |'">"SEQ, "
    ID,
    DEPT_NO,
    EMP_NAME} I change the column attributes of Dept_NO to display as the selection of the name of the Department (named lov) list.

    now, I want when change of user name of the Department, the value of the MESSAGE ALERT SHOW IN SEQ

    I create JavaScript on the page

    function test (pThis) {}
    var f01_value = $('select[name="f01"]').value;

    Alert ("#SEQ:" + f01_value);

    }
    < /script >
    I call this javascript function when the change in the list, but the value undefined...

    My Question:

    How can get this value or any value of the point in reports

    concerning
    Ahmed

    Hello
    Check your page now.

    As mentioned earlier, you must use APEX_ITEM. CHECKBOX2.

    APEX_ITEM.CHECKBOX2(2,EMP.EMPNO,null,null,null,'f02_'||lpad(ROWNUM,4,'0')) NO,
    

    And the javascript function will be

    
    

    You cannot use $v to get the value of the checkbox element such that it works differently for the boxes, see http://docs.oracle.com/cd/E37097_01/doc/doc.42/e35127/javascript_api.htm #BGBGDGIH.
    Therefore, you use the jQuery val().

    See you soon,.

  • How to get the overall result sequence into LabView?

    Hello

    Could someone can help with this little problem?

    We have a batch (.bat) that launches a list given TestStand sequences, which works as expected.

    However, we monitor the results of each sequence and display it to the operator.

    Our idea is to get the result of the sequence in a VI, executed at the end of the sequence and add it to a queue, which will be extracted and displayed on a monitor of VI, executed separately.

    The only problem we have left must be able to recover the overall result of the sequence. Any idea or suggestion?

    Thank you

    Raphael

    Thanks for all your replies. We finally finished queues sequencefailed node sequence context property value.

    I also discovered the TestStand database logging and began to use it in parallel.

    Thanks again

  • How to get the total reserved cpu or total cpu capacity available with PowerCLI?

    I am able to use the cmdlet Get-Host for a total capcity of CPU and the current CPU usage.  However, I am not able to get the total of reserved CPU.  I also tried another cmdlet like Get-VMHostAdvancedConfiguration, but which does not include either total reserved cpu info.  Any suggestion?

    OK, try this

     foreach($esx in Get-VMHost){
       $parent = Get-View $esx.ExtensionData.Parent   $rp = Get-View $parent.ResourcePool   Select -InputObject $esx -Property Name,      @{N="Total CPU Capacity MHz";E={$rp.Runtime.Cpu.MaxUsage}},      @{N="Reserved CPU Capacity MHz";E={$rp.Runtime.Cpu.ReservationUsed}},      @{N="Available CPU Capacity MHz";E={$rp.Runtime.Cpu.MaxUsage - $rp.Runtime.Cpu.ReservationUsed}}
    }
    
  • Get the GPS location API error - provider is not available

    Hello

    I am trying to access the information of location using Android location API (via LocationManager.GPS_PROVIDER), but without success. I get error "provider not available."

    AndroidManifest.xml has lines related to the relevant permissions:


    But I do not get prompt the same security to grant these permissions to my request.

    OS version 2.0.0.3894

    Build 73070

    Thank you.

    I solved this problem by restarting and place the device near a window.

    What I was wondering now is why Android player never asked security permission to access the location of the GPS. What is a function of development?

  • How to get the same result on different characterset?

    Hello experts,

    I use

    Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
    PL/SQL Release 11.2.0.3.0 - Production
    "CORE 11.2.0.3.0 Production"
    TNS for 64-bit Windows: Version 11.2.0.3.0 - Production
    NLSRTL Version 11.2.0.3.0 - Production
    
    

    Needing a result even on my example.

    SELECT ASCII('ˆ') -- when characterset is -NLS_CHARACTERSET WE8MSWIN1252
    FROM DUAL;
    --result 136
    
    

    SELECT ASCII('ˆ') -- when characterset is -NLS_CHARACTERSET AL32UTF8
    FROM DUAL;
    --result 52102 but need 136
    
    

    I need same result.

    Thanks for your time...

    EDITED RESULT.

    Ask2Learn

    You test something that you call "encrypted" data, which are not at all encryption.

    For example speaking of letter t.

    The CHR (ASCII('t') + 20) operation is pretty useless. You try to find the character that has the code binary and longer than 20 that the letter 't'. This can work with single-byte as WE8MSWIN1252 character sets, but it does not work with variable width as AL32UTF8 character sets. The letter 't' a code 0 x 74 in WE8MSWIN1252 and AL32UTF8. If you add 20 (decimal), you get 0 x 88. Byte 0 x 88 is a valid code in the WE8MSWIN1252, but is not a valid code in AL32UTF8. Bytes in this range may bytes of continuation in AL32UTF8, i.e. a second, third or the fourth byte of a multi-byte sequence.

    The "circumflex accent" character code 0 x 88 (in WE8MSWIN1252) has the code of two bytes 0xCB 0 x 86 in AL32UTF8.

    So, why not take a look at DBMS_CRYPTO integrated


    Hope this helps


    Hamid

  • How to get the positive result of the search text page number?

    How can I access the results of search text of javascript page number... (using the batch sequence)

    search.matchCase = false;

    search.wordMatching = "MatchPhrase;

    Search.Bookmarks = true;

    Search.Query ("whatIMSearching", "ActiveDoc");

    ....

    get page numbers,

    then do something like the export of pages that have this text in their...

    Use the javascript method getPageNthWord.

  • SQL query to get the desired result

    I have the array with the following values

    Account Functional currency Billing currency Amount Header 5
    101USDUSD10 h 00
    101USDCAD12 h 00
    102JPYUSD9 h 00
    102JPYCAD3.00
    102JPYSGD2.00

    If the account has one different inv_currencypar that the functional currency and then

    I have to summarize the amount in this account by viewing the billing currency

    If the account has more than one other inv_currencypar that the functional currency and then

    I have to summarize the amount in this account showing the functional currency as the currency of invoicing

    I need output like this:

    Account

    Functional currency Billing currency Total amount Header 5101USDCAD10:00 pm102JPYJPY2:00 pm

    Can we get over output in sql without writing pl/sql program?

    Hello

    According to your specific needs, here's a way:

    SELECT account

    functional_currency

    CASE

    WHEN 1 = COUNT (DISTINCT NULLIF (invoice_currency

    functional_currency

    )

    )

    THEN MAX (NULLIF (invoice_currency

    functional_currency

    )

    )

    Of OTHER functional_currency

    END AS invoice_currency

    The SUM of (amount) AS total_amount

    header5

    FROM table_x

    GROUP BY account

    functional_currency

    header5

    ;

    If you would care to post CREATE TABLE and INSERT statements for your sample data, and then I could test this.

    Is it possible to have 2 (or more) of different functional_currencies in the same account?

  • How to get the confirmation message when I press the button Delete?

    Hai,
    I want to display a confirmation popup before you delete the line.

    I created an Entiity object and a view to EmpTable... object and drag and drop the button Delete.
    I want to show a confirmation message that "you want to remove?
    If the user clicks OK, the data must be deleted... or not... I'm using JDeveloper 11 g

    You can use a popup for this. Put a component showpopuupbehavior on the delete button to display a popup.
    Article 13 of the web developer guide here shows how proceed.

    Timo

Maybe you are looking for

  • Carpet * a DVD-Ram UJ - 841S comes with several error messages

    My laptop is relatively new, I've been burning some video files in DVD - RW and all of a sudden my DVD drive has stopped working. It came with several error messages format "failed due to damage media", "format failed due to general failure" and "the

  • Satellite L20 error: DVD - RAM crashes with a blue screen with the use of cd-audio

    Hello world I have a very annoying problem: when I use audio-cd, my Satellite tends to crash with a blue screen. When it crashes, blue screen, scrolls and immediately a black screen and a restart follows. It is probably a driver problem, because I ha

  • TouchSmart Netflix connection does not connect

    I have a problem where the Netflix to run the application through the application of HP touchsmart will record not in.  If I go to a browser and get on Netflix from this location, it works fine.  I called Netflix and we set up a new password but also

  • HP 250 g2 i3 wi fi drivers problems

    Hello I tried all of these operating systems: windows 7 ultimate 32/64 bit and windows 7 pro 64-bit I am installing a Wi fi drivers for this laptop, but all the time, it gives me this error: -This program may not be installed properly -Reinstall usin

  • I have a problem with the installation of the printer driver.

    It is the only computer to windows 7 in the Office I can't install driver HP T920 on. I get an error message. Windows 7 SP1 64 bit Microtour HP 505 b AMD Athlon II X 3 440 processor 3.0 GHz 4 GB OF RAM