Need help in SQL , Subtract column based on the condition in the Row

Hi all ,
I’m new to Sql Queries; I have a requirement which involves Sql query,
Can anyone tell the How to proceed or provide me Sql
Here goes the requirement
Current Table
ID |-|Month|-|Data
1|-|Jan-10|-|10.00
2|-|Feb-10|-|20.00
3|-|Mar-10|-|30.00
4|-|Apr-10|-|50.00
5|-|May-10|-|60.00
6|-|Jun-10|-|70.00
7|-|Jul-10|-|100.00
8|-|Aug-10|-|110.00
9|-|Sep-10|-|120.00
10|-|Oct-10|-|140.00
11|-|Nov-10|-|150.00
12|-|Dec-10|-|170.00
13|-|Jan-09|-|180.67
14|-|Feb-09|-|194.67
15|-|Mar-09|-|208.67
16|-|Apr-09|-|222.67
Format needed
ID |-|Month|-|Data|-|Base on this condition
1|-|Jan-10|-|10.00|-|Jan 10
2|-|Feb-10|-|10.00|-|Feb10-Jan10
3|-|Mar-10|-|10.00|-|Mar10 - Feb10
4|-|Apr-10|-|20.00|-|Apr10 - Mar10
5|-|May-10|-|10.00|-|May10 - Apr10
6|-|Jun-10|-|10.00|-|Jun10 - May10
7|-|Jul-10|-|30.00|-|Jul10 - Aug10
8|-|Aug-10|-|10.00|-|Aug10 - Jul10
9|-|Sep-10|-|10.00|-|Sep10 -Aug10
10|-|Oct-10|-|20.00|-|Oct10 -Sep10
11|-|Nov-10|-|10.00|-|Nov10 - Oct10
12|-|Dec-10|-|20.00|-|Dec10 - Nov10
13|-|Jan-09|-|10.67|-|Jan 09
14|-|Feb-09|-|14.00|-|Feb09-Jan09
15|-|Mar-09|-|14.00|-|Mar09 - Feb09
16|-|Apr-09|-|14.00|-|Apr09 - Mar09
Thanks in advance
Astle

Welcome to the forum!
this?
select id
     , mon
     , data
     , data - lag (data, 1, 0) over (partition by trunc (mon, 'yyyy')
               order by mon)
  from test
order by idas in
SQL> with test as
  2  (
  3  select 1 id ,to_date ('Jan-10', 'Mon-rr') mon,10.00 data from dual union all
  4  select 2,to_date ('Feb-10', 'Mon-rr'),20.00 data from dual union all
  5  select 3,to_date ('Mar-10', 'Mon-rr'),30.00 data from dual union all
  6  select 4,to_date ('Apr-10', 'Mon-rr'),50.00 data from dual union all
  7  select 5,to_date ('May-10', 'Mon-rr'),60.00 data from dual union all
  8  select 6,to_date ('Jun-10', 'Mon-rr'),70.00 data from dual union all
  9  select 7,to_date ('Jul-10', 'Mon-rr'),100.00 data from dual union all
10  select 8,to_date ('Aug-10', 'Mon-rr'),110.00 data from dual union all
11  select 9,to_date ('Sep-10', 'Mon-rr'),120.00 data from dual union all
12  select 10,to_date ('Oct-10', 'Mon-rr'),140.00 data from dual union all
13  select 11,to_date ('Nov-10', 'Mon-rr'),150.00 data from dual union all
14  select 12,to_date ('Dec-10', 'Mon-rr'),170.00 data from dual union all
15  select 13,to_date ('Jan-09', 'Mon-rr'),180.67 data from dual union all
16  select 14,to_date ('Feb-09', 'Mon-rr'),194.67 data from dual union all
17  select 15,to_date ('Mar-09', 'Mon-rr'),208.67 data from dual union all
18  select 16,to_date ('Apr-09', 'Mon-rr'),222.67 data from dual
19  )
20  select id
21       , mon
22       , data
23       , data - lag (data, 1, 0) over (partition by trunc (mon, 'yyyy')
24                 order by mon)
25    from test
26   order by id
27  ;
        ID MON               DATA DATA-LAG(DATA,1,0)OVER(PARTITI
         1 1-1-2010            10                             10
         2 1-2-2010            20                             10
         3 1-3-2010            30                             10
         4 1-4-2010            50                             20
         5 1-5-2010            60                             10
         6 1-6-2010            70                             10
         7 1-7-2010           100                             30
         8 1-8-2010           110                             10
         9 1-9-2010           120                             10
        10 1-10-2010          140                             20
        11 1-11-2010          150                             10
        12 1-12-2010          170                             20
        13 1-1-2009        180,67                         180,67
        14 1-2-2009        194,67                             14
        15 1-3-2009        208,67                             14
        16 1-4-2009        222,67                             14
16 rows selected

Similar Messages

  • Need help in GROUPING of COLUMN Based Query

    Hi,
    I am facing problem in generating a scenario:
    I have built a Query like this:
    SELECT
    a.sal,
    a.country
    FROM
    employee a,
    department d
    WHERE
    a.dept_code=b.dept_code
    I want the query where the Query will produce a result set based on SALARY based on COUNTRY.
    For example if the COUNTRY is US then it will show the breaking of SALARIES.If the COUNTRY is NON US it will club the SALRIES
    Any help will be needful for me

    Hi,
    Here's one way:
    SELECT       deptno
    ,       SUM (sal)     AS sal
    FROM       scott.emp
    GROUP BY  deptno
    ,            CASE
               WHEN  deptno = 30
               THEN  empno
           END
    ORDER BY  deptno
    ;Output:
    `   DEPTNO        SAL
            10       8750
            20      10875
            30       1600
            30       1250
            30       1250
            30       2850
            30       1500
            30        950In the table, there are 3 rows with deptno=10, and 5 rows with deptno=20, but the CASE expression returns NULL for all of them, so there is only 1 sub-group for each of those deptnos.
    Edited by: Frank Kulash on Mar 13, 2013 5:41 AM
    This is essentially the same solution that Jeneesh already posted. (Sorry, I didn't see that before.)

  • Need help with SQL for selecting ID where the sequence does not match..

    I have the following dilemma:
    Database contains IDs as follows:
    Incident#, Case#, & Part Sequence#
    example
    Record 1
    Incident_Number = 123456
    Case_Number = 1
    Part_sequence = 1
    Record 2
    Incident_Number = 123456
    Case_Number = 1
    Part_sequence = 2
    Sometimes the user will delete (let's say) Record 2 after creating a new Record 3
    So now the sequencing goes as follows:
    Record 1
    Incident_Number = 123456
    Case_Number = 1
    Part_sequence = 1
    Record 2
    Incident_Number = 123456
    Case_Number = 1
    Part_sequence = 3
    Now there will no longer be a Part_Sequence 2
    Need a SQL to select all records where the maximum part sequence > than the count of Incident_number||'-'||Case_number
    I tried the following:
    select a.incident_number||'-'||a.case_number||'-'||a.part_sequence
    from chsuser.a_compl_summary a
    where a.entry_date >= '01-may-2011'
    and max(a.part_sequence) > count(distinct a.incident_number||'-'||a.case_number)I end up getting a ORA-00934: group function is not allowed here (highlighting on the Max(a.part_sequence) portion.
    Any suggestions/hints
    Thanks

    select  incident_number || '-' || case_number || '-' || part_sequence
      from  (
             select  incident_number,
                     case_number,
                     part_sequence,
                     max(part_sequence) over(partition by incident_number,case_number) max_seq,
                     count(*) over(partition by incident_number,case_number) cnt
               from  chsuser.a_compl_summary
               where entry_date >= DATE '2011-05-01'
      where cnt != max_seq
    /SY.

  • Need help with SQL Query with Inline View + Group by

    Hello Gurus,
    I would really appreciate your time and effort regarding this query. I have the following data set.
    Reference_No---Check_Number---Check_Date--------Description-------------------------------Invoice_Number----------Invoice_Type---Paid_Amount-----Vendor_Number
    1234567----------11223-------------- 7/5/2008----------paid for cleaning----------------------44345563------------------I-----------------*20.00*-------------19
    1234567----------11223--------------7/5/2008-----------Adjustment for bad quality---------44345563------------------A-----------------10.00------------19
    7654321----------11223--------------7/5/2008-----------Adjustment from last billing cycle-----23543556-------------------A--------------------50.00--------------19
    4653456----------11223--------------7/5/2008-----------paid for cleaning------------------------35654765--------------------I---------------------30.00-------------19
    Please Ignore '----', added it for clarity
    I am trying to write a query to aggregate paid_amount based on Reference_No, Check_Number, Payment_Date, Invoice_Number, Invoice_Type, Vendor_Number and display description with Invoice_type 'I' when there are multiple records with the same Reference_No, Check_Number, Payment_Date, Invoice_Number, Invoice_Type, Vendor_Number. When there are no multiple records I want to display the respective Description.
    The query should return the following data set
    Reference_No---Check_Number---Check_Date--------Description-------------------------------Invoice_Number----------Invoice_Type---Paid_Amount-----Vendor_Number
    1234567----------11223-------------- 7/5/2008----------paid for cleaning----------------------44345563------------------I-----------------*10.00*------------19
    7654321----------11223--------------7/5/2008-----------Adjustment from last billing cycle-----23543556-------------------A--------------------50.00--------------19
    4653456----------11223--------------7/5/2008-----------paid for cleaning------------------------35654765-------------------I---------------------30.00--------------19
    The following is my query. I am kind of lost.
    select B.Description, A.sequence_id,A.check_date, A.check_number, A.invoice_number, A.amount, A.vendor_number
    from (
    select sequence_id,check_date, check_number, invoice_number, sum(paid_amount) amount, vendor_number
    from INVOICE
    group by sequence_id,check_date, check_number, invoice_number, vendor_number
    ) A, INVOICE B
    where A.sequence_id = B.sequence_id
    Thanks,
    Nick

    It looks like it is a duplicate thread - correct me if i'm wrong in this case ->
    Need help with SQL Query with Inline View + Group by
    Regards.
    Satyaki De.

  • Need to show the images in calculated column based on the condition in sharepoint 2013.

    Hi ,
    I'm working in sharepoint 2013, In this i want to show an image in calculated column based on the condition, but image is not showing .
    In sharepoint 2010 its working with help of javascript in content editor on the same page. Same thing i tried in SP 2013. but doesn't work.
    Could any one please give proper solution for this?
    Thanks in advance.
    by,
    Siva.

    Hi please provide the JS you have used in the content editor as well as the calculation for the column so we can better understand what's maybe differs?
    Blog: chrisstahl.wordpress.com Twitter:
    @Cstahl

  • I need help installing my adobe premiere element 13. the soft ware is 3.5 GB and I have a 654.4GB space is this enought space to dowload the software. It appears the software is downloading , but when it gets to the end it gives me the error summary messa

    I need help installing my adobe premiere element 13. the soft ware is 3.5 GB and I have a 654.4GB space is this enought space to dowload the software. It appears the software is downloading , but when it gets to the end it gives me the error summary message. I have been on the phone for hours being transfered from from person to the next with no results.

    reesep
    This is not Adobe. Rather user to user. The Adobe Staff and PRE_help presence in this forum is undefined.
    The replies from here are not instantaneous. But, your fellow users all try to respond as timely as possible.
    What is your computer operating system?
    From where are you downloading Premiere Elements 13....direct from Adobe as the tryout or the purchased product? If the purchased product, do you now have a purchased serial number to go with the installation files?
    Where are you stopped in the installation....at the Sign In or before? Is the faulting module cited in any messages? What are the error messages?
    The usual questions:
    1. Are you working with the antivirus and firewall(s) disabled?
    2. Are you running the computer programs as administrator?
    3. Are you working with a pen and tablet device instead of mouse?
    4. Is your video card/graphics card up to date according to the web site of the manufacturer of the card?
    5. Are you installing the program to the default location on the Local Disc C (if Windows).
    Let us start here and then decide what next based on the details that you post. Subsequent troubleshooting may include:
    deletion of the Adobe Premiere Elements Prefs file and/or uninstall, free ccleaner run through, reinstall with antivirus and firewall(s) disabled.
    Any questions or need clarification, please do not hesitate to ask.
    Thank you.
    ATR

  • Sql agent jobs based on the database and the user

    Dear All,
    I have a requirement of configuring an user who should only have access to one database out of all and also able to create, view and modify sql agent jobs only for the allowed database. For allowing to see the selected database to particular user I have
    granted permissions as per below,
    create LOGIN mhtc WITH PASSWORD='mhtc', CHECK_POLICY = OFF;
    USE master;
    GO
    DENY VIEW ANY DATABASE TO mhtc;
    USE master;
    GO
    ALTER AUTHORIZATION ON DATABASE::MHTC TO mhtc;
    GO
    but user not able to see sql agent jobs so I want to give necessary permission to this user to view,create and modify sql agent jobs only in allowed databases. Basically I want to separate users from each database and separate sql agent jobs based on the
    user and database.
    Your expert advices are highly appreciated.
    Thanks,
    Manjula

    I want to give necessary permission to this user to view,create and modify
    sql agent jobs only in allowed databases.
    Hello Manjula,
    SQL Server Agent Jobs are not created / dedicated for a user database. The Jobs are store in System database "msdb".
    See
    Implement SQL Server Agent Security for Details about required permissions.
    Olaf Helper
    [ Blog] [ Xing] [ MVP]

  • Need help in modifying mapping parameters of out the box mapping

    Hi There,
    I am a new bee to dac.
    Need help in modifying mapping parameters of out the box mapping, which is invoked by DAC task.
    We got a requirement to edit mapping parameter. When I go and see parameter under mappings tab in a mapping, I could not see any values in it.
    But when I set any value, and validate it. It is successful.
    Is it right way to do it?
    What my concern is, When I initially go and see parameter values under maapings tab in a mapping, they are blank.
    Where is it storing these values?
    Thanks,
    Rag

    If you modify mapping then u have to create new task in dac and dac itself craete parameter file at run time. if you want to add more parameters then do it in dac system parameters tab.
    Thanks
    Jay.

  • When I import a text file(comma separated )into a numbers spread sheet all the data goes into one column. Why does the text not go into separate columns based on the commas.

    When I import a text file(comma separated) into a numbers spreadsheet all the data goes into one column instead of individual columns based on the comma separators.  Excel allows you to do this during the import..  Is there a way to accomplish this in numbers without opening it in Excel and the importing into Numbers.

    Your user info says iPad. This is the OS X Numbers forum. Assuming you are using OS X… Be sure the file is named with a .csv suffix.
    (I don't have an iPad, so I don't know the iOS answer.)

  • I need help in activating apple id to for the find my iphone app

    i need help in activating apple id to for the find my iphone app

    What sort of help are you requesting from us, your fellow users? What exactly is the problem/issue?

  • HT6114 I need help hacking into my ipod i forgot the password and i cant factory reset it because the power button is broken?!

    I need help hacking into my ipod i forgot the password and i cant factory reset it because the power button is broken?!

    If your iPod Touch, iPhone, or iPad is Broken
    Apple does not fix iDevices. Instead, they exchange yours for a refurbished or new replacement depending upon the age of your device and refurbished inventories. On rare occasions when there are no longer refurbished units for your older model, they may replace it with the next newer model.
    ATTN: Beginning July 2013 Apple Stores are now equipped to do screen repairs/replacements in-house on iPhone 5 and 5C. In some cases while you wait. According to Apple this is the beginning of equipping Apple Stores with the resources needed to do most repairs for iPhones, iPads, and iPod Touches that would not require major replacements. Later in the year the services may be extended as Apple Stores become equipped and staffed with the proper repair expertise. So, if you need a screen repaired or a broken screen replaced or have your stuck Home button fixed, call your local Apple Store to see if they are now doing these in-house.
    You may take your device to an Apple retailer for help or you may call Customer Service and arrange to send your device to Apple:
    Apple Store Customer Service at 1-800-676-2775 or visit online Help for more information.
    To contact product and tech support: Contacting Apple for support and service - this includes international calling numbers.
    You will find respective repair costs in the appropriate link:
    iPod Service Support and Costs
    iPhone Service Support and Costs
    iPad Service Support and Costs
    There are third-party firms that do repairs on iDevices, and there are places where you can order parts to DIY if you feel up to the task:
    1. iResq or Google for others.
    2. Buy and replace screen yourself: iFixit
    Locked Out, Forgot Lock or Restrictions Passcode, or Need to Restore Your Device: Several Alternative Solutions
    A
    1. iOS- Forgotten passcode or device disabled after entering wrong passcode
    2. iPhone, iPad, iPod touch: Wrong passcode results in red disabled screen
    3. Restoring iPod touch after forgotten passcode
    4. What to Do If You've Forgotten Your iPhone's Passcode
    5. iOS- Understanding passcodes
    6. iTunes 10 for Mac- Update and restore software on iPod, iPhone, or iPad
    7. iOS - Unable to update or restore
    Forgotten Restrictions Passcode Help
                iPad,iPod,iPod Touch Recovery Mode
    You will need to restore your device as New to remove a Restrictions passcode. Go through the normal process to restore your device, but when you see the options to restore as New or from a backup, be sure to choose New.
    You can restore from a backup if you have one from BEFORE you set the restrictions passcode.
    Also, see iTunes- Restoring iOS software.

  • Hello, my iphone for several times shows me this message "this iphone can't be used because the Apple Mobile Device service not started". I need help because i'm enable do download the new update. Thanks

    Hello, my iphone for several times shows me this message "this iphone can't be used because the Apple Mobile Device service not started".
    I need help, because i'm enable do download the new update.
    Thanks

    Type "Apple Mobile Device service" into the search bar at the top of this age by "Support" and read the resulting help articles.

  • NEED HELP!! i restore backup at the wrong iphone

    NEED HELP!! i restore backup at the wrong iphone
    i have 2 iphone 4s.. i already have backuped the 1st iphone, then the 2nd iphone not yet backuped.
    when i plug the 2nd iphone it restore the backup of 1st iphone.. now the 1st and 2nd iphone has the same files... the original files of 2nd iphone has gone, how to restored the original files of 2nd iphone since i restored the backup of 1st iphone.and i do not have backup for the 2nd iphone. >.< PLEASE HELP!!!!

    With no backup for the 2nd iPhone, you can't.

  • Hi, Need help. I am trying to download the latest iTunes for Windows application but on running the program it says that it can not be completed as it can not find the folder location of the file "iTunes.msi" . Any clues would be appreciated

    Hi, Need help. I am trying to download the latest iTunes for Windows application but on running the program it says that it can not be completed as it can not find the folder location of the file "iTunes.msi" . Any clues would be appreciated

    (1) Download the Windows Installer CleanUp utility installer file (msicuu2.exe) from the following Major Geeks page (use one of the links under the "DOWNLOAD LOCATIONS" thingy on the Major Geeks page):
    http://majorgeeks.com/download.php?det=4459
    (2) Doubleclick the msicuu2.exe file and follow the prompts to install the Windows Installer CleanUp utility. (If you're on a Windows Vista or Windows 7 system and you get a Code 800A0046 error message when doubleclicking the msicuu2.exe file, try instead right-clicking on the msicuu2.exe file and selecting "Run as administrator".)
    (3) In your Start menu click All Programs and then click Windows Install Clean Up. The Windows Installer CleanUp utility window appears, listing software that is currently installed on your computer.
    (4) In the list of programs that appears in CleanUp, select any iTunes entries and click "Remove", as per the following screenshot:
    (5) Quit out of CleanUp, restart the PC and try another iTunes install. Does it go through properly this time?

  • I need help with changing my verizon vm to the system voicemail

    I need help with changing my verizon vm to the system voicemail

    Deleting the iTunes account is not the solution as you will very likely lose any and all purchases ever made with the account.
    If you cannot find the option on the site, contact iTunes support.

Maybe you are looking for

  • Mid 2009 MacBook Pro - Running really slow.

    Hi, I'm having problems with my mid 2009 MBP running really slowly. The problem seems to be the mds_stores Process using lots of CPU and memory. I have done the following to try and resolve it: Uninstalled AV software (Avast) Created new admin user a

  • Do I need to update DST Patch in both locations.

    Hi, I have Solaris 10 installed on SPARc machine which is having Java 1.5 version buy defauly and doesnot reqiuire patch update as I have understand. But my application runs on Java 1.4.05 at present and java home is a custom path different from defa

  • Migrated to Macbook Air. Apps now on both new and old macbooks. How?

    I have just used the migration software to tranfer to a late 2011 Macbook Air (Lion) from a late 2009 Macbook Pro (Snow Leopard). My intention is to sell my old Macbook Pro. After migrating, I now have apps (ex. iLife, iWorks, Office, etc) that appea

  • DW CS3 very high CPU usage

    Just installed DW CS3, installation went normally without any problems. However when I start the program, DreamWeaver.exe constantly occupies between 80 and 100% of the CPU resources - even when minimized. The same happens on 2 XP SP2 systems, one 3G

  • What is OBIEE?

    Hi, I have a background in using Hyperion products, from Essbase to Planning to Shared Services, etc... I'm trying to learn about OBIEE and started googling and did not find the exact answers I was looking for. What exactly is OBIEE? How is it differ