Need suggestions on date range query

I have a requirement to show the amount of product remaining. There is a table that holds updated "inventory" amounts with a date and tonnage, and a series of transactional tables that detail the individual disbursements from the stockpile. The trick is that the dates for the inventory adjustments may not all be the same, meaning that I need to individually resolve the stockpiles.
This query will give me the inventory disbursements:
select FN_STN_KEY(j.FACTORY_ID, j.STATION_ID) as STATION,
  count(j.LOAD_JOB_ID) as LOADS,
           CASE SUM(w.SPOT_WEIGHT)
             WHEN 0 THEN SUM(NVL(j.MAN_SPOT_WT,0))
             ELSE SUM(w.SPOT_WEIGHT)
           END TONS
       from TC c, TC_LOAD_JOBS j, SPOT_WEIGHTS w
      where c.TC_ID = j.TC_ID
        and c.DATE_INDEX = w.DATE_INDEX and j.LOAD_RATE_ID = w.LOAD_RATE_ID
        and c.DATE_INDEX BETWEEN to_date('09/01/2009','MM/DD/YYYY') and sysdate
        and FN_STN_KEY(j.FACTORY_ID, j.STATION_ID) in (810,410)
      group by FN_STN_KEY(j.FACTORY_ID, j.STATION_ID);Note that the date and the list of stations in the where clause are dynamic and selected by user. If this was only one station at a time it wouldn't be this complicated.
This query will give me the last known inventory amount:
select to_char(MAX(AS_OF_DT),'Mon DD, YYYY'), TONS
  from STATION_LOG
      where AS_OF_DT < sysdate and STN_KEY in (810,410) group by TONS;Again, the date and list of stations are selected by user. They should be identical to those selected for the other query.
Does anyone have any good ideas on how to combine these two statements into a single report?
Note: FN_STN_KEY acts as a join function. You don't really want me to get into why there isn't a single unique key to reference.

Hi,
I'm trying to follow your descrioption, but lots of things don't make sense to me.
blarman74 wrote:
Yeah. I put in some data so I could get the message back to you, then filled in the rest.
So the user is going to pass in two parameters: The date of the report and the list of stations they want to get an inventory count on. What were the parameters that produced the output you posted before:
STATION     INITIAL_TONS     USED_TONS     AS_OF_DATE
810               835500        465100      09/01/2010
410               495800        366900      09/02/2010
550               568900        122600      08/31/2010
What I need the report to do is
1) take a station from the list
2) find out the inventory tally from STATION_LOG where the date is the largest date less than the supplied date. This should give me AS_OF_DATE and my initial quantity.
3) query the data table for all tons hauled from the AS_OF_DATE for that station.
4) repeat for the next station.So this is what your existing PL/SQL code does. A non procedural language, like SQL, won't follow the same steps, of course.
The sample data for station_log is:
INSERT INTO STATION_LOG (1, to_date('08/31/2010','MM/DD/YYYY'), 810, 562500);
INSERT INTO STATION_LOG (2, to_date('09/02/2010','MM/DD/YYYY'), 410, 495500);
INSERT INTO STATION_LOG (3, to_date('09/01/2010','MM/DD/YYYY'), 910, 832600);
INSERT INTO STATION_LOG (4, to_date('12/31/2010','MM/DD/YYYY'), 810, 239800);How do you get the initial_tons in the output above from the data above? Did you mean to post some new sample data for station_log?
I still get ORA-00928 errors from all the INPUT statements.
As I said, I can do it inside a loop in PL/SQL, but I got completely stumped on how I could accomplish this in SQL. The trick is that if I can do it in SQL, I can allow the user to export the data to csv using built-in functionality. If I have to do it in PL/SQL, I can't provide the export as easily.
One more thing I just thought about, I am going to need to use a BETWEEN on the dates of the data I need to grab. I obviously don't want to grab data past another inventory tally record from STATION_LOG for the same station, and I can use an NVL so it cuts off at SYSDATE. I obviously haven't hauled anything in the future ;)I doubt if I'll get enough information to do this for you before I leave on vacation.
Here's an example of what you need to do using the scott.emp table instead of your station_log table:
SELECT       job
,       hiredate
,       sal
FROM       scott.emp
ORDER BY  job
,            hiredate
JOB       HIREDATE           SAL
ANALYST   03-Dec-1981       3000
ANALYST   19-Apr-1987       3000
CLERK     17-Dec-1980        800
CLERK     03-Dec-1981        950
CLERK     23-Jan-1982       1300
CLERK     23-May-1987       1100
MANAGER   02-Apr-1981       2975
MANAGER   01-May-1981       2850
MANAGER   09-Jun-1981       2450
PRESIDENT 17-Nov-1981       5000
SALESMAN  20-Feb-1981       1600
SALESMAN  22-Feb-1981       1250
SALESMAN  08-Sep-1981       1500
SALESMAN  28-Sep-1981       1250Say we want to find, for each job in a given list, the sal that corresponds to the last hiredate that is no later than the given report_date. (This seems pretty close to what you heed: fior each stn_key in a given list, the quantity that corresponds to the last row that is no later than the given report_date.)
That is, if we're only interested in the jobs CLERK, MANAGER and PRESIDENT, and only in hiredates on or before December 31, 1981, the output would be:
JOB       LAST_HIREDA   LAST_SAL
CLERK     03-Dec-1981        950
MANAGER   09-Jun-1981       2450
PRESIDENT 17-Nov-1981       5000That is, we want to ignore all jobs that are not in the given list, and all rows whose hiredate is after the given report_date. Among the rows that remain, we're interested only in the last one for each job.
Note that the last_sal for CLERK is not 1300 or 1100: those values were after the given report_date. also, the last_sal for CLERK is not 800; that's not the last one of the remaining rows.
Here's one way to get those results in pure SQL:
DEFINE       jobs_wanted     = "CLERK,MANAGER,PRESIDENT"
DEFINE       report_date     = "DATE '1981-12-31'"
SELECT       job
,       MAX (hiredate)                         AS last_hiredate
,       MAX (sal) KEEP (DENSE_RANK LAST ORDER BY hiredate)     AS last_sal
FROM       scott.emp
WHERE       hiredate     <= &report_date
AND        ',' || '&jobs_wanted' || ','     LIKE
       '%,' || job                  || ',%'
GROUP BY  job
ORDER BY  job
;I used substitution variables for the parameters. You could use bind_variables, or hard-code the values instead.
The WHERE clause is applied before aggreate functions are computed, so rows after &report_date don't matter.
"MAX (sal) KEEP (DENSE_RANK LAST ORDER BY hiredate)" means the sal that is associated with the last row, in order by hiredate. If there happens to be a tie (that is, two or more rows have exactly the same hiredate, and no row is later) then the highest sal from thsoe rows is returned; that's what MAX means here. Ties may be impossible in your data.
You need to write a similar query using your station_log table, and join the results of that to your load_data table, including only the rows that have dates between the date in the sub-query (last_hiredate in my example) and the parameter report_date. That can be part of the join condition.

Similar Messages

  • Need of additional date ranges in Campaighn

    Hi,
           Can any one please explain me the Need of additional date ranges in Campaighn.we have option of creating additional date ranges,Can any one give live example for this requirement.
    Regards
    Narayana

    Hello Manam,
    I think you can add up to 6(not sure though) additional data ranges.
    These are the dates relating to 1)Buying dates 2)Predip 3)Post dip dates
    4)Promotion dates.
    Promotion dates are the dates that are used in TPM where the stock reaches retailer and shelves them and starts selling the good for a promotional price and the price ends on the Promotion end date.
    Please reward if this helps.
    regards,
    Muralidhar Prasad.C

  • Date range query  problem  in report

    Hi all,
    I have created a report based on query and i want to put date range selection but query giving problem.
    If i am creating select list selection then it is working fine means it will display all records on the particular date.
    But what i need is that user will enter date range as creation_date1,creation_date2 and query should return all the records between these date range. i want to pass it by creating items, i created two items and passing creation_date range to display all records but not displaying and if not passing date then should take null as default and display all records
    Here is the query:
    /* Formatted on 2006/12/10 20:01 (Formatter Plus v4.8.0) */
    SELECT tsh."SR_HEADER_ID", tsh."SALES_DEPT_NUMBER", tsh."COUNTRY",
    tsh."LOCAL_REPORT_NUMBER", tsh."ISSUE_DATE", tsh."SUBJECT",
    tsh."MACHINE_SERIAL_NUMBER", tsh."MACHINE_TYPE", tsh."MACHINE_HOURS",
    tsh."STATUS"
    FROM "TRX_SR_HEADERS" tsh, "TRX_SR_PARTS" tsp
    WHERE (tsh.status LIKE :p23_status_sp OR tsh.status IS NULL)
    AND (tsh.machine_type LIKE :p23_machine_type_sp)
    AND ( tsh.machine_serial_number LIKE
    TO_CHAR (:p23_machine_serial_number_sp)
    OR tsh.machine_serial_number IS NULL
    AND ( TO_CHAR (tsh.failure_date, 'DD-MON-YY') LIKE
    TO_CHAR (:p23_failure_date_sp)
    OR TO_CHAR (tsh.failure_date, 'DD-MON-YY') IS NULL
    AND ( TO_CHAR (tsh.creation_date, 'DD-MON-YY')
    BETWEEN TO_CHAR (:p23_creation_date_sp)
    AND TO_CHAR (:p23_creation_date_sp1)
    OR TO_CHAR (tsh.creation_date, 'DD-MON-YY') IS NULL
    AND (tsh.issue_date LIKE :p23_date_of_issue_sp OR tsh.issue_date IS NULL)
    AND (tsh.country LIKE :p23_country_sp OR tsh.country IS NULL)
    AND ( tsh.local_report_number LIKE TO_CHAR (:p23_local_rep_num_sp)
    OR tsh.local_report_number IS NULL
    AND ( tsp.part_number LIKE TO_CHAR (:p23_part_number_sp)
    OR tsp.part_number IS NULL
    AND tsh.machine_type IN (
    SELECT DISTINCT machine_type
    FROM trx_sales_dept_machine_list
    WHERE sales_department_id IN (
    SELECT DISTINCT sales_department_id
    FROM trx_user_sales_department
    WHERE UPPER (user_name) =
    UPPER ('&APP_USER.'))
    AND SYSDATE >= valid_from)
    AND tsh.sr_header_id = tsp.sr_header_id
    can any one tell me wat is wroung in this query.
    Any other way to write this?
    Thank You,
    Amit

    Hi User....
    Here is some date range SQL that my teams uses with some success:
    For date columns that do not contain NULL values, try this (note the TRUNC, it might help with your "today" problem).
    The hard coded dates allow users to leave the FROM and TO dates blank and still get sensible results (ie a blank TO date field asks for all dates in the future.
    AND TRUNC(DATE_IN_DATABASE)
    BETWEEN
    decode( :P1_DATE_FROM,
    TO_DATE('01-JAN-1900'),
    :P1_DATE_FROM)
    AND
    decode( :P1_DATE_TO,
    TO_DATE('31-DEC-3000'),:
    :P1_DATE_TO)
    For date columns that contain NULL values, try this (a little bit trickier):
    AND nvl(TRUNC(DATE_IN_DATABASE),
    decode( :P1_DATE_FROM,
    decode( :P1_DATE_TO,
    TO_DATE('30-DEC-3000'),
    NULL),
    NULL)
    BETWEEN
    decode( :P1_DATE_FROM,
    TO_DATE('01-JAN-1900'),
    :P1_DATE_FROM)
    AND
    decode( :P1_DATE_TO,
    TO_DATE('31-DEC-3000'),
    :P1_DATE_TO)
    Note the 30-DEC-3000 versus 31-DEC-3000. This trick returns the NULL dates when the FROM and TO date range items are both blank.
    I hope this helps.
    By the way, does anyone have a better way of doing this? The requirement is given a date column in a database and a FROM and a TO date item on a page,
    find all of the dates in the database between the FROM and TO dates. If the FROM date is blank, assume the user want all dates in the past (excluding NULL dates). If the TO date is blank, assume that the user wants all of the dates in the future (excluding NULL dates). If both FROM and TO dates are blank, return all of the dates in the databse (including NULL dates).
    Cheers,
    Patrick

  • Single day date range query

    I have a table with a created_date column. The datatype is DATE.
    When i query this table for some date range I do not ever get any results for a single day.
    e.g. select * from table where created_date >='29-Dec-2009' and created_date <='30-Dec-2009'.
    Result set = 5 records
    However,
    select * from table where created_date >='29-Dec-2009' and created_date <='29-Dec-2009'.
    OR
    select * from table where created_date >='30-Dec-2009' and created_date <='30-Dec-2009'.
    yield 0 records.
    Why is this so? I seem to be missing something obvious. Any pointers would be really helpful

    select * from table where created_date >='29-Dec-2009' and created_date <='30-Dec-2009'.means all created date between '29-Dec-2009 00:00:00' and '30-Dec-2009 00:00:00'
    select * from table where created_date >='29-Dec-2009' and created_date <='29-Dec-2009'.means all created date between '29-Dec-2009 00:00:00' and '29-Dec-2009 00:00:00'
    select * from table where created_date >='30-Dec-2009' and created_date <='30-Dec-2009'.means all created date between '30-Dec-2009 00:00:00' and '30-Dec-2009 00:00:00'
    So you need to correctly manage the time...
    select * from table where created_date >='29-Dec-2009' and created_date &lt; '30-Dec-2009'.To get only records at any time during Dec-29.
    select * from table where created_date >='30-Dec-2009' and created_date &lt; '31-Dec-2009'.To get only records at any time during Dec-30.
    select * from table where created_date >='29-Dec-2009' and created_date &lt; '31-Dec-2009'.To get only records at any time during Dec-29 and Dec-30.
    Max
    [My Italian Oracle blog|http://oracleitalia.wordpress.com/2009/12/29/estrarre-i-dati-in-formato-xml-da-sql/]

  • Need help with date range searches for Table Sources in SES

    Hi all,
    I need help, please. I am trying to satisfy a Level 1 client requirement for the ability to search for records in crawled table sources by a date and/or date range. I have performed the following steps, and did not get accurate results from Advanced searching for date. Please help me understand what I am doing wrong, and/or if there is a way to define a date search attribute without creating a LOV for a date column. (My tables have 500,00 rows.)
    I am using SES 10.1.8.3 on Windows 32.
    My Oracle 10g Spatial Table is called REPORTS and this table has the following columns:
    TRACKNUM Varchar2
    TITLE Varchar2
    SUMMARY CLOB
    SYMBOLCODE Varchar2
    Timestamp Date
    OBSDATE Date
    GEOM SDO_GEOMETRY
    I set up the REPORTS table source in SES, using TRACKNUM as the Primary Key (unique and not null), and SUMMARY as the CONTENT Column. In the Table Column Mappings I defined TITLE as String and TITLE.
    Under Global Settings > Search Attributes I defined a new Search Attribute (type Date) called DATE OCCURRED (DD-MON-YY).
    Went back to REPORTS source previously defined and added a new Table Column Mapping - mapping OBSDATE to the newly defined DATE OCCURRED (DD-MON-YY) search attribute.
    I then modified the Schedule for the REPORTS source Crawler Policy to “Process All Documents”.
    Schedule crawls and indexes entire REPORTS table.
    In SES Advanced Search page, I enter my search keyword, select Specific Source Group as REPORTS, select All Match, and used the pick list to select the DATE OCCURRED (DD-MON-YY) Attribute Name, operator of Greater than equal, and entered the Value 01-JAN-07. Then the second attribute name of DATE_OCCURRED (DD-MON-YY), less than equals, 10-JAN-07.
    Search results gave me 38,000 documents, and the first 25 I looked at had dates NOT within the 01-JAN-07 / 10-JAN-07 range. (e.g. OBSDATE= 10-MAR-07, 22-SEP-07, 02-FEB-08, etc.)
    And, none of the results I opened had ANY dates within the SUMMARY CLOB…in case that’s what was being found in the search.
    Can someone help me figure out how to allow my client to search for specific dated records in a db table using a single column for the date? This is a major requirement and they are anxiously awaiting my solution.
    Thanks very much, in advance….

    raford,
    Thanks very much for your reply. However, from what I've read in the SES Admin Document is that (I think) the date format DD/MM/YYYY pertains only to searches on "file system" sources (e.g. Word, Excel, Powerpoint, PDF, etc.). We have 3 file system sources among our 25 total sources. The remaining 22 sources are all TABLE or DATABASE sources. The DBA here has done a great job getting the data standardized using the typical/default Oracle DATE type format in our TABLE sources (DD-MON-YY). Our tables have anywhere from 1500 rows to 2 million rows.
    I tested your theory that the dates we are entering are being changed to Strings behind the scenes and on the Advanced Page, searched for results using OBSDATE equals 01/02/2007 in an attempt to find data that I know for certain to be in the mapped OBSDATE table column as 01-FEB-07. My result set contained data that had an OBSDATE of 03-MAR-07 and none containing 01-FEB-07.
    Here is the big issue...in order for my client to fulfill his primary mission, one of the top 5 requirements is that he/she be able to find specific table rows that are contain a specific date or range of dates.
    thanks very much!

  • Date Ranges Query

    Hi Experts,
    A very happy new year to all of you(to some in advance)!!
    I have a table where I have the employee id and task assigned with start dates & end dates. Overlapping dates of tasks is a data issue however I know how to spot them & eradicate them. So there will be no overlapping task dates
    Employee_id
    Task_No
    Task_Start_date
    Task_End_Date
    Sample Data
    1     T1     01-Jan-2014     28-Feb-2014
    1     T2     01-Mar-2014     31-Dec-2014
    2     T1     23-Jan-2014     31-Dec-2014
    2     T2     01-Jan-2015     31-Dec-2073 (Means end of time)
    3     T3     01-Jan-2014     15-Jul-2014
    3     T4     01-Aug-2014     31-Dec-2014
    4     T5     01-Jan-2014     31-Dec-2073
    I want to devise a query where i provide the end date & it will list out all the employees who are free for even one day starting 01-Jan-2014. So for example If I give 31-Dec-2014, it will list out
    EmpId     (First day on which the employee is free)
    2               01-Jan-2014
    3               16-Jul-2014
    If I give end date = end of time(31-Dec-2013), Expected Result -
    EmpId     (First day on which the employee is free)
    1               01-Jan-2015
    2               01-Jan-2014
    3               16-Jul-2014
    If I give end date = 31 Jan 2014, Expected Result -
    EmpId     (First day on which the employee is free)
    1               01-Jan-2015
    I devised following query, however it does not catch employee id 2. Also it does not provide flexibility to change end date-
    select *
      from (select employee_id,
                   task_start_date,
                   task_end_date,
                   lag(task_end_date, 1, task_start_date) over(partition by employee_id order by task_start_date) prev_end_date
              from shop.employee_tasks
             where task_end_date >= trunc(sysdate))
    where task_start_date - prev_end_date > 1
    Thanks in advance!!
    Regards,

    This is an example of what I call the "free time" query: you have the dates when you are busy and you want the dates when you are free.
    You can find a beautiful solution to the basic problem here: Ask Tom "SQL Query to find gaps in date ranges" (search for Antony Boucher's solution). Please note that this solution works even with overlapping date ranges.
    To apply the solution here, first create the test data (please do this yourself in later questions).
    create table t(Employee_id, task_no, Task_Start_date, task_end_date)
    as select
    1, 'T1', to_date('01-jan-2014'), to_date('28-feb-2014') from dual union all select
    1, 'T2', to_date('01-Mar-2014'), to_date('31-Dec-2014') from dual union all select
    2, 'T1', to_date('23-jan-2014'), to_date('31-dec-2014') from dual union all select
    2, 'T2', to_date('01-jan-2015'), to_date('31-dec-2073') from dual union all select
    3, 'T3', to_date('01-jan-2014'), to_date('15-jul-2014') from dual union all select
    3, 'T4', to_date('01-aug-2014'), to_date('31-dec-2014') from dual union all select
    4, 'T5', to_date('01-Jan-2014'), to_date('31-Dec-2073') from dual;
    In the query, you have to add records for yesterday and for the "end date" you want. This allows you to find "free time" before and after the date ranges in your table.
    Now you partition by Employee_id and order by Task_Start_date. Using the max(task_end_date) analytic function, you get the latest end date up to now. Add 1 to this and you get the first free date (maybe). To make sure that date is free, it has to be before the next start date.
    variable end_date varchar2(64)
    exec :end_date := '31-Dec-2073';
    with boundaries as (
      select trunc(sysdate)-1 task_start_date, trunc(sysdate)-1 task_end_date from dual
      union all
      select to_date(:end_date)+1, to_date(:end_date)+1 from dual
    ), data as (
      select * from t
    where task_end_date >= trunc(sysdate)
      and task_start_date < :end_date
      union all
      select distinct a.employee_id, null, b.task_start_date, b.task_end_date
      from t a, boundaries b
    select employee_id, min(free_start) free_start
    from (
      select employee_id,
      max(task_end_date) over (partition by employee_id order by task_start_date)+1 free_start,
      lead(task_start_date) over (partition by employee_id order by task_start_date)-1 free_end
      from data
    where free_start <= free_end
    group by employee_id;
    EMPLOYEE_ID
    FREE_START
    1
    01-JAN-15
    2
    01-JAN-14
    3
    16-JUL-14

  • Date range query

    I have the following query.
    I have a table that contains studentid,student_date_of_birthA second table contains studentid,class_passed,date_of_passingNow I want to know all students who passed their 'CLASS_X' during
    a particular date range and at the time of passing,the age of the student was 15 years.
    Any help would be greatly appreciated.
    Regards.

    Hi, this works in Oracle 9i
    SQL>SELECT studentid
    2 FROM table2
    3 JOIN table1
    4 USING (studentid)
    5 WHERE class_passed = 'CLASS_X'
    6 AND date_of_passing BETWEEN '&&Date1' AND '&&Date2'
    7 AND (FLOOR(MONTHS_BETWEEN('&Date1',student_date_of_birth)/12) = 15
    8 OR FLOOR(MONTHS_BETWEEN('&Date2',student_date_of_birth)/12) = 15)
    9 /
    Regards,
    Ely

  • **PLEASE HELP**Need Help adding date ranges to form

    I have designed a form with approximately 28 user defined date fields. I want the user to be able put in the first date range and then seautomaticaly fill in the other dates sequencially.
    ie,.... the user puts in 08/01/2007 then the following date field populates with 08/02/2007, then 08/03/2007 and so on....

    The following script can be used for the "exit" action so the basic action to get the start date's number and then it can be used to compute the next days field values by adding the appropriate number of days. A more experienced user could use a control loop t0 modify the field names and number of days to add to the start date's day number for the additional date fields.
    // Script for exit from date select field
    // perform calculation only if an input date has been selected
    if (HasValue(cInputDate)) then
    // format string for the date fields
    var cDateFormat = "MMM D, YYYY"
    // intermediate information start - optional
    xfa.host.messageBox(Concat("The input date formatted value is: ", cInputDate.formattedValue, "
    The input date's numeric value: ", Date2Num($.formattedValue, cDateFormat), "
    The adjusted date number is: ", (Date2Num($.formattedValue, cDateFormat) + 1), "
    The adjusted date is: ", Num2Date(Date2Num($.formattedValue, cDateFormat) + 1) ),
    "Intermediate Detail", 3, 0)
    // intermediate information end - optional
    var StartDateNumber = Date2Num($.formattedValue, cDateFormat)
    // add 1 day to StartDateNumber
    cAdjustDate = Num2Date(StartDateNumber + 1, cDateFormat)
    // add code for additional date fields here adding appropriate number of days for date
    else
    cAdjustDate = "" // no input keep field blank
    // add code for additional date fields here
    endif

  • Need to specify date range for query result.

    Below is my query. The query as is is working fine. The columns 'totalCalls' , 'totalOrders' and 'totalSCs' are all stored by date. I have created a form where the user can specify a start and end date. How would I change this query to report within those specified dates.
    <cfquery name="qZVPData_Western" datasource="xxxxxx">
    SELECT UserID,
           TMName,
        UserZone,
        AVG(WeekCallGoal) AS WCG,
        AVG(QTCallGoal) AS QTCG,
              (SELECT COUNT(*)
               FROM Sales_Calls
               WHERE Sales_Calls.UserID = u.UserID) as totalCalls,
        (SELECT COUNT(*)
         FROM Orders
         WHERE Orders.UserID = u.UserID) as totalOrders,
        (SELECT SUM(Quantity)
         FROM ProductOrders PO
         WHERE PO.UserID = u.UserID AND PO.NewExisting = 1) as newItems,
        (SELECT SUM(NewExisting)
         FROM  ProductOrders PO_
         WHERE PO_.UserID = u.UserID) as totalNew,
        (SELECT COUNT(ServiceCall_ID)
         FROM  ServiceCalls SC
         WHERE SC.UserID = u.UserID) as totalSCs,
        (SELECT COUNT(UserID)
         FROM  TMStatusLog TSL
         WHERE TSL.UserID = u.UserID AND TSL.Status = 'Vacation') as TSLdays1,
        (SELECT COUNT(UserID)
         FROM  TMStatusLog TSL
         WHERE TSL.UserID = u.UserID AND TSL.Status = 'TradeShow') as TSLdays2,
        (SELECT COUNT(UserID)
         FROM  TMStatusLog TSL
         WHERE TSL.UserID = u.UserID AND TSL.Status = 'Admin Day') as TSLdays3,  
        SUM(TSLdays1)+(TSLdays2)+(TSLdays3) AS TSLdays,   
        SUM(totalOrders)/(totalCalls) AS closePerc,
        SUM(totalOrders)/(totalCalls) - (.30) AS GRV,
        SUM(totalSCs)+(totalCalls)-(QTCG) AS PerVar,
       (SUM(totalSCs) + totalCalls + (TSLdays*WCG/5))/QTCG AS PerCalls
    FROM Users u
    WHERE UserZone = 'Western'
    GROUP BY UserZone, UserID, TMName
    </cfquery>
    I figured I could add this to the columns WHERE statements;
    'AND Column BETWEEN #FORM.Start# AND #FORM.End#' but this isn't working.
    Any ideas???

    What is the SQL being generated by your <cfquery> contents?  Is it valid SQL?  This is always the first thing to check when you get SQL errors back from the DB... check what you're sending to the DB.
    Second: don't hard-code dynamic values into your SQL string, pass them as parameters.
    Re all the subqueries: it runs fine in dev. Have you tried to load test it?  If poss move your subqueries to the FROM statement, as then they're only run once per recordset. As opposed to once per row of the result set, when the subqueries are in the SELECT or WHERE statement.
    Adam

  • Need suggestion for data encryption

    Hello Experts,
    I need your expert opinion on one of the data encryption method. We have some legal compliance to implement data encryption as listed below, lets say we have to apply encryption on 2 tables (1) TAB_A (2) TAB_B.
    (1) Need data encryption on the TAB_A & TAB_B for 2-3 columns and not the entire table.
    (2) Data should not be in readable format, if anyone connect to database and query the table.
    (3) We have reporting services on our tables but reporting services doesn't connect to our schema directly rather they connect to a different schema to which we have given the table Select grant.
    (4) Reports should work as it is, and users should see the data in readable format only.
    (5) There are batch processes which generates the data into these tables and we are not allowed to make any changes to these batch processes.
    This is a business need which has to be delivered. I explored various options such as VPDs, Data encryption methods etc but honestly none of these are serving our business need. There is also a limitation of encrypting data as data volume of quiet high (30TB DB) and generally users query the data on millions of records at a time. Also reports have very tight SLAs as well. If we create any encryption wrapper then decrypt will take longer in reports and will cause the SLA miss for reports.
    Could someone please suggest any better solution to me or if something is inbuilt in Oracle? We are using Oracle 11g.
    Regds,
    Amit.

    you can read about Transparent Data Encryption
    Check
    http://docs.oracle.com/cd/B28359_01/network.111/b28530/asotrans.htm

  • Need suggestions for improving sql query.

    Hi,
    There is an sql query which loads data into a single table from 80 other tables which are loaded with data from different sites.The sql query to load from these 80 tables into single table is taking almost 40 min everyday with 6-7 million records.
    The notation followed
    the table is truncated first and then the constraints are disabled.
    insert into single table (column1,column2,column3)
    ( select * from table1
    union all
    select * from table2
    union all
    select * from table3
    union all
    select * from table80);
    enable the constraints.
    The database is 10.2.0.3.Is there any other way that can be implemented or modified this to get a better response time to load the data faster.
    Thanks

    A lot of IFs, but
    if you have a licence for the partitioning option, and
    if the data has some convenient partitioning column - such as a 'site' column which could be used for list partitioning, and
    if you don't need the separate tables to exist after you have consolidated the data
    if you can create all other necessary constraints on the base tables,
    you could:
    create main table as empty, but partitioned with 80 empty partitions
    exchange partition with table 80 times in a row.
    Regards
    Jonathan Lewis
    http://jonathanlewis.wordpress.com
    http://www.jlcomp.demon.co.uk

  • Need suggestion on Data Center Migration

    Hi All,
    We are currently handling a datacenter migration project where in the souce instance is hosted at Oracle Datacenter ( Texas ).
    and the target instance need to be built on **** Hosted datacenter ( U.K).
    -> It takes 3days to ship the (cold) backup from U.S datacenter to UK datacenter. We could able to bring up the database using that. But
    in order to keep the new Instnace in sync with the source ( which is being used by bussiness) we have to apply all the archvies in this gap.
    --> Database size 250G
    --> We could manage to ship the archives generated after cold backup directly to FTP server ( in UK network). But there ar around 3000 - 4000 archives to be applied.
    We have planned to perform a cancel based recovery here.
    In this scenario, need your advice, If we have any other alternative ( Best practice)
    -Regards
    Vinay

    Hi,
    have you seen this:
    WAN Zero Data Loss Failover with Oracle Data Guard 11.2 using a Near-DR Strategy (Broker version)          [Document 1489359.1]
    Regards
    Sebastian

  • Date range query between day1 and day1-14

    I am using:
    Oracle SQL Developer (3.0.04)
    Build MAin-04.34
    Oracle Database 11g
    Enterprise Edition 11.2.0.1.0 - 64bit Production
    I am having diffitult time getting this filter to work:
    {Code}
    where date_value between :day1 and :day1 - 14
    {Code}
    What am I doing wrong?
    I am looking to input a single day and have it give a range of 14 days back. So if I put "25-JUN-2013" it should include that day and track back 14 days.

    1007410 wrote:
    where date_value between :day1 and :day1 - 14
    Reverse the order.
    You generally specify a range from LOW to HIGH. Or in dates, EARLIER to LATER.
    You've done the reverse.
    Basically instead of saying "Pick a number from 1 to 10", you said "Pick a number from 10 to 1".
    with xx as
       ( select to_date('25-jun-2013', 'dd-mon-yyyy') d from dual union all
         select to_date('20-jun-2013', 'dd-mon-yyyy') d from dual union all
         select to_date('15-jun-2013', 'dd-mon-yyyy') d from dual union all
         select to_date('05-jun-2013', 'dd-mon-yyyy') d from dual
    Select d
      from xx
    where d between sysdate and sysdate-14 ;
    no rows selected.
    with xx as
       ( select to_date('25-jun-2013', 'dd-mon-yyyy') d from dual union all
         select to_date('20-jun-2013', 'dd-mon-yyyy') d from dual union all
         select to_date('15-jun-2013', 'dd-mon-yyyy') d from dual union all
         select to_date('05-jun-2013', 'dd-mon-yyyy') d from dual
    Select d
      from xx
    where d between sysdate-14 and sysdate;
    D       
    25-JUN-13
    20-JUN-13
    15-JUN-13
    3 rows selected.

  • Query for date range? JE

    Hi,
    I have seem some posts on the JE forum regarding quering for date range, but it is mostly using DPL.
    Is there any way to do that using the JE API
    Thanks,
    Mohammad

    Hi Mohammad,
    A date range query can be performed as a key range query. There's nothing special about dates except that you'll want to use a key binding that gives a meaningful sort order. If you're representing your dates in milliseconds, then a LongBinding (in com.sleepycat.bind.tuple) will work well. In general, use tuple bindings for keys, because they provide a meaningful sort order.
    To perform a range query, this FAQ has some hints:
    http://www.oracle.com/technology/products/berkeley-db/faq/je_faq.html#28
    On range searches in general, they can be done with Cursor.getSearchKeyRange or with the SortedSet.subSet and SortedMap.subMap methods, depending on whether you are using the base API or the Collections API. It is up to you which to use.
    If you use Cursor.getSearchKeyRange you'll need to call getNext to iterate through the results. You'll have to watch for the end range yourself by checking the key returned by getNext. This API does not have a way to enforce range end values automatically.
    If you use the Collections API you can call subMap or subSet and get an Iterator on the resulting collection. That iterator will enforce both the beginning and the end of the range automatically.
    Does this answer your question?
    --mark                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Get the date range

    Hi,
    There is a requirement that we need to get date range.This date range will be arrived at using the function module BKK_GET_PRIOR_WORKDAY. The factory calender id is GB. It should return the date range. Therefore, for weekends the date range will be Saturday, Sunday and Monday.  The date range will change based on holidays that exist in the holiday calendar.
    Now my question is how to calculate that there are two days between friday and monday.
    Please help.

    Hi Nishigandha,
    If i am correct you need the number of days between two dates
    ex:
    date1: 04/06/2009
    date2: 12/06/2009
    you need the number of days??
    then probably check this Fm out:
    SD_DATETIME_DIFFERENCE this is a function module which gives the difference in Days and Time for entered two dates
    P.S Additionally  Chk this out:
    /people/mustafabaris.gunes/blog/2009/03/16/calculating-number-of-working-days-in-query-level
    Hope its useful
    Thanks and Regards
    Srikanth.P
    Edited by: SRIKANTH P on Jun 4, 2009 2:33 PM

Maybe you are looking for