Finding monthly changes in a data set

Hello
Given the below sample table and data, I am after a SQL script that can generate the sample output shown.
In a nutshell, I need to identify in a derived column, compared to the previous month, what's:
1) Not changed (e.g. 'A' in Feb)
2) Been removed (e.g. 'B' in Feb)
3) Been added (e.g. 'D' in Feb)
Can someone help with this? From what I remember Windows function would be ideal for this but it has been a long time since I used them...
CREATE TABLE [dbo].[Data](
[MyDate] [date] NOT NULL,
[MyText] [varchar](10) NOT NULL
) ON [PRIMARY]
GO
-- Jan
INSERT INTO [dbo].[Data](MyDate,MyText) VALUES ('01Jan2014', 'A')
INSERT INTO [dbo].[Data](MyDate,MyText) VALUES ('01Jan2014', 'B')
INSERT INTO [dbo].[Data](MyDate,MyText) VALUES ('01Jan2014', 'C')
-- Feb - 'B' dropped, 'D' added
INSERT INTO [dbo].[Data](MyDate,MyText) VALUES ('01Feb2014', 'A')
INSERT INTO [dbo].[Data](MyDate,MyText) VALUES ('01Feb2014', 'C')
INSERT INTO [dbo].[Data](MyDate,MyText) VALUES ('01Feb2014', 'D')
-- Mar - 'A' and 'C' dropped, 'E' and 'F' added
INSERT INTO [dbo].[Data](MyDate,MyText) VALUES ('01Mar2014', 'D')
INSERT INTO [dbo].[Data](MyDate,MyText) VALUES ('01Mar2014', 'E')
INSERT INTO [dbo].[Data](MyDate,MyText) VALUES ('01Mar2014', 'F')
/* Sample required output
MyDate MyText State
2014-01-01 A 0
2014-01-01 B 0
2014-01-01 C 0
2014-02-01 A 0
2014-01-01 B -1
2014-02-01 C 0
2014-02-01 D 1
2014-02-01 A -1
2014-02-01 C -1
2014-03-01 D 0
2014-03-01 E 1
2014-03-01 F 1

;With CTE
AS
SELECT d.MyDate,t.MyText,d1.MyDate AS RetDate,ROW_NUMBER() OVER (PARTITION BY t.MyText ORDER BY d.MyDate) AS Rn
FROM (SELECT DISTINCT MyDate FROM Data)d
INNER JOIN (SELECT DISTINCT MyText,MIN(Mydate) AS MinDate,MAX(Mydate) AS MaxDate
FROM Data
GROUP BY MyText
)t
ON t.MinDate <= d.MyDate
AND d.MyDate <= DATEADD(mm,1,t.MaxDate )
LEFT JOIN Data d1
ON d1.MyDate = d.MyDate
AND d1.MyText = t.MyText
SELECT MyDate,MyText,
CASE WHEN RetDate IS NULL THEN -1
WHEN Rn > 1 THEN 0
ELSE 1
END
FROM CTE
ORDER BY MyDate,MyText
Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

Similar Messages

  • Can I use ReadXML/WriteXML to change Meta Data Set Type?

    Has anyone successfully changed which Meta Data Set an asset is using? In the XML description of the asset there's this entry:
    <mdValue fieldName="Metadata Set" dataType="string">paassetmedia</mdValue>
    but if I try to change it by using this XML statment it doesn't change:
    <FinalCutServer>
    <request reqId="setMd" entityId="/asset/4922">
    <params>
    <mdValue fieldName="Metadata Set" dataType="string">paassetshot</mdValue> </params>
    </request>
    </FinalCutServer>
    I know the XML is working because I can change any mdValue besides the MetaData Set.
    Ideas?

    Nick is correct - the only way that you are going to be able to change the MD Set for a given asset is to duplicate it and to delete the original. I've had to do this a few times, using search expired + subscription + responses to do the trick.
    Ideally you get the md set right when you first add the assets

  • Where can I find the current time and date of Time Capsule?

    I have a home network consisint of 2 x Time Capsules and 1 x Airport Express.  All is working fine.  I would like to set up access control for one machine connecting to the network and I can see I can do this under Access Control on the Airport Utility.  My question is where can I find the current time and date setting for these devices? 
    I can set them to use the time.apple.com server to set the time automatically, but I can't find where to see the current time and date of the device.  The only place I can see reference to the time and date is on the logs and statistics under the Advanced tab of the AirPort Untility.

    I don't think you can change the time and date on the TC itself, it just connects to a time server (and you set your own time zone).

  • Open data set from Text mode to Binary mode

    Hello All,
    In a program i am using the command
        OPEN DATASET file FOR OUTPUT IN TEXT MODE
        ENCODING NON-UNICODE IGNORING CONVERSION ERRORS.
    Now i want to change this open data set to binary mode. I mean to change the command like below.
    OPEN DATASET file FOR OUTPUT IN BINARY MODE.
    when i do this. the output in the file is totally changing. I mean previously i have some text now i have some diffrent characters.
    How can i use this command with out change in the output as previous output. Please tell me  the changes and why i have to do.
    OPEN DATASET file FOR OUTPUT IN BINARY MODE.
    Regards,
    Lisa.
    Message was edited by:
            Lisa Roy

    Text Mode means that the records in the file have a carriage return marking the end of each record.
    Binary Mode means the file is a string of data.
    You can't read a text file in BINARY MODE, or a binary file in TEXT MODE.
    If you are changing the mode, then you must be supplied with a file in the new mode.

  • To put a new page when month changes in sap script

    hello experts,
    how to put a page break whenever the month changes from the date range given ...
    e,g : 12.03.2007 to 12.09.2007 ... here i want to put the page break betwwen the records of month 03 .. 04..05..06... 09...

    Hi
    It depends on how you manage your sapscript, anyway in your program load all data the range in a internal table:
    DATA: BEGIN OF ITAB OCCURS,
                  DATE LIKE SY-DATUM,
              END    OF ITAB.
    ITAB-DATE = DATE_TO.
    APPEND ITAB.
    MONTH = DATE_TO+4(2) + 1.
    DO.
      IF ITAB-DATE = DATE_TO. EXIT. ENDIF.
      ITAB-DATE+4(2) = MONTH.
      IF ITAB-DATE(6) = DATE_TO(4).
        ITAB-DATE = DATE_TO.
      ENDIF. 
      APPEND ITAB-DATE.
    ENDDO.
    LOOP AT ITAB.
      CALL FUNCTUON 'CONTROL_FORM'
            COMMAND = 'NEW-PAGE'.
    ENDLOOP.
    Max

  • OPEN DATA SET change character "SPACE" to '..'

    Hi!
       I am loadind a XML file by OPEN DATA SET but it is changing from the "space" Hex = 20 to '..' Hex = "0D0C".
       Do you know where is the problem?
    Thanks.
    ABAP  -
    >
       OPEN DATASET VP_FILEXML FOR INPUT IN BINARY MODE.
        OPEN DATASET VP_FILEXML FOR INPUT IN TEXT MODE ENCODING UTF-8.
       REFRESH objbin.
       DO.
           READ DATASET VP_FILEXML INTO objbin.
           IF sy-subrc EQ 0.
              APPEND objbin.
              CLEAR  objbin.
           ELSE.
              APPEND objbin.
              CLEAR  objbin.
              IF V_FLAG_EXIT = 'X'.
                 EXIT.
              ENDIF.
              V_FLAG_EXIT = 'X'.
           ENDIF.
       ENDDO.
       CLOSE DATASET VP_FILEXML.

    Hi
    Change TYPE X values to TYPE C in data declaration ans then call class CL_ABAP_CHAR_UTILITIES
    For "0D0C" call class:
    for 0D:
    CLASS cl_abap_char_utilities DEFINITION LOAD.
    DATA : C_CR TYPE C VALUE  CL_ABAP_CHAR_UTILITIES=>CR_LF.
    for 0C:
    CLASS cl_abap_char_utilities DEFINITION LOAD.            
    DATA : C_FORM TYPE C VALUE CL_ABAP_CHAR_UTILITIES=>FORM_FEED.
    check this link for more details.. [Link|http://help.sap.com/saphelp_nw04/helpdata/en/79/c554d9b3dc11d5993800508b6b8b11/frameset.htm]
    you will get more information on hexa decimal related
    Thanks and regards.

  • I need to find out how much wifi data my apps are using. I have a very limited amount of wifi data, and I am exceeding my monthly allowance. Apparently, even apps I think are not open are sending/receiving data through the wifi and using up my allowance.

    I need to find out how much wifi data my apps are using. I am on a very limited amount of WiFi data each month, which I am regularly exceeding. I have been told to work out which of my apps is using the data. Also, I think I have closed an app by double clicking the home button, then swiping the app up - is this the way to close it, or will it still be sending/receiving data?

    Go into your Settings : General : and turn off background refresh for your apps.  In Settings : Mail  turn Fetch new data to OFF and Load Remote Images to OFF.  This will mean that Mail will only check for messages when you actually use it, and all your advertising junk mail won't have all the images in it.
    Turn off push notifications every chance you get.
    Make sure you are actually quitting apps:  to quit apps press the Home button twice and you should see a bunch of smaller screen images for every open app.  To quit the app swipe from the screen image (not the icon) upward off the top of the iPad.  You can swipe left and right to see more open apps, but there must be no left-right movement on the screen when you swipe upward to close the app.
    Turn off your internet connection when you do not need it.  The easiest way to do this is to swipe up from the bottom of you screen to get the control centre, and then touch the airplane to turn on airplane mode.  You can repeat this sequence to turn it back on again when you need it.  Most especially turn airplane mode on whenever you are sleeping your iPad for long periods.  This will save battery life too.  OR actually turn your iPad off - which means holding the power key down for several seconds until the red swipe bar appears, and then swipe to turn it off.  If you go this route, note that it will take longer to turn on then it takes to wake from sleep.

  • Find month and year of a given date

    Hi,
    How to find the month and year of a given date.
    month ( sy-datum)  year ( sy-datum )
    --Bala

    hi
    DATA: EDAYS LIKE VTBBEWE-ATAGE,
    EMONTHS LIKE VTBBEWE-ATAGE,
    EYEARS LIKE VTBBEWE-ATAGE.
    PARAMETERS: FROMDATE LIKE VTBBEWE-DBERVON,
    TODATE LIKE VTBBEWE-DBERBIS DEFAULT SY-DATUM.
    Call Function 'FIMA_DAYS_AND_MONTHS_AND_YEARS'
      exporting
        i_date_from          = FROMDATE
        i_date_to            = TODATE
      I_FLG_SEPARATE       = ' '
      IMPORTING
        E_DAYS               = EDAYS
        E_MONTHS             = EMONTHS
        E_YEARS              = EYEARS.
    WRITE:/ 'Difference in Days   ', EDAYS.
    WRITE:/ 'Difference in Months ', EMONTHS.
    WRITE:/ 'Difference in Years  ', EYEARS.
    INITIALIZATION.
    FROMDATE = SY-DATUM - 60.
    Using teh abiove u can get difference but when u pass previous year u wont get the exact.
    There is no seperate FM for this, u have to use three FM.
    If possible using these three FM code u can create an FM.
    For years and months between two days:
    DATA:   EYEARS  LIKE VTBBEWE-ATAGE.
    PARAMETERS: FROMDATE LIKE PREL-BEGDA,
                     TODATE   LIKE PREL-BEGDA DEFAULT SY-DATUM.
    CALL FUNCTION 'COMPUTE_YEARS_BETWEEN_DATES'
      EXPORTING
        first_date                        = fromdate
      MODIFY_INTERVAL                   = ' '
        second_date                       = todate
    IMPORTING
       YEARS_BETWEEN_DATES               =  EYEARS
    EXCEPTIONS
      SEQUENCE_OF_DATES_NOT_VALID       = 1
      OTHERS                            = 2
    IF sy-subrc <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    Write:/ eyears.
    DATA:       EMONTHS LIKE VTBBEWE-ATAGE.
    PARAMETERS: FROMDATE LIKE SY-DATUM,
                TODATE   LIKE SY-DATUM
    DEFAULT SY-DATUM.
    CALL FUNCTION 'MONTHS_BETWEEN_TWO_DATES'
      EXPORTING
        i_datum_bis         = fromdate
        i_datum_von         = todate
      I_KZ_INCL_BIS       = ' '
    IMPORTING
       E_MONATE            = emonths
    write:/ emonths
    CALL FUNCTION 'DAYS_BETWEEN_TWO_DATES'
    EXPORTING
    I_DATUM_BIS = x_faede-zfbdt
    I_DATUM_VON = p_fdat
    I_KZ_EXCL_VON = '0'
    I_KZ_INCL_BIS = '0'
    I_KZ_ULT_BIS = ' '
    I_KZ_ULT_VON = ' '
    I_STGMETH = '0'
    I_SZBMETH = '1'
    IMPORTING
    E_TAGE = dias_v.
    IF SY-SUBRC <> 0.
    ENDIF.
    x_faede-zfbdt -> 20050915
    p_fdat -> 20050811
    dias_v = 4
    try this and let me know.
    regards
    ravish
    <b>plz dont forget to reward useful points</b>

  • IAM-3056160:Modify User Profile request cannot set or change attribute Job Code, since it is not defined in the corresponding data set.

    I am trying to modify the value of the field "Job Code" through API I am getting the following error.(OIM11gr2). I do not get this error when updating the other fields. There is a field by the name USR_JOB_CODE in the database. When I poked around I found that there is no Job Code field in the User Form. Any ideas?
    IAM-3056160:Modify User Profile request cannot set or change attribute Job Code, since it is not defined in the corresponding data set.:Modify User Profile:Job Code
    oracle.iam.identity.exception.ValidationFailedException: IAM-3056160:Modify User Profile request cannot set or change attribute Job Code, since it is not defined in the corresponding data set.:Modify User Profile:Job Code
           at weblogic.rjvm.ResponseImpl.unmarshalReturn(ResponseImpl.java:237)
           at weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:348)
           at weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:259)
           at oracle.iam.identity.usermgmt.api.UserManager_nimav7_UserManagerRemoteImpl_1036_WLStub.modifyx(Unknown Source)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
           at java.lang.reflect.Method.invoke(Unknown Source)
           at weblogic.ejb.container.internal.RemoteBusinessIntfProxy.invoke(RemoteBusinessIntfProxy.java:85)
           at $Proxy2.modifyx(Unknown Source)
           at oracle.iam.identity.usermgmt.api.UserManagerDelegate.modify(Unknown Source)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke

    THanks for your reply. Here is the snippet from User.xml that contains info about job code.
    <entity-attribute>Job Code</entity-attribute>
    <target-field>usr_job_code</target-field>
    <field name="usr_job_code">
    <type>string</type>
    <required>false</required>
    </field>
    <attribute name="Job Code">
    <type>string</type>
    <required>false</required>
    <searchable>true</searchable>
    <multi-valued>false</multi-valued>
    <MLS>false</MLS>
    <multi-represented>false</multi-represented>
    <attribute-group>Basic</attribute-group>
    <metadata-attachment>
    <metadata>
    <name>multi-valued</name>
    <value>false</value>
    <category>properties</category>
    </metadata>
    <metadata>
    <name>user-searchable</name>
    <value>true</value>
    <category>properties</category>
    </metadata>
    <metadata>
    <name>category</name>
    <value>Preferences</value>
    <category>properties</category>
    </metadata>
    <metadata>
    <name>bulk-updatable</name>
    <value>true</value>
    <category>properties</category>
    </metadata>
    <metadata>
    <name>read-only</name>
    <value>false</value>
    <category>properties</category>
    </metadata>
    <metadata>
    <name>visible</name>
    <value>true</value>
    <category>properties</category>
    </metadata>
    <metadata>
    <name>encryption</name>
    <value>CLEAR</value>
    <category>properties</category>
    </metadata>
    <metadata>
    <name>display-type</name>
    <value>TEXT</value>
    <category>properties</category>
    </metadata>
    <metadata>
    <name>system-controlled</name>
    <value>false</value>
    <category>properties</category>
    </metadata>
    <metadata>
    <name>max-size</name>
    <value>512</value>
    <category>properties</category>
    </metadata>
    <metadata>
    <name>custom</name>
    <value>false</value>
    <category>properties</category>
    </metadata>
    </metadata-attachment>
    </attribute>
    I am able to retrieve the value of the Job Code attribute without any problem with the following code.
    System.out.println("JOB Code: "+user.getAttribute("Job Code"));

  • Find 3rd friday of the month for a given date

    how to find out 3rd friday of the month for a given date? I can always pass the first day of the month as input
    eg, input date is 01-JAN-09. need to get the 3rd friday 16-JAN-09 (Jan has 5 fridays 02, 09, 16,23,30)
    input date : 01-feb-09, need to get 20-feb-09
    Edited by: user520824 on Apr 1, 2009 12:30 PM

    NLS independent solution:
    SELECT  DT,
            TO_CHAR(DT,'FMDay') day,
            CASE
              WHEN TRUNC(TRUNC(DT,'MM') + 7,'IW') - 3 < TRUNC(DT,'MM') THEN TRUNC(TRUNC(DT,'MM') + 7,'IW') + 18
              ELSE TRUNC(TRUNC(DT,'MM') + 7,'IW') + 11
            END THIRD_FRIDAY_OF_THE_MONTH
      FROM  (
             SELECT  TO_DATE(LEVEL || '/2009','MM/YYYY') DT
               FROM  DUAL
               CONNECT BY LEVEL < 13
    DT        DAY       THIRD_FRI
    01-JAN-09 Thursday  16-JAN-09
    01-FEB-09 Sunday    20-FEB-09
    01-MAR-09 Sunday    20-MAR-09
    01-APR-09 Wednesday 17-APR-09
    01-MAY-09 Friday    15-MAY-09
    01-JUN-09 Monday    19-JUN-09
    01-JUL-09 Wednesday 17-JUL-09
    01-AUG-09 Saturday  21-AUG-09
    01-SEP-09 Tuesday   18-SEP-09
    01-OCT-09 Thursday  16-OCT-09
    01-NOV-09 Sunday    20-NOV-09
    DT        DAY       THIRD_FRI
    01-DEC-09 Tuesday   18-DEC-09
    12 rows selected.
    SQL> SY.

  • HT4839 I cant find the cellular data setting on my iphone how i can fix it

    i cant find the cellular data setting on my iphone
    how i can fix it
    because I can't use the internet with my iphone right now

    If you go into Settings>General>Network, you should still be able to access VPN settings. My listings show Cellular Data, Data Roaming, Set Up Personal Hotspot, VPN and Wi-Fi. I do not use Personal Hotspot (tethering) so mine says Setup. However, VPN settings are different than cellular data settings. Not sure which you are asking about now.

  • How to find out last month's 1st & last date

    could you please tell me how can I find out last month's 1st & last date?
    regards

    Here is one way:
    SQL> SELECT  ADD_MONTHS
      2          ( TRUNC(SYSDATE,'MONTH')
      3          , -1
      4          )                                    AS FIRST_OF_MONTH
      5  ,       TRUNC(SYSDATE,'MONTH') - 1           AS LAST_OF_MONTH
      6  FROM    DUAL
      7  /
    FIRST_OF_MONTH      LAST_OF_MONTH
    05/01/2010 00:00:00 05/31/2010 00:00:00

  • Date setting change

    Hello everybody,
    I have little confusion, on my server date setting is like M/d/yyyy.
    SAP on windows 2003 and SQL server 2005 platform.
    Please suggest if  I change date format from M/d/yyyy to dd/mm/yyyy, what will impact?
    is any impact on report, transaction etc.
    Thanks
    Ganesh

    Hello Ganesh,
    There is no issue in changing the date format as Arjun also said in the last post.. Usually Dates are shown as what is set in user profile..in SU01...
    More info : Note 1379084 - Conversion of date from external to internal format

  • Change my bill date because i only get paid a set date

    Hello. I am wanting to change my bill date to the 1st every month. How do i go about changing it ??

    By phoning EE.

  • HT5706 My Apple TV is frozen on the time and date setting and then it goes to the screen saver I tried changing the batteries on the remote and it still doesn't make a difference someone help

    My Apple TV is frozen on the time and date setting and then it goes to the screen saver I tried changing the batteries on the remote and it still doesn't make a difference someone help...has anyone else had these problems?

    Hi i ran into the same problem Ithoght it was the control, i changed the batteries the the control, and stil did not work, in the same connection (network and everything) i installed a new one and it worked perfectly, i can deduct the unit is nit working properly

Maybe you are looking for

  • Error in Parsing the Taglib portion of JSP

    Weblogic failed to parse the JSP file. In the JSP file, I use Tag libraries.           The setter method in the Tag class accepts the input as 'java.lang.Object'.           If I pass the attribute value as string as given below in the error          

  • Duplicate the details section when I retrieve data from Delivery notes

    Hey all. I built a query that retrieve data from ODLN and it works fine. But, when I have a Delivery Notes Document that has items from some different orders (that I copy them to Delivery notes of course) it duplicate my rows in the report I run the

  • Badi or User-Exit for TCode-oawd

    Hi All, Please help me out with a BADI or EXIT for the Tcode - OAWD (Store Documents) .As per our requirement, while executing OAWD, after the step - work item created, we want to add a pop-up which will take us to tcode- SBWP. Any help will be highl

  • Internal MBP HD up as external - cant see Admin user

    Woke up to my Macbook Pro a couple weeks ago sitting in a puddle of water. Fried. Went to Apple Store and had them take out internal hard drive and bought an external case for it. They said the HD looked ok and was worth a shot. Today I finally found

  • File Sharing Missing from System Preferences Sharing

    Hello, I am running MAC OSX Server 10.5.8. When I open System Preferences and click on Sharing I do not see the option to enable File Sharing. What must I do to get that to show up in the list? Right now my list looks like this... __ DVD or CD Sharin