Oracle 10g Query on Date Column

Hello -
What is the most efficient way to query on a date column to get all dates within the last 2 months? I use something like the following in the WHERE clause:
billing_date >= to_date(add_months(sysdate, -2))
However, I can't ever get the index on billing_date to be used.
Any help is greatly appreciated...
Thanks!!

This is a perfectly valid way to query the data. Here's an example on my own production tables. This one has about 300 million rows:
select *
from   prod.tran_history
where  tran_date >= to_date(add_months(sysdate, -2))
| Id  | Operation                   |  Name                        | Rows  | Bytes | Cost  |
|   0 | SELECT STATEMENT            |                              |    11M|   863M|   146K|
|   1 |  TABLE ACCESS BY INDEX ROWID| TRAN_HISTORY                 |    11M|   863M|   146K|
|*  2 |   INDEX RANGE SCAN          | TRAN_HIST_DATE_IDX           |  2144K|       |  2098 |
--------------------------------------------------------------------------------------------

Similar Messages

  • Oracle 10g query rows to column transpose

    In oracle 10g I have table called LoginUser
    Select * from LoginUser where Day = '01-Dec-2012'
    will give me the below result
    Day, Name, Country
    1-Dec-12 , John, UK
    1-Dec12 , Cate, US
    1-Dec-12 , Prakash, India
    1-Dec-12 ,Juli, Pak
    But I want result as below
    Day, Name1, Country1, Name2, Country2, Name3, Country3, Name4, Country4
    1-Dec-12, John, UK, Cate, US, Prakash, India, Juli, Pak
    Please help
    Edited by: 975340 on Dec 6, 2012 1:48 AM
    Edited by: 975340 on Dec 6, 2012 1:50 AM

    Refer to below thread
    Re: Multiple rows into a single line in 'Single Column Table'

  • Is there any option in oracle 10g to rename a column?

    Hi friends,
    Is there any option in oracle 10g to rename a column? Please send me reply....Waiting for that...

    You can even rename a column in oracle 8i or 9i
    E.g:
    ALTER TABLE TABLE_NAME RENAME COLUMN OLD_COLUMN_NAME TO NEW_COLUMN_NAME;Message was edited by:
    Mohana Kumari

  • Oracle 10g Query Format

    0
    down vote
    favorite I am using oracle 10g. I have a temp table TEMP.
    TEMP has following structure:-
    USER COUNT TYPE
    1 10 T1
    2 21 T2
    3 45 T1
    1 7 T1
    I need a query which will show all types has column names,and types can be any value like T1,T2,..Tn and columns will be like:-
    USER T1 T2 Tn
    and User column will show all the users and T1,T2 column will show total count of types.
    Edited by: 969468 on Nov 15, 2012 9:35 PM

    Please see my original post ("I have a page erroring in production (but not in development) when it gets to a date that it needs to display.")  Sorry if it came across as vague.  It made sense, to me, when I typed it.  But, then, I'm usually typing fast just to get the question out there, when I'm in a hurry.
    Haven't done a CFDUMP, yet, as every time I make a change in development that needs to be tested in production, I have to notify my supervisor that there are files that need to be copied into production, which can sometimes take a while, so I try to do troubleshooting on dev side - it's a pain in the you-know-what, but that's the kind of environment I'm working in.
    As it turns out, changing the format in the SELECT to_char() did the trick.  If anyone else has this issue with Oracle 10g, I'm now using SELECT to_char(create_date,'YYYY/MM/DD HH:MI') FROM tableA, and now the CFOUTPUT is processing the whole page.  I guess the MM-DD-YYYY threw CF into a tizzy, breaking the process?
    Anyhoo, it's working, now.  Thank you, Dan and Adam, for your thoughts on this.
    ^_^

  • Oracle 10G query

    I have following queries to be solved please help me out....
    1) Difference / Limitation of Oracle Standard Edition / Enterprise Edition 10..... latest ?
    2) Can Data of Oracle 8. ..... Expo File will be Imported to 10....... latest ?
    If not than what steps we have to take. Basically I would like my current DB of 8 to be moved to 10G
    3) From which AIX Ver Oracle 10 ...... is Supported ?
    4) if the customer has 8... Ver Lic When if goes for New Ver than media will have full Ver or only Upgrade files. ?
    5) If the Customer has Old ver Lic and he goes for upgrade Lic Than we have to first Install Old Lic Ver & than Upgrade or directly we can load Latest Lic Ver on New Server and work. ? What I mean is if I have to go ahead from Oracle 8 to Oracle 10G how should I go about.
    6) Is Oracle Media ver Compatible for Aix ?
    7) Oracle Lic Policy / Product Information Web Link ?
    Thanks & Regards
    Shivanand S S

    This is a perfectly valid way to query the data. Here's an example on my own production tables. This one has about 300 million rows:
    select *
    from   prod.tran_history
    where  tran_date >= to_date(add_months(sysdate, -2))
    | Id  | Operation                   |  Name                        | Rows  | Bytes | Cost  |
    |   0 | SELECT STATEMENT            |                              |    11M|   863M|   146K|
    |   1 |  TABLE ACCESS BY INDEX ROWID| TRAN_HISTORY                 |    11M|   863M|   146K|
    |*  2 |   INDEX RANGE SCAN          | TRAN_HIST_DATE_IDX           |  2144K|       |  2098 |
    --------------------------------------------------------------------------------------------

  • Query a date column in consideration of daylight saving zone

    Hi!
    I have a database (oracle 9) column with dates that have an offset of one or two hours, one hour when the date is in winter time and two hours when the date is in summertime. I want to query the dates from the table and correct the date at the same time.
    Can anybody help me with this issue?
    Thx
    Christoph

    Hello
    It would be helpful if you could provide a small test case that shows the data you have and the result you expect. For example...
    WITH src AS
    (  SELECT TO_DATE('15/08/2011 12:00','dd/mm/yyyy hh24:mi') a_date, 1 offset from dual union all
        SELECT TO_DATE('15/08/2011 12:00','dd/mm/yyyy hh24:mi') a_date, 2 offset from dual union all
        SELECT TO_DATE('15/08/2011 23:59','dd/mm/yyyy hh24:mi') a_date, 1 offset from dual union all
        SELECT TO_DATE('28/02/2011 23:59','dd/mm/yyyy hh24:mi') a_date, 1 offset from dual
    SELECT
        a_date,
        offset,
        a_date + numtodsinterval(offset,'HOUR') new_date
      FROM
            srcHTH
    David
    Edited by: Bravid on Aug 15, 2011 11:05 AM

  • How to archive and Purge in Oracle 10g based on data

    Hi,
    Our requirement is that based on some rules stored in static tables we want to perform Archiving, purging and compression of data in Oracle 10g.
    Can anyone guide me to proper information material for how to go about this?
    Thanks!
    Avinash.

    Hi,
    Thanks for the information.
    But this traditional way of doing is archiving is for maintenance and backup operations.
    We want to have this process online, without taking db offline. In this case will this approach work?
    In our case, the rules can be like -
    1. For table 'A', if rows exceed 10Million, then start archiving of the data for that table.
    2. For table 'B', if data is older than 6 motnhs start archiving of the data for that table.
    3. Archiving should be on for 15 minutes only after that should pause, and should resume whenever user wants to resume.
    4. Archiving should start on specified days only... ETC...

  • Does Oracle 10g support Active Data-guard?

    Hi all,
    Can Oracle Database 10g support Active Data-guard?
    Thanks you!
    Dan
    Edited by: Dan on Jul 12, 2011 10:13 AM

    no, active data guard is 11gR1 new feature.
    In Oracle Database 10g and below you could open the physical standby database for read-only activities (say, to offload some reporting), but only after stopping the recovery process. With these releases, If Data Guard is a part of your DR solution, you really can't afford to pause the recovery process for a long duration for fear of falling behind, so the physical standby database is essentially useless for any read-only activity.
    With Oracle Database 11g, that situation changes: You can open the physical standby database in read-only mode and restart the recovery process. This means you can continue to be in sync with primary but can use the standby for reporting. (As in previous versions, you can take the backup from the standby as well.)
    refer:- http://www.oracle.com/technetwork/articles/sql/11g-dataguard-083323.html

  • Querying a date column

    hi
    need help for a simple query. while quering a date column in forms6i. i am not able to retrive the data for that particular date.
    details:
    Data Type = Date
    Format Mask = DD-MON-YYYY HH24:MI:SS
    Max Length = 20
    when i enter the date (21-JUL-2008), the time comes by default as 00:00:00. I am able to query with a date value Eg: '01-JAN-2008 00:00:00' for which it retives. i presume it is taking the time as 00:00:00 for all dates. i need to retive all the details for that particular date for all time. how should i handle this?
    Thanks in advance

    If you have an entry in the date field when you execute the query then the records retrieved will have to match that field. This is often unsuitable for datetime fields so you can do this to get round it:
    Create a new datetime* item in a control block.
    Include the new item in the main block's where clause property:
    (trunc(date_column) = :control.date_item or :control.date_item is null)Add code to the main block's pre-query trigger to set the new item and the item in the main block:
    if trunc(:block.date_column) = :block.date_column then
         ::control.date_item := :block.date_column;
         :block.date_column := null;
    else
         ::control.date_item := null;
    end if;Now when the user enters a date and time the form will retrieve records which match exactly, but when they enter just a date (or they enter the time as midnight) the form will fetch all records for that day.
    You could also use forms' default Query/Where functionality but this is more difficult for end users and is a potential security issue
    http://www.red-database-security.com/wp/sql_injection_forms_us.pdf
    *I use forms 9.0.4 and date items are ok with time components but after Rosario's post I'll assume you need a datetime in 6i.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Convert Oracle SQL query to single column output

    Hello All,
    I need to build the query to have multiple columns in a single column with multiple rows.
    select a.customer_trx_id,a.previous_customer_trx_id
    from ra_customer_trx_all a
    where a.customer_trx_id = :customer_trx_id
    here, a.customer_trx_id and a.previous_customer_trx_id are in two columns. I need to bring them into a single column.
    Say: the above output is
    a.customer_trx_id a.previous_customer_trx_id
    123456 87654
    Need to have single column
    As
    123456
    87654
    Please do the needful.
    Thanks,
    Abdul

    Hi,
    Post your question in [SQL and PL/SQL|http://forums.oracle.com/forums/forum.jspa?forumID=75] forum, you would probably get a better/faster response.
    Regards,
    Hussein

  • Oracle DB Query Generating Invalid Column Index error

    Dear All,
    Apologies for a question on this rather basic error but I don't understand why i'm experiencing the behaviour described.
    The piece of code below connects to a database, runs a query, then attempts to display the contents of the resultset. I understand from other posts on the topic that the query I am executing (INSERT INTO table VALUES (...)) as in the comments below needs to have autocommit disabled to allow me to access the resultset without the transaction closing.
      public void runQuery(String query) throws Exception {   Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con = DriverManager.getConnection(database, username, password); con.setAutoCommit(false); Statement st = con.createStatement();         ResultSet rs = st.executeQuery (query);         // "select * from user_tables"         // "INSERT INTO BH VALUES ('','2.25','Dark Red','Samsonite','4987638762')"         while (rs.next()) {         System.out.print (rs.getString(1)); // Exception thrown here     }         con.commit(); st.close();   con.close();   }
    I have removed try {} catch {} error handling for clarity.
    I am receiving the invalid column index error from the indicated line, which clearly suggest column 1 doesn't exist, however if i run the same query in another tool, something like the following is returned:
    Rows Affected
    1
    The query is being received by the database as the row ends up in the table intact, so I am clearly missing something about how to access the resultset correctly.
    I am guessing that I am either misusing/misunderstanding the autocommit option or that the resultset is different to what I am expecting...
    Can someone please shed a little light?
    Many thanks!
    George

    Ok, I've done a little more digging, and i've come to the conclusion that an INSERT query doesn't return a resultset (or returns an empty one)
    Could someone with a little more knowledge confirm this as I've had little luck finding an explicit yes or no from the web!
    Many Thanks in advance...

  • Oracle 10g - query is very slow in jdbc but not in other tools

    I'm using PreparedStatement to execute a query with some parameters.
    in sql developer, it just spends 12 seconds but in jdbc, it's around 140 seconds.
    I disabled "_optim_peek_user_binds" parameter already but it didn't help.
    alter session set "_optim_peek_user_binds"=false;

    Please mark this thread ANSWERED and repost it in the JDBC forum.
    https://forums.oracle.com/forums/category.jspa?categoryID=288
    When you post provide your 4 digit Oracle version, Java version and JDBC jar name and version as well as the code you are using.

  • Oracle 10g - Problem with Date Ranges using Between

    I am keeping track of patients who have not been contacted during a date range. if a nurse adds an event or note and the note is type 1,3,4 then this is a contact. The following works but if the event or note was made on the same day as the report is run, the current contact is not printed. Any help to improve the query and identify the problem would be appreciated. Also, if you add '1' to' n.created_date_time BETWEEN '10-Jan-2010' AND TRUNC(SYSDATE)' so it is ' n.created_date_time BETWEEN '10-Jan-2010' AND TRUNC(SYSDATE) + 1'. It works. What is wrong?
    SELECT upper(symptom_text),
      COUNT(UNIQUE(c.patient_id)) ,
      COUNT(UNIQUE(
      CASE
        WHEN e.eventdate BETWEEN '10-Jan-2010' AND TRUNC(SYSDATE)
        OR (n.created_date_time BETWEEN '10-Jan-2010' AND TRUNC(SYSDATE)
        AND n.note_type_id                                             IN (1,3,4))
        THEN c.patient_id
        ELSE 0
      END)) - 1
    FROM patient c,
      cust_info ci,
      event e,
      note n
    WHERE c.physician_id = 74
    AND c.patient_id      = ci.patient_id
    AND ci.info_type_id    = 32
    AND ci.symptom_text      IS NOT NULL
    AND c.patient_id      = e.patient_id(+)
    AND c.patient_id      = n.pk_id(+)
    AND n.table_name(+)    = 'patient'
    GROUP BY upper(symptom_text)
    ORDER BY DECODE(upper(symptom_text), 'A+', 1, 'A', 2, 'B', 3, 'C', 4, 'D', 5, 99)I suspect the end date is not inclusive. Fields are Date data types. Thanks for any help.

    Hi,
    achtung wrote:
    Understood. Frank was correct. Do you mean about the dates? Is that issue solved now?
    It would help a lot if you posted some sample data (CREATE TABLE and INSERT statements) and the results you want from that data. Simplify as much as possible. For example, if everything involving the e and n tables is working correctly, forget about them for now. Post a question that only invloves the c and ci tables.
    But, additionally, when a record in the cust_info doesn't exist my contact count is inaccurate. How can you explain this conceptually. I understand the query path is checking for this record due to the predicate. Perhaps the query should be redesigned. Thanks for your input!Again, you can see the results, and you know what they should be. Nobody else has that information. Please post some sample data and the results you want from that data.
    In your earlier message you said
    Could there be a problem if a record does not exist in the ci table?The condition:
    AND ci.symptom_text IS NOT NULL
    filters for this right? Maybe this could be part of the problem. why I'm not seeing records when a note is added to a patient's doc. How would this be technically be explained? You're doing a inner join between c and ci:
        AND c.patient_id      = ci.patient_id
        AND ci.info_type_id   = 32
        AND ci.symptom_text   IS NOT NULL ...Rows from c will be included only if there is a row in ci with the same patiend_id: even more, that matching row in ci must also have info_type_id=32 in a non-NULL symptom_text, otherwise, the row from c will be ignored.
    If you want rows from c to be included even if there is no matching row in ci, then do an outer join, like you're already doing with the e and n tables:
      WHERE c.physician_id = 74
        AND c.patient_id         = ci.patient_id (+)
        AND ci.info_type_id (+)  = 32
        AND ci.symptom_text (+)  IS NOT NULL
        AND c.patient_id         = e.patient_id(+)
        AND c.patient_id         = n.pk_id(+)
        AND n.table_name(+)      = 'patient'

  • ORacle 10g - Display database data, edit it and write back to database

    Hi!
    I would like to find an application which allows me to read data from a database (object-relational database schema), display it visually, make it editable and offer me the opportunity to write changed data to the database.
    Does anyone have experience with Visio or some other tool?
    Timo

    Hi,
    thank you for your replies!
    I am not sure if the mentioned applications really are what I am looking for. It seems to me that they are developing tools rather than presentation and editing tools. I don't want to present or change the schema but the data of the database. The objects in my database are business processes and the user should be able to get all the information he/she desires about them, be able to edit them and also to create new processes. It's more like a GUI for end users.
    Timo

  • Oracle sql query with data aggregation

    Hi,
    I need help to aggregate sql result as given below
    Result what I get :
    Project Id | Project Name | Employee ID | Task Count
    1 :::::::::::| Project01 ::::::| 254 :::::::::::::| 20
    1 :::::::::::| Project01 ::::::| 266 :::::::::::::| 32
    1 :::::::::::| Project01 ::::::| 654 :::::::::::::| 12
    2 :::::::::::| Project02 ::::::| 741 :::::::::::::| 54
    2 :::::::::::| Project02 ::::::| 266 :::::::::::::| 31
    3 :::::::::::| Project03 ::::::| 877 :::::::::::::| 8
    3 :::::::::::| Project03 ::::::| 955 :::::::::::::| 19
    Result should come like this :
    Project Id | Project Name | Employee ID | Task Count
    1 :::::::::::| Project01 ::::::| 254 :::::::::::::| 20
    ::::::::::::::| ::::::::::::::::::::| 266 :::::::::::::| 32
    ::::::::::::::| ::::::::::::::::::::| 654 :::::::::::::| 12
    2 :::::::::::| Project02 ::::::| 741 :::::::::::::| 54
    ::::::::::::::| ::::::::::::::::::::| 266 :::::::::::::| 31
    3 :::::::::::| Project03 ::::::| 877 :::::::::::::| 8
    ::::::::::::::| ::::::::::::::::::::| 955 :::::::::::::| 19
    I need to know whether this is possible using only sql and how to do ? (Please ignore the colons)
    Thanks

    please learn how to use tags
    [code]
    Project Id | Project Name | Employee ID | Task Count
    1          | Project01    | 254         | 20
               |              | 266         | 32
               |              | 654         | 12
    2          | Project02    | 741         | 54
               |              | 266         | 31
    3          | Project03    | 877         | 8 
               |              | 955         | 19
    [/code]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Maybe you are looking for

  • Custom Page Sizes - How to change from centimeters to Inches?

    Hello, I'm working on some panoramic prints to an Epson R2400 and I need to set up a custom paper size for printing to roll paper. When I select Custom Page Sizes from the Page Setup dropdown, all my measurements are forced into centimeters. Even if

  • Blocking creation of Return PO to vendor

    Vendor owes us credit note on return stock. Debit amount in excess of $2000. Is there a way to block Users from creating a Return PO back to the same vendor but allow creation of normal standard PO. Is there a alternative way to release the block onc

  • HT1386 Itunes wont let my iphone 4 sync?

    Recently got the Iphone 4 as a present, and i've already set it up but when i plug it onto my computer, it shows on the screen of my iphone its connected to itunes but then itunes tells me to restore the phone to its original settings before i sync i

  • Mass Upload doesn't work

    Hi everybody, i am trying to upload user photos via mass upload iview. Therefore i create a folder (c:\photos). In this folder i have two files a)  mapping.properties.txt with the line: Anton=1011 b) 1011.jpg I use the iView for Massupload from conte

  • Freezed - Restoring the Window Layout

    Help! My fcpx is freezing when opening.   Take a look at the screen shot. I'm using an external 2TB WD Harddrive and have not had this problem before.  I recently installed the Mavericks latest update, maybe this has something to do with it? Thanks f