How to compare 2 rows


Hello

I work to find our sales trend, below is a sample of my data grouped by week

Dateweekly sales
15 June 13725
22 June 131248
29 June 131212
July 6, 131266
13 July 131240
20 July 131404
27 July 131380

the result should be as below, in excel I can do in + trend = IF (C3 - C2 > = 0, C3 - C2, 0) and - trend = IF (C3 - C2 < 0, C3 - C2, 0) (the first column of trend should be 0)

DateWeekly sales+ Trend-Trend
15 June 1372500
22 June 13124800
29 June 1312120-36
July 6, 131266540
13 July 1312400-26
20 July 1314041640
27 July 1313800-24

but of course I have to make it automatic to send it directly to a report.,.

can you please help me with this.

Looks like a job for the analytic lag function.  Something like:

SQL > with sample_data like)
2 Select to_date (June 15, 2013 ', 'mon-dd-yyyy') week_date, 725 amount of all the double union
3 select to_date (June 22, 2013 ',' mon-dd-yyyy "") 1248 Union double all the
4 Select to_date (29 June 2013 ',' mon-dd-yyyy ""), 1212 union double all the
5 Select to_date (' 6 July 2013', ' mon-dd-yyyy ""), 1266 union double all the
6 select to_date (July 13, 2013 ',' mon-dd-yyyy ""), 1240 union double all the
7 select to_date (20 July 2013 ',' mon-dd-yyyy ""), 1404 union double all the
8 select to_date (July 27, 2013 ',' mon-dd-yyyy ""), the double 1380)
9. Select week_date,
10 cash when amt_diff > 0 then amt_diff end pos_trend, otherwise 0
11 case when amt_diff< 0="" then="" amt_diff="" else="" 0="" end="">
12 years of (select week_date,
13 amount - lag (amount) on amt_diff (week_date control)
14 of the sample_data)
15 order by 1;

WEEK_DATE POS_TREND NEG_TREND
----------- ---------- ----------
JUNE 15, 2013 0 0
JUNE 22, 2013 523 0
JUNE 29, 2013 - 0 36
JULY 6, 2013 54 0
JULY 13, 2013-26 0


20 JULY 2013 164 0
JULY 27, 2013 0-24

John

Tags: Database

Similar Questions

  • How to compare the new values with the old values in triggers.

    Dear all,

    Please tell me how to compare the new values with the old values in triggers.

    Hi, the employee example is in the document. You'd better read yourself.

    CREATE OR REPLACE TRIGGER Print_salary_changes
      BEFORE DELETE OR INSERT OR UPDATE ON Emp_tab
      FOR EACH ROW
    WHEN (new.Empno > 0)
    DECLARE
        sal_diff number;
    BEGIN
        sal_diff  := :new.sal  - :old.sal;
        dbms_output.put('Old salary: ' || :old.sal);
        dbms_output.put('  New salary: ' || :new.sal);
        dbms_output.put_line('  Difference ' || sal_diff);
    END;
    /
    
  • How to compare two lines in PL/SQL?

    Hi all

    How to compare two lines in PL/SQL? Is there a method I can use to compare their column by column instead?

    Any comments would be much appreciated.

    PhoenixBai wrote:
    By lines I mean, two rows of the same table!

    Ah, finally, we get a bit more useful information

    and I need to compare the column by column to see if there is a difference between these two rows.

    Like this, you mean?

    SQL> ed
    Wrote file afiedt.buf
    
      1  with data as (select 1 as id, 'A' as dta, 'B' as dta2 from dual union all
      2                select 2, 'A', 'B' from dual union all
      3                select 3, 'B', 'C' from dual)
      4  --
      5      ,chk as (select &id1 as id1, &id2 as id2 from dual)
      6  --
      7  select case when (
      8    select count(*)
      9    from (
     10      select dta, dta2
     11      from data, chk
     12      where id = id1
     13      minus
     14      select dta, dta2
     15      from data, chk
     16      where id = id2
     17      )
     18    ) = 0 then 'No Difference'
     19          else 'Difference'
     20          end as check_result
     21* from dual
    SQL> /
    Enter value for id1: 1
    Enter value for id2: 2
    old   5:     ,chk as (select &id1 as id1, &id2 as id2 from dual)
    new   5:     ,chk as (select 1 as id1, 2 as id2 from dual)
    
    CHECK_RESULT
    -------------
    No Difference
    
    SQL> /
    Enter value for id1: 1
    Enter value for id2: 3
    old   5:     ,chk as (select &id1 as id1, &id2 as id2 from dual)
    new   5:     ,chk as (select 1 as id1, 3 as id2 from dual)
    
    CHECK_RESULT
    -------------
    Difference
    
    SQL>
    

    My only concern is the result of this sql. East - reliable? It can really be used to differentiate the two lines? I mean, the result would be the same, as I do with Java?

    Not sure what you mean by "reliable"? SQL is not some kind of generator of random result (by derogation from the use of the DBMS_RANDOM package). If you ask him if 1 + 1 = 2, then it will always give you the right answer.

  • How to compare the 2 versions of the same document in Pages?

    How to compare the 2 versions of the same document on Pages?

    There is no OS X Visual tool which can open two Pages document and show the differences between them - other than the human eye.

    What information are you interested in identify as different between the two documents? What specific version of Pages?

  • How to compare table 2D lines?

    Hello

    I have a 2D binary table.

    -I want to compare lines each against the other.

    -Two lines should be compared if they have only a different element.

    -The result of the comparison must be a line with the other element assigned as 2 (or a different number of 0, 1).

    -Continue to compare result rows against each other.

    -Repeat until the no line cannot be compare (these rows must be submitted).

    Any ideas will be appreciated!


  • How to compare the original value of table size and the changed value

    juice I took a table and then took the function of the size of the array so that it shows me the number of the elements present in it. so it'll be the original table size value. If the items in the table even changes another value, then I want to compare the original table size value and the value of table size has changed. How to compare... Please help me. you are looking for a possible solution. Thank you

    Hi stara,

    the attached picture shows the ony solution.

    It will be useful.

    Mike

  • How to compare the content of two Wordpad / documents notebook without reading them line by line?

    How to compare the content of two Wordpad / documents notebook without reading them line by line?

    Hello

    Without this feature is included in the operating system.

    However, you can use your favorite search engine to look for software that needs to perform these tasks.

    WARNING of THIRD PARTY SOFTWARE: Using third-party software, including hardware drivers can cause serious problems that may prevent your computer from starting properly. Microsoft cannot guarantee that problems resulting from the use of third-party software can be solved. Software using third party is at your own risk.

    Hope this information helps.

  • How to change the row height in tableLayoutManager?

    HIII all,.

    I used TableLayoutManager in my application but don't know how to increase the row height.

    I use this reference for the TableLayoutManager link.

    How - to create a presentation of the rich UI at TableLayoutManager
    Article number: DB-00783

    http://www.BlackBerry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800505/800508/...

    and I want to make the size of the entire line 68 px...

    any body can help me how to change the RowHeight in tableFieldManager?

    @peter

    Thank you Peter for your answer.

    I solved the problem as you indicate in the Tablelayoutmanager.java change before 5 months.

    I asked you our post reply you as soon as possible...

    I don't care

    Thank you.

    regaurds,

    Albert_anks

  • How to compare two dates and find exactly

    How to compare two dates and find the exact age of the person, no one could be an age of child 2 days or a month, or other.

    I'd really appreciate if someone help o

    Concerning

    After spending 2 hours, I go out with a solution by myself, how ever the function can be customize to check if the user enters date right.

    function findAge(subjectName,fromdate, todate) {
        console.log("findAge(fromdate, todate) is called now "+subjectName+"-->"+fromdate+"-->"+todate);
        if(todate) todate= new Date(todate);
        else todate= new Date();
    
        var age= [], fromdate= new Date(fromdate),
        y= [todate.getFullYear(), fromdate.getFullYear()],
        ydiff= y[0]-y[1],
        m= [todate.getMonth(), fromdate.getMonth()],
        mdiff= m[0]-m[1],
        d= [todate.getDate(), fromdate.getDate()],
        ddiff= d[0]-d[1];
    
        if(mdiff < 0 || (mdiff=== 0 && ddiff<0))--ydiff;
        if(mdiff<0) mdiff+= 11;
        if(ddiff<0){
            fromdate.setMonth(m[1]+1, 0);
            ddiff= fromdate.getDate()-d[1]+d[0];
            --mdiff;
        }
        if(ydiff> 0) age.push(ydiff+ ' year'+(ydiff> 1? 's ':' '));
        if(mdiff> 0) age.push(mdiff+ ' month'+(mdiff> 1? 's':''));
        if(ddiff> 0) age.push(ddiff+ ' day'+(ddiff> 1? 's':''));
        if(age.length>1) age.splice(age.length-1,0,' and ');
        console.log("===============================");
        console.log("Subject age is = "+age.join(''));
        console.log(" age Day = "+ddiff);
        console.log(" age Month = "+mdiff);
        console.log(" age Year = "+ydiff);
        console.log("===============================");
        var subjectAGE =  age.join('');
    
    }
    

    peardox Thanks for the reply

  • How to compare installed Windows with a disk

    How to compare Windows with a DVD disc and product installed?

    Me once again.
    Just read the error 0xC004F063 code indicates that the license service is not in the BIOS of the computer, the required license. This occurs when the parts have been replaced. Here, you should try an activation of the phone.

    See you soon
    Julia

  • HOW TO COMPARE THE REGULAR TEACHER VS VERSIONS?

    HOW TO COMPARE THE REGULAR TEACHER VS VERSIONS?

    Thank you

    An educational version of Photoshop Elements are totally same (feature wise) than the regular commercial versions. So, it is unnecessary to compare.

  • How to compare documents in pdf format

    How to compare documents in pdf format

    Hey deepakk40834887,

    It is not possible to compare PDF files using the player.

    You may need to use Acrobat for the same thing.

    Reference: Acrobat Help. Compare two versions of a PDF file

    Hope that helps.

    Kind regards

    Ana Maria

  • How to compare premises, iSCSI and storage NFS for VMware ESXi 5 with IO Analyzer?

    Hello

    I have 2 hosts ESXi 5.0 with a few virtual machines running. I would compare the IO throughput for my local storage, iSCSI and NFS vmware storage store. Can someone point me to the right direction? I read the documents and tutorials, but I'm not sure how to compare tests from these 3 options.

    I have a need to compare the following storage options.

    Local disk on the ESX host

    iSCSI on QNAP NAS device

    iSCSI on EMC e 3100

    NFS on QNAP NAS device

    NFS on EMC e 3100

    IOmeter seems to be good tool based on reading, but still not clear where to make changes to pass tests of storage to another.

    Thanks in advance,

    Sam

    If you use IO monitor, then you must simply vmotion the VMS to the data store for the device you want to test, and then run the test (and Records results).  Can vmotion for the following data store and repeat.

  • How to compare two files to see the differences in the content?

    How to compare two files (one word and a single pdf) to see if the files are identical without having read all 7 pages?

    Thank you

    You cannot compare files of different nature. If you convert the Word file to PDF, you can then compare the two files in Acrobat.

  • How to compare two users (2)

    Hi all

    11.2.0.1

    AIX 6.1

    I have two databases (dev & uat) on separate servers with equal facility.

    On each database, I created a schema user "batchdba" and gave them same privilege "dba".

    But when I import (impdp) the dumpfile output from other databases that encrypted table and with the same command, it runs successfully on DEV but not on the UAT.

    How can I compare that privilege is missing from the UAT for the user "batchdba". Or how to compare the privileges granted to these two patterns?

    Thank you very much

    zxy

    You can query the following views on the databases to compare the result

    dba_tab_privs

    dba_role_privs

    dba_sys_privs

    query these tables with the status RECIPIENT = "BATHCDBA."

Maybe you are looking for

  • How can I send messages Windows Live through my MobileMe account?

    Original title: Windows Live Mail and mobileme I have successfully added my mobileme account to my Windows Live Mail program, but I can't send emails via the account.  I can receive them and I watched all my settings; I just can't understand it.  Can

  • Reset my password without the alternative email

    I have reset my password on windows live but the alternative e-mail in inactive... How can I get the link to reset my password? I don't remember the securtity question as well... what I need to do to get a new password? Thank you

  • What is a good program to recover deleted photos?

    I accidentally deleted some photos, and I found the programs listed online in order to recover erased images. Any suggestions on a good safe program to download and use? Prefer free.

  • Missing DLL files while trying to load a game

    Original title: missing DLL I bought a game for my daughter who said it is compatible to Windows XP. However, when I try to charge it I am constantly be bombarded with messages stating that files such as Wing32.dll, wing de.dll and wingdib.drv cannot

  • Required USB Hub recommendation

    I just got a new computer desk and its configuration makes me need a USB 4-port hub, I think. All the products HP (pavilion slimline, photosmart 8250 & 4480 deskjet printers). My printer photo, the keyboard and the mouse are unable to reach USB ports