Sql queries for date and year

Hi Friends,
I Have a view named - item_sales with 4 column
Item code
Item name
Transaction_YYYYMM (Date stored in YYYYMM format )
QTY_RECEIVED
QTY_SOLD
Sample data is
ITEM_CODE ITEM NAME  TRANSACTION_YYYMM     QTY_RECD    QTY_SOLD
AX             TSHIRT                201307                             3000               2000
AX             TSHIRT                201308                             2000               500
AX             TSHIRT                201309                             1000              3000
CX             XLSHIRT              201307                              3000             2000
CX             XLSHIRT              201308                              3000             2500
CX             XLSHIRT             201309                               3000             2500
EVERY MONTH END I WILL RUN THIS QUERY TO FIND OUT THE BELOW DETAILS
1. TO FIND ITEM_NAME WISE  -  QTY_RECEIVED AND QTY_SOLD ( FOR CURRENT MONTH - EXAMPLE SEP )
2. TO FIND ITEM_NAME WISE  -  QTY_RECEIVED AND QTY_SOLD   (FOR CURRENT YEAR EXAMPLE FROM JAN TO SEP )
OUTPUT FOR SEPTEMBER MONTH LOOK LIKE THIS
                                                        SEP-MONTH                              JAN TO SEP
ITEM_CODE   ITEM_NAME    QTY_RECEIVED  QTY_SOLD      QTY_RECEIVED  QTY_SOLD
AX                TSHIRT                   1000                 3000                 6000                 5500
CX                XLSHIRT                  3000                2000                 9000                 7000
Pls advise me how to write queries for this
Rdk

Just FYI, you *can* edit your own posts, you know
Rdk wrote:
Transaction_YYYYMM (Date stored in YYYYMM format )
First "problem". Don't store dates as string. Store them as dates. It will save you so much headache don't the road you won't believe it.
True, this is a view, so maybe not as critical - assuming the underlying *DATA* is actually a date.
1. TO FIND ITEM_NAME WISE  -  QTY_RECEIVED AND QTY_SOLD ( FOR CURRENT MONTH - EXAMPLE SEP )
2. TO FIND ITEM_NAME WISE  -  QTY_RECEIVED AND QTY_SOLD   (FOR CURRENT YEAR EXAMPLE FROM JAN TO SEP )
So yeah, based on these requirements, I'd recommend you make that column a DATE, not a string. Dates are easier to parse for date-related logic - such as month by month as you need here.
Using that, here's one way to do it:
with w_data as (
      select 'AX' item_code, 'TSHIRT ' item_name, to_date('20130701','yyyymmdd') trans_dt, 3000 qty_recd, 2000 qty_sold from dual union all
      select 'AX'          , 'TSHIRT '          , to_date('20130801','yyyymmdd')         , 2000         , 500           from dual union all
      select 'AX'          , 'TSHIRT '          , to_date('20130901','yyyymmdd')         , 1000         , 3000          from dual union all
      select 'CX'          , 'XLSHIRT'          , to_date('20130701','yyyymmdd')         , 3000         , 2000          from dual union all
      select 'CX'          , 'XLSHIRT'          , to_date('20130801','yyyymmdd')         , 3000         , 2500          from dual union all
      select 'CX'          , 'XLSHIRT'          , to_date('20130901','yyyymmdd')         , 3000         , 2500          from dual
   w_base as (
      select item_code, item_name, trans_dt, qty_recd, qty_sold,
             sum(qty_recd) over (partition by item_code, trunc(trans_dt, 'MM')) mm_recd,
             sum(qty_sold) over (partition by item_code, trunc(trans_dt, 'MM')) mm_sold,
             sum(qty_recd) over (partition by item_code, trunc(trans_dt, 'YY')) yy_recd,
             sum(qty_sold) over (partition by item_code, trunc(trans_dt, 'YY')) yy_sold,
             row_number() over (partition by item_code order by trans_dt desc) rnum
        from w_data d
Select item_code, item_name, mm_recd, mm_sold, yy_recd, yy_sold
  from w_base
where rnum = 1
IT ITEM_NA    MM_RECD    MM_SOLD    YY_RECD    YY_SOLD
AX TSHIRT        1000       3000       6000       5500
CX XLSHIRT       3000       2500       9000       7000

Similar Messages

  • Sql queries for retrieving setups data for functional modules

    Hi,
    Can anyone give me the sql queries for retrieving setups data for functional modules (GL, AP, AR, FA, and CM) from Database.

    Hi,
    Can anyone give me the sql queries for retrieving setups data for functional modules (GL, AP, AR, FA, and CM) from Database.

  • Data element for Month and Year

    Hello All,
    Is there any data element which will have only Month and Year.
    I have to introduce this field in a table. It should have convesion exits also.
    Ex: If i give 092009, it sould come like 09.2009
    Thank you.
    Best Regards,
    Sasidhar Reddy Matli.

    Kindly Try this code for Month and year as input and having standard F4 help..
    INCLUDE RMCS0F0M.
    TYPES : BEGIN OF TY_SELECT,
              MONTH TYPE FTI_MONTH_YEAR,
            END OF TY_SELECT.
    DATA : WA_SELECT TYPE TY_SELECT.
    SELECTION-SCREEN : BEGIN OF BLOCK SANDEEP WITH FRAME.
      SELECT-OPTIONS : S_MONTH FOR WA_SELECT-MONTH OBLIGATORY NO INTERVALS NO-EXTENSION.
    SELECTION-SCREEN : END OF BLOCK SANDEEP.
    AT SELECTION-SCREEN ON VALUE-REQUEST FOR S_MONTH-LOW.
      PERFORM MONAT_F4.
    SANDEEP JAIN

  • Month to Date and Year to Date Scenarios

    <b>Dear SAP BI Gurus,
    Can anyone please give me guidance how to create a Month to Date and Year To Date Scenarios (Variables perhaps?) for 0SRR_CF_C1?  The date is in decimal and not DATE format…  I’m assuming the reason for this is to get the Time variance….  Nonetheless, I’d like to create a MTD and YTD scenario for reporting.
    Much Thanks
    Philips Manalaysay</b>

    Hi,
    You should take a look at the blog and doc below.
    /people/vikash.agrawal/blog/2006/10/17/enable-trend-reporting-150-by-manipulating-variable-value
    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/25d98cf6-0d01-0010-0e9b-edcd4597335a
    Regards,

  • Using CVS in SQL Developer for Data Modeler changes.

    Hi,
    I am fairly new to SQL Developer Data Modeler and associated version control mechanisms.
    I am prototyping the storage of database designs and version control for the same, using the Data Modeler within SQL Developer. I have SQL Developer version 3.1.07.42 and I have also installed the CVS extension.
    I can connect to our CVS server through sspi protocol and external CVS executable and am able to check out modules.
    Below is the scenario where I am facing some issue:
    I open the design from the checked out module and make changes and save it. In the File navigator, I look for the files that have been modified or added newly.
    This behaves rather inconsistently in the sense that even after clicking on refresh button, sometimes it does not get refreshed. Next I try to look for the changes in Pending Changes(CVS) window. According to the other posts, I am supposed to look at the View - Data Modeler - Pending Changes window for data modeler changes but that shows up empty always( I am not sure if it is only tied to Subversion). But I do see the modified files/ files to be added to CVS under Versioning - CVS - Pending Changes window. The issue is that when I click on the refresh button in the window, all the files just vanish and all the counts show 0. Strangely if I go to Tools - Preferences - Versioning - CVS and just click OK, the pending changes window gets populated again( the counts are inconsistent at times).
    I believe this issue is fixed and should work correctly in 3.1.07.42 but it does not seem to be case.
    Also, I m not sure if I can use this CVS functionality available in SQL Dev for data modeler or should I be using an external client such as Wincvs for check in/ check out.
    Please help.
    Thanks

    Hi Joop,
    I think you will find that in Data Modeler's Physical Model tree the same icons are used for temporary Tables and Materialized Views as in SQL Developer.
    David

  • Arbitrary change of date and year on photos taken in folder structure

    I run Adobe Photoshop Elements 8.0 in a Microsoft Office 2007 environment originally on a Dell laptop. Before installing Adobe I had a library in My Pictures that I have maintained and uses this structure (folders by year and events). I have moved everything to a stationary computer. Now dates has been changed on the pictures pictures from 2001 has been changed to 2010 also in properties). When adding new folders (by year and months) for transfere of pictures from my camera pictures alrready in the files starts populate the folder just established.
    On the old Dell laptop the correct dates and year are still maintained (used as a second back-up).
    What happens?

    Finally got the date fixed, it was simple once I got palm support. I did a hard reset, then set the date but DID NOT click on TODAY at the bottom of the screen, the secret is to tap on the actual DAY on the calendar, NOT TODAY (after you set the year)
    Post relates to: Tungsten E2

  • F4 help for month and Year field in ztable please

    I have created a ztable. 
    The first field in my ztable is a primary key field and it holds MM/YYYY(eg.  08/2011).
    How ever my requirement is when ever we want to see the output, uer wants F4 help on that fields and when he selects the range , the ztable should display the values in that range.
    For eg.
    1). if user selects   01/2011  to 08/2011 using F4 Help,
    2). then the ztables should display all the records in that range.
    Thanks in advance for your help.
    Best regards,Sam
    Moderator message : Spec dumping / Outsourcing is not allowed.  Thread locked.
    Edited by: Vinod Kumar on Aug 8, 2011 11:17 AM

    Hi Sam,
    Create an F4 help using this FM
    l_t_date" has month and date
    l_f_programm = sy-repid.
        l_f_dynnr    = sy-dynnr.
        CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
          EXPORTING
            retfield         = 'Date'"Field name of coloum of value tab
            dynpprog         = l_f_programm
            dynpnr           = l_f_dynnr
            dynprofield      = <>"Screen field name'
            value_org        = 'S'
            callback_program = l_f_programm
            callback_form    = ''
          TABLES
            value_tab        = l_t_date"Value table for date and month
    *      FIELD_TAB        = L_T_RETURN
            return_tab       = l_t_return_tab
          EXCEPTIONS
            parameter_error  = 1
            no_values_found  = 2
            OTHERS           = 3.
        READ TABLE l_t_return_tab INDEX 1.
        IF  sy-subrc = 0 .
        <fields> = l_t_return_tab-fieldval.
        ENDIF.
    Regards,
    Amit
    then filter the ztable accordingly

  • Function Modules for Data and Time

    Hi all,
              I need 2 function modules for date and time. when we pass current data(sy-datum) and current time (sy-uzeit) into function modules, shoud get date in <b>dd/mm/yyyy or dd.mm.yyyy</b> and time in<b> HH:MM:SS</b> formats.
    Thanks in advance

    Hi Ranjith,
    i think this will b usefull for you..
    SAP Bar Chart Function Modules and what they are used for
    Function module
    Used for
    BARC_GRAPHIC_PBO
    Starting bar chart at PBO time, using a graphic profile (parameter PROFILE)
    BARC_GRAPHIC_PAI
    Analyzing data returned by the graphic
    BARC_SET_TIME_AXIS
    Setting start and end of time axis
    BARC_SET_OPTIONS
    Setting options
    BARC_ADD_CHART
    Creating a chart
    BARC_SET_CHART_ATTRIB
    Setting chart attributes
    BARC_ADD_SECTION
    Creating a section on the time axis
    BARC_SET_SECTION_ATTRIB
    Setting section attributes
    BARC_ADD_RIBBON
    Adding a ribbon to the time axis
    BARC_SET_RIBBON_ATTRIB
    Setting attributes for ribbons in the chart
    BARC_ADD_GRID
    Adding a time grid
    BARC_SET_GRID_ATTRIB
    Setting grid attributes
    BARC_ADD_LAYER
    Adding a layer (graphic elements)
    BARC_SET_LAYER_ATTRIB
    Setting layer attributes
    BARC_ADD_LINE
    Adding a line
    BARC_ADD_CALENDAR
    Creating a calendar
    BARC_SET_CALENDAR_ATTRIB
    Setting attributes for a calendar
    BARC_ADD_TIME_PROFILE
    Creating time profiles
    BARC_SET_TIME_PROFILE_ATTRIB
    Setting attributes for time profile
    BARC_ADD_INTERVAL
    Adding a time interval
    BARC_SET_INTERVAL_ATTRIB
    Setting time interval attributes
    BARC_ADD_TIME_OBJECT
    Creating a time object
    BARC_CONVERT_DATE
    Creating a date string in bar chart format
    BARC_REVERT_DATE
    Converting a date string in bar chart format to date and time
    BARC_ADD_DATELINE
    Creating a date line
    BARC_SET_DATELINE_ATTRIB
    Setting dateline attributes
    BARC_GET_PROFILE_CONTENTS
    Obtaining profile contents for customizing a chart
    BARC_GET_COLUMN_WIDTH
    Selecting new column width
    BARC_SET_COLUMN_WIDTH
    Setting the column width
    BARC_GET_TEXTINDEX
    Obtaining the text index of a field
    BARC_SET_LABELS
    Positioning the chart display
    BARC_SET_COLUMN_ATTRIB
    Setting column attributes
    BARC_SET_ROW_ATTRIB
    Setting row attributes
    BARC_SET_ROW_HEIGHT
    Setting the line height
    BARC_SET_MAXCHARTS
    Setting the maximum number of charts sent
    <b>If its usefull reward points
    </b>

  • Tolerance Limits for Date and quantity..??

    Hi Experts,
    We would like to set the GR Tolerance Limits in the Configuration level.
    how to set the Tolerance Limits for Date and quantity..??
    if we created PO 100qty and date 01-05-009.
    if we receive the goods on 20-04-2009 and 50qty...
    For GR .. Migo should be give error message..
    where can i set this Tolerance Limit Configuration.... can u please explin me..
    Thanks in Advance,
    Anthyodaya.

    Hai
    for  quantity tolerence limit  u can do it  while creating  P O  in item details-->delivery tab.
    for date go to SPRO-->mm>purchasing->material master> define purchase value key>in that   st.del.time var               here it wil compare the dates and shows how much deviation is occuring for GR date
    configure messages in SPRO-->mm>environment data--> define attributes of system messages
    hope it may help u
    regards
    chandrasekhar

  • GR  Tolerance Limits for Date and quantity..??

    Hi Experts,
    We would like to set the GR Tolerance Limits in the Configuration level.
    how to set the Tolerance Limits for Date and quantity..??
    if we created PO 100qty and date 01-05-009.
    if we receive the goods on 20-04-2009  and  50qty...
    For GR .. Migo should be give error message..
    where can i set this Tolerance Limit  Configuration.... can u please explin me..
    Thanks in Advance,
    Anthyodaya.

    Hi,
    GR tolerance limits can be set based on the quantity only .It is configuration settings .
    MM-->Inventory Management-->Inventory Management  and physical Inventory --->set tolerance limit/
    Following are the standard settings:
    B1 ( order price quantity variance with error message): 50 %
    B2 (order price quantity variance with warning): 20 %
    VP (variance from moving average price) 25 %
    In case of dates, the tolerance limit settings based on delivery date can be achived by using user exists or create a Z t.code goods receipt copying standard one & write a code to apply your logic while you do the GR.
    Thanks,
    AMIT

  • Split month, date and year

    How to split month,date and year from date field ? is there any standard function module.

    You can do this.
    data: year(4) type c,
          month(2) type c,
          day(2) type c.
    year = sy-datum(4).
    month = sy-datum+4(2).
    day = sy-datum+6(2).
    write:/ year.
    write:/ month.
    write:/ day.
    Regards,
    Rich Heilman

  • To implement search help for date and time fields details

    how can i implement search help for date and time fields in screen painter

    Hi
    Declare the variables as sy-datum and sy-uzeit or any other pre-defined data typ of date and ime types. Serach help will automatically comes.
    Aditya

  • Can I use Microsoft SQL Server Management Studio version 11.0 to write SQL queries for "SQL Server Compact 4.0 Local Database"

    Hi, Can I use Microsoft SQL Server Management Studio version 11.0 to write SQL queries for "SQL Server Compact 4.0 Local Database" ?
    When I use Connect Object Explorer, the "Connect to Server" dialog box which pops up has only 4 selections in the Server Type Drop Down List. They are Database Engine, Analysis Services, Reporting Services & Integration Services. I have read
    somewhere that there should be a compact database option. but I do not see it.
    What I would like to do is use free form SQL Queries against the tables in "SQL Server Compact 4.0 Local Database" .
    Once I have validated these queries, then I will use them in my Visual Studio 2012 C#, ASP.NET application. I created the Local Database using Visual Studio 2012 for use by my application.
    Thank you for your help..
    diana4

    Hello,
    With SSMS 2005 we have had the Option to work with SQL CE database files, but not with higher Version of SSMS.
    You can use the free SQL CE Toolbax instead; see
    http://sqlcetoolbox.codeplex.com/
    Olaf Helper
    [ Blog] [ Xing] [ MVP]

  • Apple TV froze. Unplugged and re plugged, and now it is continuously checking for date and time. If I bypass it by holding menu, I only have a settings icon, I lost everything else, what do I do?

    My Apple TV froze while trying to access Netflix. I unplugged the Apple TV from the wall and re plugged it. Now it stays on the loading time and date setting. After 10 minutes, I held menu, and hit restart (as the troubleshoot said). Same problem is still occurring with the continuous "checking for date and time". And if I hold menu to bypass this, I only have my settings and computer icon, all other icons have disappeared. Not quite sure what to do, any comments would surely help.
    Thanks guys, appreciate it.
    Matt

    Apple TV 1st generation or 2nd?
    I would try plugging in the HDMI from the ATV to a different HDMI port on your TV and even trying the ATV on a different TV altogether...sounds like the HDMI port on your TV might be bad or a bad connection. Keep in mind you'll need to switch the TV input to the new HDMI port

  • Aggregated data for quatrter and year

    Hi,
         I have created a input schedule by using any by any template, I have account on row and time dimension on column, I input some test data for each month for some accounts, it work ok and I send the data successfully.
        I create another report using Evdre with the  row and column structure, I set both member set as "SELF AND DEP", however, when I run the report, I notice that the Quarter data and the year data is not correct, For example, Q1 data should the  summary of Jan,FEB and MAR, instead it show the same data as MAR, while the year data show the same data as Q4.
       Anybody know hot to fix this?
    Thanks

    I did another test as following:
    I copy a appshell from SAP standard AppShell, I generated some test data for each month using input schedule, then I build a evdre report, it work perfect,if i  choose PERIODIC, I get the quarter and year data summary the month or quarter, if I choose YTD, I get the year to date data,
    then I copy the my master data to  replace the orginal master data, I generated some test data, this time the report only show the YTD data no mater I choose PERIODC OR YTD for measure in CV.   This is verystrange.

Maybe you are looking for