How to select dates by year?

Hi,
Lets say I have a table(itemID, datePurchase, itemPrice)
and I want to list the sum of itemPrice based on the year of datePurchase.
eg. I want the total amount spent in the year 2007
DATEPURCHASE ITEMPRICE
04-OCT-07 300
07-NOV-07 250
17-JAN-08 125
16-NOV-08 250
27-NOV-08 250
17-DEC-09 200
How do I filter the date out by year?
Regards,
Keith

Hi,
Welcome to the forum!
If you're hard-coding the target date, you can do something like this:
SELECT     SUM (itemprice)     AS total_price
FROM     table_x
WHERE     datepurchase     >= DATE '2007-01-01'
AND     datepurchase     <  DATE '2008-01-01'
;If the year is in a variable, then you'll want something more like this:
VARIABLE  year_wanted  VARCHAR2 (4)
EXEC     :year_wanted := '2007';
SELECT     SUM (itemprice)     AS total_price
FROM     table_x
WHERE     datepurchase     >= TO_DATE ( year_wanted || '-01-01'
                          , 'YYYY-MM-DD'
AND     datepurchase     <  TO_DATE ( year_wanted || '-12-31'
                          , 'YYYY-MM-DD'
                       ) + 1
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
See the forum FAQ {message:id=9360002}
Edited by: Frank Kulash on Oct 4, 2012 12:18 PM
Added keyword DATE to date literals.

Similar Messages

  • How to select data from a table using a date field in the where condition?

    How to select data from a table using a date field in the where condition?
    For eg:
    data itab like equk occurs 0 with header line.
    select * from equk into table itab where werks = 'C001'
                                                      and bdatu = '31129999'.
    thanks.

    Hi Ramesh,
    Specify the date format as YYYYMMDD in where condition.
    Dates are internally stored in SAP as YYYYMMDD only.
    Change your date format in WHERE condition as follows.
    data itab like equk occurs 0 with header line.
    select * from equk into table itab where werks = 'C001'
    and bdatu = <b>'99991231'.</b>
    I doubt check your data base table EQUK on this date for the existince of data.
    Otherwise, just change the conidition on BDATU like below to see all entries prior to this date.
    data itab like equk occurs 0 with header line.
    select * from equk into table itab where werks = 'C001'
    and <b> bdatu <= '99991231'.</b>
    Thanks,
    Vinay
    Thanks,
    Vinay

  • How to select data from a table by passing document number from another tab

    How to select data from a table by passing document number from another table.
    for eg:-
    I want to display name, adres, region from ADRC table
    by using field delivery document number
    Kind Regards,
    Shanbagavalli.S

    Hi Shanbagavalli,
    There are multiple solutions to this questions a few i will try to answer and then you can take the best required for your requirements.
    **Consider that you have a Internal table having document number from other table..
    SELECT NAME ADRES REGION FROM ADRC
           INTO IT_ADRC
           FOR ALL ENTRIES IN IT_DOC
           WHERE DOCUMENT_NO = IT_DOC-DOCUMENT_NO.
    **Consider that you have 1 document number then
    SELECT NAME ADRES REGION FROM ADRC
         INTO IT_ADRC
         WHERE DOCUMENT_NO = W_DOCUMENT_NO.
    Hope this solves your problem.
    Regards,
    Kunjal

  • How to select data from cluster table

    hi experts,
                   I have a report which picks data from bseg (cluster table ) for a month report it is taking around 4 minutes to process.I feel it is not good when take the report after some months.
    how to select data from these table???how to declare itab for these cluster tables????can we include any search condition or any other kind of internal table???
    please advice.
    mani

    Hi Manikandan,
    The following code may be helpful to understand how to select the data from cluster table.
    Types: Begin of ty_kna1,
    Kunnr type kna1-kunnr,
    adrnr type kna1-adrnr,
    end of ty_kna1,
    begin of ty_bseg,
    belnr type bseg-belnr,
    kunnr type bseg-kunnr,
    end of ty_bseg,
    begin of ty_final,
    belnr type bseg-belnr,
    kunnr type kna1-kunnr,
    adrnr type kna1-adrnr,
    end of ty_final.
    Data: it_kna1 type table of ty_kna1,
    wa_kna1 type ty_kna1,
    it_bseg type table of ty_bseg,
    wa_bseg type ty_bseg,
    it_final type table of ty_final,
    wa_final type ty_final.
    Select kunnr adrnr from kna1 into table it_kna1 where....
    if sy-subrc = 0.
    select belnr kunnr into table it_bseg for all entries in it_kna1 where kunnr = it_kna1-kunnr.
    endif.
    sort it_kna1 by kunnr.
    Loop at it_bseg into wa_bseg.
    move wa_bseg-belnr to wa_final-belnr.
    read table it_kna1 into wa_kna1 with kunnr = wa_bseg-kunnr binary search.
    if sy-subrc = 0.
    move: wa_kna1-kunnr to wa_final-kunnr,
    wa_kna1-belnr to wa_final-belnr.
    endif.
    append wa_final to it_final.
    clear wa_final.
    endloop.
    Loop at it_final into wa_final.
    write: / wa_final-belnr, wa_final-kunnr, wa_final-adrnr.
    endloop.
    Reward if useful.
    Thankyou,
    Regards.

  • How to select data from structure

    how to select data from structure

    Hi Laxman,
    Structure holds single record of data. You can acess data by using the syntax <b>Structure-field</b>
    Please see sample.
    data: wa_mara  type mara.
    select *
              from  mara
              into wa_mara
              up to 1 rows.
    endselect.
    write:/ wa_mara-mandt,
             wa_mara-matnr,
             wa_mara-maktl.
    if helps plz reward points.
    Regards
    Bhupal Reddy

  • How to select data from Maintance View in Program

    Dear All ,
    How to select data from Maintance View V_T052 in abap Program.
    Regards,
    Archana

    TABLES: t179, t179t.
    DATA: v_t179_int TYPE TABLE OF v_t179.
    SELECT * FROM t179
      JOIN t179t ON
        t179prodh = t179tprodh
      INTO CORRESPONDING FIELDS OF TABLE v_t179_int.
    Reward points...

  • How to select date while Updating record

    I Have created Validation through user api hook on Performance Management Module.but when user enter objective ,if is greater then or less then hundred then system raise error as per policy,but when user enter another objective to meet 100 percent then system raise error "hr object lock " because i am using select statement to sum of that object ,any body idea how to select date while inserting or updating any record.
    Thansks.

    Hi,
    drag and drop a af:convertDateTime on top of the select inputDate component
    <h:form>
    <af:selectInputDate label="Label 1">
    <af:convertDateTime pattern="yyyy-MM-dd hh:mm"/>
    </af:selectInputDate>
    </h:form>
    This adds the current time to the selected dat. In JDeveloper 11 you will be able to explicitly change the time in the calendar
    Frank

  • Select date whithout year.

    how can I filter a date without year, considering only the day and the month.
    Example: SELECT * FROM KNVK WHERE GBDAT BETWEEN '%0101' AND '%0131'  to select the contacts whom was born in january. This instruction doesn't work.
    Any one has a suggestion?

    Ypu can use:
    WHERE endda  LIKE '____0101'.
    f [NOT] LIKE g
    Addition:
    ... ESCAPE h
    Effect
    The condition is met for a table entry if the statement "f (does not) equal the pattern in g" is true for the values of f and g. f must always be a field descriptor, and g an ABAP field or an ABAP string. If f has the value NULL, then the result of the check for the statement is unknown. Within a pattern, there are two special characters:
    '_' (underscore) stands for any single character.
    '%' (percentage sign) stands for any sequence of characters, including an empty string.
    Lucky

  • How to select data from an internal table

    material                          norm                    date last modified
    B2-SP HEAT                 50.000               20090420
    BF COKE                                 575.000               20090419
    GROSS COKE                 200.000               20090419
    B2-SP HEAT                 100.000               20090419
    TWT                                 33.000               20090330
    B7-SP HEAT                 2.000               20090310
    B1-SP HEAT                 1.000               20090309
    B7-SP HEAT                 615.000               20090308
    B2-SP HEAT                 585.000               20090308
    B1-SP HEAT                 100.000               20090308
    B3-SP HEAT                 610.000               20090308
    BF COKE                                 68.500               20090308
    GROSS COKE                 72.600               20090308
    B8-SP HEAT                 600.000               20090308
    B9-SP HEAT                 625.000               20090308
    BX-SP HEAT                 615.000               20090308
    B9-SP HEAT                 58.000               20090307
    B1-SP HEAT                 100.000               20090307
    B6-SP HEAT                 350.000               20090306
    B2-SP HEAT                 888.000               20090306
    Like above there r numerous data in a table :
    how will i select data into another internal table where material above is not repeated with latest modified date.please help.

    Hi Sonu,
    The main task is to move the contents of the one internal table to another with some condition.
    First sort and delete the duplicate entries from the First Internal table like below : 
    sort it_tab by material ascending date_modified descending.
    delete adjacent duplicates from it_tab.
    Then move that Internal table contents to another internal table.
    Define another internal table with the same structure as you have first internal table and then
    Second Step :
    it_itab1 = it_itab.
    If you are using seperate Header line and Body then you can do like below :
           it_itab1[] = it_itab[].
    This will fix the issue.
    Please let me know if you need any further explonation.
    Regards,
    Kittu
    Edited by: Kittu on Apr 24, 2009 12:21 PM

  • How to Select data using same column name from 3 remote database

    Hi,
    Can anyone help me on how to get data with same column names from 3 remote database and a single alias.
    Ex.
    SELECT *
    a.name, b.status, SUM(b.qty) qantity, MAX(b.date) date_as_of
    FROM
    *((table1@remotedatabase1, table1@remotedatabase2, table1@remotedatabase3)a,*
    *(table1@remotedatabase1, table1@remotedatabase2, table1@remotedatabase3)b)*
    WHERE b.dept = 'finance'
    AND a.position = 'admin'
    AND a.latest = 'Y' AND (b.status <> 'TRM') AND b.qty > 0;
    GROUP BY a.name, b.status ;
    NOTE: the bold statements is just an example of what I want to do but I always gets an error beacause of ambiguous columns.
    Thanks in advnce. :)
    Edited by: user12994685 on Jan 4, 2011 9:42 PM

    user12994685 wrote:
    Can anyone help me on how to get data with same column names from 3 remote database and a single alias.Invalid. This does not make sense and breaks all scope resolution rules. And whether this is in a single database, or uses tables across databases, is irrelevant.
    Each object must be uniquely identified. So you cannot do this:
    select * from (table1@remotedatabase1, table1@remotedatabase2, table1@remotedatabase3) a3 objects cannot share the same alias. Example:
    SQL> select * from (dual, dual) d;
    select * from (dual, dual) d
    ERROR at line 1:
    ORA-00907: missing right parenthesisYou need to combine the objects - using a join or union or similar. So it will need to be done as follows:
    SQL> select * from (select * from dual d1, dual d2) d;
    select * from (select * from dual d1, dual d2) d
    ERROR at line 1:
    ORA-00918: column ambiguously definedHowever, we need to have unique column names in a SQL projection - so the join of the tables need to project a unique set of columns. Thus:
    SQL> select * from (select d1.dummy as dummy1, d2.dummy as dummy2 from dual d1, dual d2) d;
    DUM DUM
    X   X
    SQL> I suggest that you look closely at what scope is and how it applies in the SQL language - and ignore whether the objects referenced are local or remote as it has no impact to fundamentals of scope resolution.

  • How to select data before a date

    in previous I need to select data from invoice between date 01-10-2007 and 01-11-2007
    it is work fine.
    select sum(invoice_total) total from invoice
    where com_id = 356
    and invoice_date between TO_DATE('01-10-2007','DD-MM-YYYY') and ADD_MONTHS(TO_DATE('01-10-2007','DD-MM-YYYY'), 1)
    NOW, I need to select from data invoice before 01-10-2007.
    How is the statement?
    Thanks

    please refer to the < operator.
    Sybrand Bakker
    Senior Oracle DBA

  • How to select data from one nested table and into another nested table

    create or replace
    TYPE ctxt_code_rec as object
    ctxt_header varchar2(10),
    header_description varchar2(300),
    status varchar2(30),
    adjacent_code varchar2(300),
    adjacent_desc Varchar2(400),
    adjacent_flag varchar2(4000),
    adjacent_text_href varchar2(4000)
    create or replace
    type ctxt_code_table as table of CTXT_CODE_REC
    d_table ctxt_code_table ;
    v_tab ctxt_code_table ;
    Iam trying to select data from d_table to v_tab
    using and bulk collect into
    select m.*
    bulk collect into p_code_result
    from table(l_loop_diag_code_table1)m
    order by 1;
    Receiving error:
    ora 94007 : not enoughvalues
    Could you please let me know how to solve it?
    Thanks,
    in advance

    >
    create or replace
    TYPE ctxt_code_rec as object
    ctxt_header varchar2(10),
    header_description varchar2(300),
    status varchar2(30),
    adjacent_code varchar2(300),
    adjacent_desc Varchar2(400),
    adjacent_flag varchar2(4000),
    adjacent_text_href varchar2(4000)
    create or replace
    type ctxt_code_table as table of CTXT_CODE_REC
    d_table ctxt_code_table ;
    v_tab ctxt_code_table ;
    Iam trying to select data from d_table to v_tab
    using and bulk collect into
    select m.*
    bulk collect into p_code_result
    from table(l_loop_diag_code_table1)m
    order by 1;
    Receiving error:
    ora 94007 : not enoughvalues
    Could you please let me know how to solve it?
    >
    Not unless you provide the code you are actually using.
    There is no definition of 'p_code_result' in your post and you say you 'trying to select data from d_table' but there is no code that loads 'd_table' in what you posted.
    And the SELECT query you posted actuall selects from an object named 'l_loop_idag_code_table1' which isn't mentioned in your code.
    Post the actual code you are using and all of the structures being used.
    Also explain why you even need to use nested tables and PL/SQL for whatever it is you are really doing.

  • How to select data in excel

    Hi,
    I am writing a program which selects data from MS Excel, below is the excel sheet colums
    A     B     C
    x     y     z
    When I run the below program i get an error for the line :
    ResultSet rs = stmt.executeQuery("select B from [Sheet1$] where A=x");
    Connection Failedjava.sql.SQLException: [Microsoft][ODBC Excel Driver] Too few parameters. Expected 1.
    Can you please advice how to fix this code ?
    package app;
    import java.sql.*;
    public class TestExcel {
    public TestExcel() {
    public static void main(String[] args) {
    try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Excel Files;DBQ=C:\\excel\\expenses1.xls; READONLY=0;", "", "");
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("select B from [Sheet1$] where A=x");
    while (rs.next()){
    String a = rs.getString(1);
    System.out.println(a);
    catch (Exception e) {
    System.out.println("Connection Failed" + e);
    Thx

    That error is the driver's way of saying that you have used table names that don't exist in the database, or column names that don't exist in the table. There's only one such erroneous name in the query.

  • How to select  data from cube by BAPI

    Hi
    now i want to select data from cube by selection
    and the parameter is BAPI'S import.
    I want to know how can i select data from which table the storage  the cube's data
    Thank you for your help !!

    Hi,
    You can use FM: RSDRI_INFOPROV_READ.
                               BAPI_CUBE_GETDETAIL
    Pls Check the below weblog
    /people/dinesh.lalchand/blog/2006/06/07/reading-infocube-data-in-updatetransfer-rules
    Regards,
    CSM Reddy

  • How to select data

    material            norm        date last modified
    B2-SP HEAT     50.000     20090420
    BF COKE     575.000     20090419
    GROSS COKE     200.000     20090419
    B2-SP HEAT     100.000     20090419
    TWT     33.000     20090330
    B7-SP HEAT     2.000     20090310
    B1-SP HEAT     1.000     20090309
    B7-SP HEAT     615.000     20090308
    B2-SP HEAT     585.000     20090308
    B1-SP HEAT     100.000     20090308
    B3-SP HEAT     610.000     20090308
    BF COKE     68.500     20090308
    GROSS COKE     72.600     20090308
    B8-SP HEAT     600.000     20090308
    B9-SP HEAT     625.000     20090308
    BX-SP HEAT     615.000     20090308
    B9-SP HEAT     58.000     20090307
    B1-SP HEAT     100.000     20090307
    B6-SP HEAT     350.000     20090306
    B2-SP HEAT     888.000     20090306
    Like above there r numerous data in a table :
    how will i select data into another internal table where material above is not repeated with latest modified date.please help.

    Hi Arindam,
    You have to call the FM NUMBER_GET_NEXT.
    DATA : l_num TYPE char8.
    CALL FUNCTION 'NUMBER_GET_NEXT'
    EXPORTING
    nr_range_nr                   = '01'  "Check it in SNRO for your scenario
    object                        = 'ZVLEMDFE'
    IMPORTING
    NUMBER                                                 = l_num
    EXCEPTIONS
    INTERVAL_NOT_FOUND            = 1
    NUMBER_RANGE_NOT_INTERN       = 2
    OBJECT_NOT_FOUND              = 3
    QUANTITY_IS_0                 = 4
    QUANTITY_IS_NOT_1             = 5
    INTERVAL_OVERFLOW             = 6
    BUFFER_OVERFLOW               = 7
    OTHERS                        = 8.
    You'll get the value in l_num.
    Regards,
    Manish.

Maybe you are looking for