Age of query optimization in the year

Hey there,

I calculate the difference of the year between two dates by doing the following thing:
TRUNC(MONTHS_BETWEEN(RES.SAMPLE_SAMPLING_DATE,RES.PATIENT_BIRTHDATE)/12)+1
Do you think I can optimized the query or you know other ways to do the same calculation?

Kind regards
Jarod.

Have you already searched this forum?
There are many examples...

Tags: Database

Similar Questions

  • Suggest the Sql query to get the years, months, days of the two dates.

    Hi all

    SELECT TO_DATE('06-02-2014','DD-MM-YYYY') - TO_DATE('01-04-1998','DD-MM-YYYY') SERVICE_DAYS OF THE DOUBLE

    5790 SERVICE_DAYS

    POWER REQUIRED:
    YEARS MONTHS DAYS
    15 10 5

    OUTPUT DISPLAY:

    Select
    Floor(5790/ 365.25),
    Floor (mod (5790,365.25) / (365.25 / 12)),.
    ceil (mod(5790,365.25 / 12))
    Double;

    YEARS MONTHS DAYS
    15 10 7 - TWO DAYS MORE


    How they calculate manually is as follows:

    2014 02-06
    1998-04-01
    ---------------
    05 - days

    2014 02-06
    1998-04-01
    + 12
    ---------------
    10 month 02 is lower than 04, so we borrow 12 months of TI (2014) years will be (2013)

    2014 02-06
    1998-04-01
    -(1).
    ---------------
    15. for the years since we-1

    Kind regards

    Afzal.

    OK... so try not not to multiply the reminder to add_months with 30 but to solve the 'day-thing' in a different way...

    first step (data) is only my implementation testdata.

    WITH DATA AS (SELECT TO_DATE('02-03-2014','DD-MM-YYYY') AS Degnide

    TO_DATE('07-04-2008','DD-MM-YYYY') AS dayfrom

    OF the double

    UNION ALL

    SELECT TO_DATE('12-04-2014','DD-MM-YYYY') AS Degnide

    TO_DATE('11-05-1994','DD-MM-YYYY') AS dayfrom

    OF the double

    UNION ALL

    SELECT TO_DATE('06-02-2014','DD-MM-YYYY') AS Degnide

    TO_DATE('01-04-1998','DD-MM-YYYY') AS dayfrom

    OF the double

    )

    calc AS (SELECT months_between (dayto, dayfrom) SERVICE_DAYS

    Degnide

    dayfrom

    DATA)

    SELECT Degnide

    dayfrom

    floor(service_days/12) IN years

    floor (mod(service_days,12)) AS month

    , BUSINESS sign (EXTRACT (Degnide DAY) - EXTRACT (DAY FROM dayfrom))

    WHEN THEN-1 EXTRACT (DAY FROM Degnide) + 30 - EXTRACT (DAY OF THE dayfrom)

    WHEN 0 then 0

    WHEN 1 THEN EXTRACT (Degnide DAY) - EXTRACT (DAY FROM dayfrom)

    0 OTHERWISE

    end as days

    CALC

    HTH

  • query not using the index for some user

    Hello

    I have a query that is running in less than a second for sys, system, or schema owner. However, another user (test_user) take 30 seconds to run the same query.

    I certainly dba and privileges identical to test_user as schmea_user, but the result is the same.

    I checked

    Select * from V$ SYS_OPTIMIZER_ENV;

    Both are the same for both users.

    I have check the plan to explain to both users. I noticed that for sys/system/schema_owner, the query uses an index, but not the test_user.

    All have experience the issue where a user uses an index, but not the other?

    Thank you for any assistance.

    Thank you for the display of formatting output, this output is much easier to read.

    One of the first things you notice about the execution plans that is for the owner non-schema "SQL_ID, 0wcs85uywn72m, number of children 1" appears in the output of DBMS_XPLAN, while "SQL_ID 0wcs85uywn72m, child number 0" (the same SQL_ID but a different number of child) appears for the schema owner. "" Whereas the SQL_ID is the same, which indicates that the client requires exactly the same SQL statement, so it's a good start.

    Then, note that in the predicate for the nonschema owner information section the following appears (sometimes with the order of the two conditions switched in position) as a condition placed on each table that is available in the schema:

    filter(("SEAL_FLAG" IS NULL OR "SEAL_FLAG"'Y'))
    

    The above suggests the presence of the virtual private database (or a superset of private database virtual) generated the predicates. You should be able to confirm that this is the case by querying V$ VPD_POLICY using the SQL_ID which was displayed in the DBMS_XPLAN output:

    SELECT
      *
    FROM
      V$VPD_POLICY
    WHERE
      SQL_ID='0wcs85uywn72m';
    

    As a test, I made a few minor adjustments to the example on this page:
    http://Antognini.ch/2011/09/optimizer_secure_view_merging-and-VPD/
    I changed the name of T to T12 and TESTUSER table specified for the schema names. I then created the function S of this page as follows:

    CREATE OR REPLACE FUNCTION s (schema IN VARCHAR2, tab IN VARCHAR2) RETURN VARCHAR2 AS
    BEGIN
      RETURN 'ID < 10';
    END;
    /
    

    I then added a couple of lines in the T12 test table:

    INSERT INTO T12 VALUES (1,1,NULL);
    INSERT INTO T12 VALUES (4,1,NULL);
    INSERT INTO T12 VALUES (10,1,NULL);
    INSERT INTO T12 VALUES (12,1,NULL);
    
    COMMIT;
    

    With an active 10053 trace, I executed the following SQL statement:

    SELECT id, pad
      FROM t12
      WHERE
      spy(id, pad) = 1
    

    The SQL_ID (in my case, found in the 10053 trace file) was 6hqw5p9d8g8wf, so I checked V$ VPD_POLICY to this SQL_ID:

    SELECT
      *
    FROM
      V$VPD_POLICY
    WHERE
      SQL_ID='6hqw5p9d8g8wf';
    
    ADDRESS          PARADDR            SQL_HASH SQL_ID        CHILD_NUMBER OBJECT_OWNER OBJECT_NAME                    POLICY_GROUP                   POLICY                 POLICY_FUNCTION_OWNER          PREDICATE
    ---------------- ---------------- ---------- ------------- ------------ ------------ ------------------------------ ------------------------------ ---------------------- ------------------------------ ------------------------------------------------------------------------------------
    000007FFB7701608 000007FFB7743350 1518838670 6hqw5p9d8g8wf            0 TESTUSER     T12                            SYS_DEFAULT                    T_SEC                  TESTUSER                       ID < 10
    

    As noted above, the VPD test function named S added the predicate "ID".< 10"="" to="" the="" sql="">

    There are not many clues in the 10053 trace file in my test VPD generated additional predicates. Trace the following was found shortly after the beginning of the file (this is the SQL statement initially presented):

    ----- Current SQL Statement for this session (sql_id=6hqw5p9d8g8wf) -----
    SELECT id, pad
      FROM t12
      WHERE
      spy(id, pad) = 1
    

    I searched then down in the trace for final after changes query file (to be noted that this sentence could be slightly different in different versions of database Oracle). That's what I found:

    Final query after transformations: ******* UNPARSED QUERY IS *******
    SELECT "T12"."ID" "ID","T12"."PAD" "PAD" FROM "TESTUSER"."T12" "T12" WHERE "TESTUSER"."SPY"("T12"."ID","T12"."PAD")=1 AND "T12"."ID"<10
    kkoqbc: optimizing query block SEL$F5BB74E1 (#0)
    

    Note that the final query after transformation shows how the final version of the query that has been rewritten by the query optimizer before the SQL statement has been executed and this version of the query includes AND "T12". "" IDENTITY CARD ".<10. if="" i="" was="" attempting="" to="" determine="" how="" that=""><10 predicate="" was="" added="" to="" the="" sql="" statement,="" i="" would="" start="" at="" the="" "current="" sql="" statement="" for"="" line="" in="" the="" trace="" file="" and="" search="" down="" the="" trace="" file="" for=""><10* -="" in="" this="" case,="" the="" following="" is="" what="" i="" found="" as="" the="" first="" search="" result,="" very="" close="" to="" the="" "current="" sql="" statement="" for"="" line="" in="" the="" trace="">

    **************************
    Predicate Move-Around (PM)
    **************************
    PM:     PM bypassed: Outer query contains no views.
    PM:     PM bypassed: Outer query contains no views.
    query block SEL$F5BB74E1 (#0) unchanged
    FPD: Considering simple filter push in query block SEL$F5BB74E1 (#0)
    "TESTUSER"."SPY"("T12"."ID","T12"."PAD")=1 AND "T12"."ID"<10
    try to generate transitive predicate from check constraints for query block SEL$F5BB74E1 (#0)
    finally: "TESTUSER"."SPY"("T12"."ID","T12"."PAD")=1 AND "T12"."ID"<10
    

    As can be seen from the above (because the predicate again appeared before and after the line containing the word "Finally: '), the AND"T12 ". "" IDENTITY CARD ".<10 predicate="" was="" already="" added="" to="" the="" original="" sql="" statement="" by="" the="" time="" the="" predicate="" move-around="" section="" of="" the="" trace="" file="" was="" written,="" and="" that="" is="" the="" first="" mention="" of=""><10 in="" the="" trace="" file.="" in="" your="" case,="" you="" would="" search="" the="" 10053="" trace="" file="">

    "SEAL_FLAG" IS NULL
    

    If V$ VPD_POLICY revealed that there are virtual private database (VPD) generated predicates applied to the SQL statement, take a look at the following article in the Oracle documentation library:
    http://docs.Oracle.com/CD/B28359_01/network.111/B28531/VPD.htm

    This article lists the different points of view, who can be interviewed to learn more about the VPD rules which are in force in the schema. For example, with my SPV test:

    SELECT
      *
    FROM
      ALL_POLICIES;
    
    OBJECT_OWNER                   OBJECT_NAME                    POLICY_GROUP                  POLICY_NAME                    PF_OWNER                       PACKAGE                       FUNCTION                       SEL INS UPD DEL IDX CHK ENA STA POLICY_TYPE              LON
    ------------------------------ ------------------------------ ----------------------------- ------------------------------ ------------------------------ ----------------------------- ------------------------------ --- --- --- --- --- --- --- --- ------------------------ ---
    TESTUSER                       T12                            SYS_DEFAULT                   T_SEC                          TESTUSER                       S                                                            YES YES YES YES NO  NO  YES NO  DYNAMIC                  NO
    

    He knows performance issues related to the use of VPD, some of which are Oracle Database version-dependent, and some have been fixed in recent versions. Take a look at the following articles if you have access to My Oracle Support:
    MetaLink (MOS) Doc ID 728292.1 ' known performance problems when you use transparent encryption data and indexes on the encrypted columns.
    MetaLink (MOS) Doc ID 967042.1 "How to investigate Query Performance regressions Caused by VPD (FGAC) predicates?"

    You might find working through the second of the above that the problem is caused by a bug in database Oracle.

    On a side note. Execution plans you have published include the 0 value in the column starts many of the operations in the execution plan. 0 indicates that the operation never actually executed. A 0 is included in the column starts on the line that includes the FULL ACCESS of TABLE of PEOPLE_TRANSACTIONS at least to the OPC. Value 123, a full table of PEOPLE_TRANSACTIONS table scan PROPERTY_CONTAINER_ID was not actually performed.

    Charles Hooper
    http://hoopercharles.WordPress.com/
    IT Manager/Oracle DBA
    K & M-making Machine, Inc.

  • Question about the query optimizer

    For a year during my database of the Conference the following table
    CREATE TABLE TASKS
    (
        "ID" NUMBER NOT NULL ENABLE,
        "START_DATE" DATE,
        "END_DATE" DATE,
        "DESCRIPTION" VARCHAR2(50 BYTE)
    ) ;
    with approximately 1.5 million entries were given. In addition, there were the following query:
    SELECT START_DATE, COUNT(START_DATE) FROM TASKS
    GROUP BY START_DATE
    ORDER BY START_DATE;
    And the Index:
    create index blub on Tasks (start_date asc);
    The main exercise was to speed up queries with indexes. Because all the data is available the optimizer ignores index and just have a full table scan.
    Here the QEP:
    ----------------------------------------------------------------------------                                                                                                                                                                                                                                 
    | Id  | Operation          | Name  | Rows  | Bytes | Cost (%CPU)| Time     |                                                                                                                                                                                                                                 
    ----------------------------------------------------------------------------                                                                                                                                                                                                                                 
    |   0 | SELECT STATEMENT   |       |  9343 | 74744 |  3423   (6)| 00:00:42 |                                                                                                                                                                                                                                 
    |   1 |  SORT GROUP BY     |       |  9343 | 74744 |  3423   (6)| 00:00:42 |                                                                                                                                                                                                                                 
    |   2 |   TABLE ACCESS FULL| TASKS |  1981K|    15M|  3276   (2)| 00:00:40 |                                                                                                                                                                                                                                 
    ----------------------------------------------------------------------------
    Then we tried to compel him to make the index with this query:
    ALTER SESSION SET OPTIMIZER_MODE = FIRST_ROWS_1;
    
    SELECT /* + INDEX (TASKS BLUB) */ START_DATE, COUNT(START_DATE) FROM TASKS
    GROUP BY START_DATE
    ORDER BY START_DATE;
    but again he ignored the index. The optimizer guide makes clear, that whenever you will use all the data in a table, it must do a full scan.
    So we fooled him doing a scan limited quick index with this query:
    create or replace function bla
    return date deterministic is
      ret date;
    begin
      select MIN(start_date) into ret from Tasks;
      return ret;
    end bla;
    
    ALTER SESSION SET OPTIMIZER_MODE = FIRST_ROWS_1;
    
    SELECT /* + INDEX (TASKS BLUB) */ START_DATE, COUNT(START_DATE) FROM TASKS
    where start_date >= bla
    GROUP BY START_DATE
    ORDER BY START_DATE; 
    now, we got the following QEP:
    -----------------------------------------------------------------------------                                                                                                                                                                                                                                
    | Id  | Operation            | Name | Rows  | Bytes | Cost (%CPU)| Time     |                                                                                                                                                                                                                                
    -----------------------------------------------------------------------------                                                                                                                                                                                                                                
    |   0 | SELECT STATEMENT     |      |     1 |     8 |     3   (0)| 00:00:01 |                                                                                                                                                                                                                                
    |   1 |  SORT GROUP BY NOSORT|      |     1 |     8 |     3   (0)| 00:00:01 |                                                                                                                                                                                                                                
    |*  2 |   INDEX RANGE SCAN   | BLUB |     1 |     8 |     3   (0)| 00:00:01 |                                                                                                                                                                                                                                
    ----------------------------------------------------------------------------- 
    So he use the index.

    Now to my two questions:

    1. why should always do a full scan (because the response of optimizer documentation is a bit unsatisfactory)?
    2. After looking at the difference between the costs (FS: 3276 IR: 3) and the time, the system needs (FS: 9.6 s IR: 4.45) why the optimizer refused the plan clearly better?

    Thanks in advance,

    Kai Gödde

    Published by: Kai Gödde on May 30, 2011 18:54

    Published by: Kai Gödde on May 30, 2011 18:56

    The reason for which Oracle is full of sweeping the table corresponding to your request:

    SELECT START_DATE, COUNT(START_DATE) FROM TASKS
    GROUP BY START_DATE
    ORDER BY START_DATE;
    

    and using the index for:

    SELECT /* + INDEX (TASKS BLUB) */ START_DATE, COUNT(START_DATE) FROM TASKS
    where start_date >= bla
    GROUP BY START_DATE
    ORDER BY START_DATE;
    

    has to do with the (possible) null values in the table. Note that the query with a predicate on start_date would have probably used the index even without suspicion.

    The optimizer does not know that there is a start_date value in each row of the table and the group by expression will include NULL values, but because you count start_date (meaning count of non-null of the expression values) the count himself will be null. For example:

    SQL> with t as (
      2     select trunc(sysdate) dt from dual union all
      3     select trunc(sysdate) dt from dual union all
      4     select trunc(sysdate-1) dt from dual union all
      5     select trunc(sysdate-1) dt from dual union all
      6     select to_date(null) from dual)
      7  select dt, count(dt) from t
      8  group by dt;
    
    DT           COUNT(DT)
    ----------- ----------
                         0
    29-MAY-2011          2
    30-MAY-2011          2
    

    Because Oracle does not create an index entry for a line with all null values in the index key, the optimizer is forced full analysis of the table to make sure that it returns all rows. In the query with a predicate on start_date the optimizer knows that no matter what start_date > blah must be non-null.

    To make your first query to use an index, you must declare either start_date as not null (which implies that it is a mandatory field), or if there may be values NULL, but you care not to add a like predicate:

    where start_date is not null);
    

    John

  • Query to calculate the age

    Hello

    Please suggest me to wirte a function that can calculate the age.

    Hello

    No need for a function. But a simple calculation.

    SQL> select round(months_between(sysdate,to_date('11-MAY-1977','dd-MON-YY'))/12) age from dual;
    
           AGE
    ----------
            33
    

    If you take into account the dates stored in an oracle table column can take care of the years
    before and after 1950.
    A thread for your reference: Re: day airthmetic

  • Query help - to get the number of employees joined on each month regardless of the year

    Hi all

    I wrote the code below for number of employees joined each month regardless of the year of the employee table. But I couldn't get the result. Kindly help me where I'm wrong in my code,

    Select to_char (hiredate, 'my') as join, count (empno) under the number

    WCP

    To_char Group (hiredate, 'my')

    After having count (empno) > 1;

    Your application displays the list of months (regardless of the year) where more than one employee was engaged with number of emplyees hired this month here. If you want months even if nobody was hired months thast, you emp table outer join to the list of every month:

    with t as)

    Select the level m

    of the double

    connect by level<=>

    )

    Select to_char (to_date (TM, 'mm'), 'my') Lun,

    Count (e.empno) cnt

    t

    left join

    E EMP

    on Tahina = to_char (e.hiredate, 'mm')

    Group of Tahina

    order of Tahina

    /

    MY CNT
    --- ----------
    1 jan
    February 2
    Mar 0
    Apr 2
    May 2
    1 Jun
    July 0
    August 0
    2 sep
    Oct 0
    1 nov

    MY CNT
    --- ----------
    Dec 3

    12 selected lines.

    SQL >

    SY.

  • How to calculate the year last month in obiee 11g?

    Hello

    I want to calculate the year last month in obiee 11g. I tried DB.

    -SELECT TO_CHAR (ADD_MONTHS (SYSDATE,-12), 'YYYYMMDD') FROM DUAL;

    Using the above query, year last month is -"08-01-2015"

    But I want to get the same result using timestampadd() in obiee 11g.

    Please help me,

    Thanks in advance,

    A.Kavya

    Hello

    TIMESTAMPADD (SQL_TSI_YEAR-1, CURRENT_DATE)

  • SQL query to display the sum of the values of each June and December

    Hello having problems of construction of a query to the list SQL query to display the sum of the values of each June and December each year.

    My Table;

    TABLE name: MONTH_TERM

    Fields with values:

    TERM_KEYMONTH_ACTUALMONTH_DATE
    8250001/11/2015 0:00
    8245001/12/2015 0:00
    8240501/01/2016 0:00
    8240001/02/2016 0:00
    8245001/03/2016 0:00
    8242501/04/2016 0:00
    8243501/05/2016 0:00
    8241006/01/2016 0:00
    8240901/07/2016 0:00
    8241501/08/2016 0:00
    8242009/01/2016 0:00
    8242210/01/2016 0:00
    8243611/01/2016 0:00
    8255601/12/2016 0:00
    8256801/01/2017 0:00
    8262402/01/2017 0:00

    What I would like to see in a query result:

    TERM_KEYMONTH_ACTUALMONTH_DATETotal of 6 months
    8250001/11/2015 0:00
    8245001/12/2015 0:00950
    8240501/01/2016 0:00
    8240001/02/2016 0:00
    8245001/03/2016 0:00
    8242501/04/2016 0:00
    8243501/05/2016 0:00
    8241006/01/2016 0:002525
    8240901/07/2016 0:00
    8241501/08/2016 0:00
    8242009/01/2016 0:00
    8242210/01/2016 0:00
    8243611/01/2016 0:00
    8255601/12/2016 0:002658
    8256801/01/2017 0:00
    8262402/01/2017 0:00

    ---

    Here's my query:

    Select

    "TERM_KEY,"

    "MONTH_ACTUAL,"

    to_char(MONTH_DATE,'MM/YYYY') MONTH_DATE,

    DEAL to_char (MONTH_DATE, 'MM')

    WHEN '06' THEN (SELECT SUM (MONTH_ACTUAL)

    OF MONTH_TERM

    WHERE the to_char (MONTH_DATE, 'MM') between 1 and 6

    AND term_key = 82)

    WHEN '12' THEN (SELECT SUM (MONTH_ACTUAL)

    OF MONTH_TERM

    WHERE the to_char (MONTH_DATE, 'MM') between 7 and 12

    AND term_key = 82)

    ELSE null

    END as SIX_MO_CUMM,

    of MONTH_TERM

    where term_key = 82

    It's my results:

    TERM_KEYMONTH_ACTUALMONTH_DATETotal of 6 months
    8250001/11/15 0:00
    8245001/12/15 0:003608
    8240501/01/16 0:00
    8240001/02/16 0:00
    8245001/03/16 0:00
    8242501/04/16 0:00
    8243501/05/16 0:00
    8241001/06/16 0:003717
    8240901/07/16 0:00
    8241501/08/16 0:00
    8242001/09/16 0:00
    8242210/01/16 0:00
    8243601/11/16 0:00
    8255601/12/16 0:003608
    8256801/01/17 0:00
    8262401/02/17 0:00

    Any ideas on how to fix would be great

    Select term_key,

    month_actual,

    month_date,

    case mod (to_char (month_date, 'mm'), 6)

    When 0 then sum (month_actual)

    During)

    term_key partition

    order of month_date

    between the previous month '5' interval and the current line

    )

    end '6 MONTHS in TOTAL'

    from tbl

    order of term_key,

    month_date

    /

    TERM_KEY MONTH_ACTUAL MONTH_DAT 6 MONTHS TOTAL
    ---------- ------------ --------- --------------
    82 500 NOVEMBER 1ST, 15TH
    82 450 1 DECEMBER 15 950
    82 405 1 JANUARY 16
    82 400 1 FEBRUARY 16
    82 450 1ST MARCH 16
    82 425 1ST APRIL 16
    82 435 1 MAY 16
    82 410 2525 1 JUNE 16
    82 409 1 JULY 16
    82 415 1ST AUGUST 16
    82 420 16 - SEP - 01

    TERM_KEY MONTH_ACTUAL MONTH_DAT 6 MONTHS TOTAL
    ---------- ------------ --------- --------------
    82 422 1 OCTOBER 16
    82-436 NOVEMBER 1, 16
    82 556 2658 1 DECEMBER 16
    82 568 1 JANUARY 17
    82 624 1 FEBRUARY 17

    16 selected lines.

    SQL >

    SY.

  • Optimization of the rules

    Hello

    I want to know ways to optimize the modules. Can someone list on optimization techniques? Those can really fast substantially determination? Thank you

    .. and if I can close the loop on what this has to do with optimizing the performance.  This is my opinion.

    I think that when Jasmine says that the rule is a rule of 'BAD', (be careful not to speak too loudly for him) she means not only from a point of view maintenance rule.

    First, the engine optimizes the path until the OPA don't just use every bit of data 'seeds' and create a static rule execution plan.

    Consider that when you give enough forced a SQL statement in the where clause, a query may return several responses and poor performance.  OPA, however, can still make a unique determination given incomplete sets attributes of database (unknowns and might.)  OPA may provide explanations that include what basic attributes must always be provided (OPA has a forward and backward chaining).  In short, the OPA may use only a small set of basic data to determine a result, even if OPA Gets a lot of seed data. In this respect, the internal dev team Oracle OPA provides optimization performance and optimizations to the engine itself to a path of optimal execution.  This way can and will change dynamically.

    Traditionally, tuning execution path is what .net/java developers worry about and most often is the prospect that developers pull then ask questions about optimizing the performance of OPA.  So my reply may seem unsatisfactory to anyone who does not participate in effective policies of the organization.

    As a second review, OPA has strengths in optimization, modeling and optimization of the policy itself, which rarely concern of the developer.  Were necessary intermediate decisions by the company and OPA said these determinations were while they were not (perhaps due to a bad mix of procedural rules / background)?  Can reduce what is necessary for improved or new political determination or remove the dependencies of base?

    So, if politics is already optimized, then OPA will not be be slower than any other method of execution that requires the same information, but probably much, much faster.  So I think that Brad question above...

    A developer usually should not change the policy, but policy changes can have the most impact on performance for the end user and the business.  Jasmine guidelines provide something of a foundation on the writing of strategy using proven methods of political organization / readability.  This provides visibility into changes policies to non-developers. Thus, when you create policy documents, we (or at least I) follow the guidelines, devote an extra effort to exploit the isomorphism of the OPA and make political visible to policy work.

    It's observation, and I speak in generalizations.  (Once the policy is well written, btw, if I have a problem which is not now obvious, so I just call the engine for the intermediate attributes know where my problem lies - that I should have for test cases in Excel and/or SoapUI.)  Be careful, however, as the need for optimization of performance may indicate really need something else entirely...)  If the answer is still not satisfactory, although I hope not, is there a policy statement of example and source documents that can be provided to get advice on the setting?  -I would recommend starting a new thread in the forum for this.

  • Recovering the year max for combination when the parameters are not exists

    I try to query a table that maintains cumulative information by calendar_year and calendar_period. The problem I encounter is that if I want to ask the total of the transaction for a combination of accounting from a certain calendar_period and calendar_year, I need get the max calendar_year and calendar_period for the previous year or period for the current_year and max calendar_year by combination of accounting (Fund, org prog (, acct) which falls outside the parameters in my where clause, which for this example would be calendar_year = '15' and calendar_period = '03'. For most of all sense I'll post and excerpt from my table.

    create table

    CREATE TABLE "MAXTABLE"
       (    "CALENDAR_YEAR" NUMBER,
        "CALENDAR_PERIOD" NUMBER,
        "AMOUNT" NUMBER,
        "FUND" VARCHAR2(20 BYTE),
        "ACCOUNT" VARCHAR2(20 BYTE),
        "ORG" VARCHAR2(20 BYTE),
        "PROG" VARCHAR2(20 BYTE)
       )
    
    
    
    
    
    
    

    values in the table.

    REM INSERTING into MAXTABLE
    SET DEFINE OFF;
    Insert into MAXTABLE (CALENDAR_YEAR,CALENDAR_PERIOD,AMOUNT,FUND,ACCOUNT,ORG,PROG) values (15,2,25,'202118','2071','3662','121');
    Insert into MAXTABLE (CALENDAR_YEAR,CALENDAR_PERIOD,AMOUNT,FUND,ACCOUNT,ORG,PROG) values (15,3,150,'202118','2070','3662','121');
    Insert into MAXTABLE (CALENDAR_YEAR,CALENDAR_PERIOD,AMOUNT,FUND,ACCOUNT,ORG,PROG) values (14,12,50,'202118','2040','3662','121');
    Insert into MAXTABLE (CALENDAR_YEAR,CALENDAR_PERIOD,AMOUNT,FUND,ACCOUNT,ORG,PROG) values (14,3,100,'202118','2070','3662','121');
    Insert into MAXTABLE (CALENDAR_YEAR,CALENDAR_PERIOD,AMOUNT,FUND,ACCOUNT,ORG,PROG) values (13,14,50,'202118','2010','3662','121');
    
    
    
    
    
    
    

    so now I want to run a query which would show me the total amounts by combination of accounting (Fund, account, org, prog) and I'd like to get it from calendar_year '15' and calendar_period ' 03 ".

    I need essentially back on the period and the year previous years courses and get the max (calendar_year) and the max (calendar_period) for the Fund, account, org, combination of prog that is not in the calendar_year '15' and calendar_period ' 03 "

    so the results would look something like below. Also the max_period and max_year could be in the same calendar year that I ask for this combination of accounting, an example of this is the 2070 account in the result below.

    I would like to run a query similar to this one

    SELECT
       CALENDAR_YEAR,
      CALENDAR_PERIOD,
      AMOUNT,
      FUND,
      ACCOUNT,
      ORG,
      PROG
    FROM MAXTABLE
    WHERE CALENDAR_PERIOD = '03'
    AND CALENDAR_YEAR = '15';
    
    
    
    
    
    
    

    but it would give me only the amounts on the table which exist for this year and the period. I also need to get the max calendar_year and max_calendar_period before calendar_period 03 and calendar_year 15 and add those to my request.

    .expected output:

    Calendar_Year calendar_period amount Fund account org PROG
    1522520211820713662121
    15315020211820703662121
    14125020211820403662121
    13145020211820103662121

    noticed that I'm not getting the $100 for account 2070 calendar_year question 14 and calendar_period ' 03 ". This point has already had a calendar_year max 15 and period 2 so I exclude it, since is less than 15 calendar_year but it is also a lot already this combination of accounting.

    Hope it makes sense.

    Sorry, I did not single-digit into account correctly.  Two years and by the need to be wrapped with to_char as:

    TO_CHAR (yr, 'fm00') | TO_CHAR (per, 'fm00')<=>

    John

  • Week of the year (from Monday to Sunday)

    Hi guys,.

    I searched 'Week of the year' and that you have not found a solution to my problem.

    I think that the standard Oracle 'IW' function returns the week of the year beginning January 1.  This year, for example, Monday, January 6 is back as week 1 because on 1 January was Wednesday, therefore, it is in the 1st 7 days of the year.

    Select to_char (to_date(:p_date,'DD/MM/YYYY'), 'IW') wrong_week_no

    , to_char (to_date (: p_date + 2, ' DD/MM/YYYY'), 'IW') correct_week_no

    of the double

    I would like the query to return the week number from Monday to Sunday, so in this case, the 6th should return as number 2 of the week.

    In the query above, by adding 2 to the date gives me the correct week number but won't work for 2015 and 2016.

    Any help would be appreciated.

    I use the Oracle 10 g platform.

    Thank you

    Chris

    Do you mean like this?

    SQL > break the month jump 1
    SQL > set linesize 200
    SQL > set pagesize 2000
    SQL > column month format a20
    SQL > column week format a4
    SQL > with req as (select '& Required_Year_YYYY' year of double)
    2, offset as (select case when to_char (trunc (to_date (yr, 'YYYY'), 'YYYY'), 'IW') in ('52 ', ' 53') then 1 else 0 end as the offset of req)
    3 select lpad (Month, - 20 (20-length (month)) / 2) months.
    4 '('|| semaine ||') ' as the week, "Mo", "You", "We", "Th", "Fr", "Sa", "knew".
    5 of)
    6. Select to_char (dt, 'fmMonth YYYY') month,
    7 case when to_char (dt, 'mm') = '12 ' and to_char (dt, 'iw') = ' 01' and offset = 0 then ' 53 "
    8 when to_char (dt, 'mm') = '12 ' and to_char (dt, 'iw') = '01' and offset = 1 then ' 54'
    9 when to_char (dt, 'mm') = '01' and to_char (dt, 'iw') and ('52', ' 53 ") then"1"
    10 to_char (to_number (to_char (dt, 'iw')) + offset) other end than the week,.
    11 max (decode (to_char(dt,'d'), '1', lpad (to_char (dt, 'GFMD') 2)),) "Mo."
    12 max (decode (to_char(dt,'d'),'2 ', lpad (to_char (dt, 'GFMD'), 2))) "you."
    Max (decode (to_char(dt,'d'),'3 ', lpad (to_char (dt, 'GFMD'), 2))) 13 '' We. ''
    14 max (decode (to_char(dt,'d'),'4 ', lpad (to_char (dt, 'GFMD'), 2))) "Th."
    15 max (decode (to_char(dt,'d'),'5 ', lpad (to_char (dt, 'GFMD'), 2))) "Fr."
    16 max (decode (to_char(dt,'d'),'6 ', lpad (to_char (dt, 'GFMD'), 2))) "his."
    17 max (decode (to_char(dt,'d'),'7 ', lpad (to_char (dt, 'GFMD'), 2))) "Su".
    18 in (select trunc (to_date (req.yr, 'YYYY'), 'y')-1 + rownum dt)
    19 object, req
    20 where rownum<= add_months(trunc(to_date(req.yr,'yyyy'),'y'),12)="" -="" trunc(to_date(req.yr,'yyyy'),'y')="">
    21, offset
    22 group by to_char (dt, 'fmMonth AAAA'), case when to_char (dt, 'mm') = '12 ' and to_char (dt, 'iw') = ' 01' and offset = 0 then ' 53 "
    23 when to_char (dt, 'mm') = '12 ' and to_char (dt, 'iw') = '01' and offset = 1 then ' 54'
    24 when to_char (dt, 'mm') = '01' and to_char (dt, 'iw') and ('52', ' 53 ") then"1"
    25 to_char (to_number (to_char (dt, 'iw')) + offset) to another end
    (26) x
    27 order by to_date (month, "Month YYYY"), to_number (x.week)
    28.
    Enter the value for required_year_yyyy: 2012
    old 1: with req as (select '& Required_Year_YYYY' year of double)
    new 1: with req (select '2012' year of double)

    MONTH WEEK Mo Tu we Th his di ve
    -------------------- ---- -- -- -- -- -- -- --
    January 2012 (1) 1
    (2) 2 3 4 5 6 7 8.
    (3) 9 10 11 12 13 14 15.


    (4) 16 17 18 19 20 21 22.
    (5) 23 24 25 26 27 28 29.
    (6) 30 31.

    February 2012 (6) 1 2 3 4 5
    (7) 6 7 8 9 10 11 12.
    (8) 13 14 15 16 17 18 19.
    (9) 20 21 22 23 24 25 26.
    (10) 27 28 29.

    March 2012 (10) 1 2 3 4
    (11) 5 6 7 8 9 10 11.
    (12) 12 13 14 15 16 17 18.
    (13) 19 20 21 22 23 24 25.
    (14) 26 27 28 29 30 31.

    April 2012 (14) 1
    (15) 2 3 4 5 6 7 8.
    (16) 9 10 11 12 13 14 15.
    (17) 16 17 18 19 20 21 22.
    (18) 23 24 25 26 27 28 29.
    (19) 30

    May 2012 (19) 1 2 3 4 5 6
    (20) 7 8 9 10 11 12 13.
    (21) 14 15 16 17 18 19 20.
    (22) 21 22 23 24 25 26 27.
    (23) 28 29 30 31.

    June 2012 (23) 1 2 3
    (24) 4 5 6 7 8 9 10.
    (25) 11 12 13 14 15 16 17.
    (26) 18 19 20 21 22 23 24.
    (27) 25 26 27 28 29 30.

    July 2012 (27) 1
    (28) 2 3 4 5 6 7 8.
    (29) 9 10 11 12 13 14 15.
    (30) 16 17 18 19 20 21 22.
    (31) 23 24 25 26 27 28 29.
    (32) 30 31.

    August 2012 (32) 1 2 3 4 5
    (33) 6 7 8 9 10 11 12.
    (34) 13 14 15 16 17 18 19.
    (35) 20 21 22 23 24 25 26.
    (36) 27 28 29 30 31.

    September 2012 (36) 1 2
    (37) 3 4 5 6 7 8 9.
    (38) 10 11 12 13 14 15 16.
    (39) 17 18 19 20 21 22 23.
    (40) 24 25 26 27 28 29 30.

    October 2012 (41) 1 2 3 4 5 6 7
    (42) 8 9 10 11 12 13 14.
    (43) 15 16 17 18 19 20 21.
    (44) 22 23 24 25 26 27 28.
    (45) 29 30 31.

    November 2012 (45) 1 2 3 4
    (46) 5 6 7 8 9 10 11.
    (47) 12 13 14 15 16 17 18.
    (48) 19 20 21 22 23 24 25.
    (49) 26 27 28 29 30.

    December 2012 (49) 1 2
    (50) 3 4 5 6 7 8 9.
    (51) 10 11 12 13 14 15 16.
    (52) 17 18 19 20 21 22 23.
    (53) 24 25 26 27 28 29 30.
    (54) 31

    64 selected lines

    I used 2012 for example, because in this case, it has 54 weeks.

    So, here we have 1 January during the week 1, but 2 January's second week, as is Monday, etc.

  • query to find the number of dates

    Hi all

    I need assistance in creating a query. Ask the details I have given below.

    Table:

    Create the table emp_record (emp_id number, emp_join_date date);

    insert into emp_record values (1, 1 June 14 ');

    insert into emp_record values (2, 26 May 14 ');

    insert into emp_record values (3, 16 May 14 ');

    insert into emp_record values (4, 14 May 14 ');

    Suppose I ran the query with sysdate, we need to subtract the date of sysdate, then a report should be prepared in function number how many have subtracted values sysdate below 0 to 3 days, 3-6 date.

    Output should be like

    0 - 3 3-6 6-9 days more than 9 days

    1              0             1                    2

    Please help to write this query. Thank you

    Hello

    Do not essentially repeat the same CASE expression with the same types of calculations for each column.  You can do the job with a CASE expression, like this:

    WITH got_group_id AS

    (

    SELECT THE CHECK BOX

    WHEN emp_join_date< trunc="" (sysdate)="" -="" 8 ="" then ="">

    WHEN emp_join_date< trunc="" (sysdate)="" -="" 5 ="" then ="">

    WHEN emp_join_date< trunc="" (sysdate)="" -="" 2 ="" then ="">

    0 OTHERWISE

    Group_id END AS

    Of emp_record

    WHERE emp_join_date<= sysdate ="" --="" if="">

    )

    SELECT *.

    OF got_group_id

    PIVOT (COUNT (*)

    FOR group_id IN (0 '0-2 days'

    , 3 AS a "3-5 days.

    6 as a "6-8 days"

    , 9 as "9 or more days.

    )

    )

    ;

    It will be much easier to maintain when needs change.

    ED mentioned, using a string, 1 June 14 ' in a place where we expect a date is just a matter of trouble.  2-digit years are too.

  • With the help of SQL, need to display the previous year, current year and the year next to single variable

    Hi all

    We have an obligation to state the previous year, current year and values of the year next as a LOV in the ADF page.

    This is the query that retrieves the previous year, current year and next year. But the result of this query displays values in 3 columns:

    Select (Extract(year from sysdate)-1), extract (year sysdate), (extract (year sysdate) + 1) twice;

    Output:

    2012 2013 2014


    But I want to display them in a single with 3 rows as column:

    2012

    2013

    2014


    Please your ideas.

    or in the same way

    SELECT EXTRACT (YEAR FROM ADD_MONTHS (SYSDATE, 12 *(LEVEL-2)))

    OF the double

    connect by level<>

  • SQL query date with century / year

    We have a database of oracle with about 6 million documents. There is a date field called Date entered from 1985 to the present day.
    Most of these recordings was entered into before January 1, 2000.
    If I run a query like
    Select count (*) (TableName) where entered_date < 1 January 00 ' I get 0
    If I do
    Select count (*) (TableName) where entered_date < 31 December 99 ' I get 0
    BUT IF I DO
    Select count (*) (TableName) where entered_date < 1 January 00 ' get 6 million documents
    or
    Select count (*) in the TREASURY.ctrc where entrydate > 31 December 99 ' get 6 million documents

    I tried the same queries using 4 digit years but get the same results. He thinks that 2000 is at least 1999
    How to do this?
    Thank you

    Hello

    975204 wrote:
    There are 6 million documents on the table
    about two-thirds have a date prior to 1 January 2000

    How do you know this? Knowledge of the application, you can find out that two-thirds of them are supposed to have dates from before 2000, but if

    SELECT  COUNT (*)
    FROM      TABLE_NAME
    WHERE      ENTRYDATE < TO_DATE ( '01-JAN-2000'
                           , 'DD-MON-YYYY'
                       )
    ;
    

    Returns the value 0, which has strong enough evidence that none of them actually do.

    When I look at the dates, they appear as December 31, 86

    Another example of why use 2 digit years is a bad idea.

    I can't actually provide these confidential customer data dump

    Seriously; You can provide an output DUMP? You already said that it displays as December 31, 86, so even if the fact that 1 entry in the table of the line 6 million was made on December 31, 1986 was such a big secret, he is already out, and you don't cause any harm more by showing the results of DUMP.

    I ran the query with the same format as the date, that is to say
    SELECT COUNT (*) FROM THE DEPARTMENT OF FINANCE. CRTC WHERE ENTRYDATE< to_date="" (="">
    "DD-MON-YY."
    )

    but same results
    If the column is defined as a DATE type, Oracle made a distinction based on the way the data is displayed, which means that she think that December 31, 86 is different from December 31, 1986?

    No, all the DATE columns have the same format. A DATE can be displayed in one way of the other, but it is stored in the form of or.

    Should I convert all the data from one year to 4 digits?

    A 4-digit year dates should be always displayed.

    Run an update query that said if year > = 80 and<= 99="" add="" 19="" in="" front="" of="" the="" year,="" else="" add="" 20="" in="" front="" of="" the="">

    We will find out exactly what the problem is, first of all.

    Have you seen the Ascheffer message? ^ 1 ^ it was dated just a couple of minutes before your last post, so you might not notice. Run it to see what are the years of 4 real numbers.
    If it shows, for example, there are a lot of entrydates in the years 2080 to 2099, and if you decide that all who should really be 100 years earlier, use ADD_MONTHS to remedy:

    UPDATE     table_name
    SET     entrydate = ADD_MONTHS ( entrydate
                               , -100 * 12
                          )
    WHERE   entrydate >= TO_DATE ( '01-JAN-2080'
                             , 'DD-MON-YYYY'
                        )
    AND     entrydate <  TO_DATE ( '01-JAN-2100'
                             , 'DD-MON-YYYY'
                        )
    ;
    

    Published by: Frank Kulash, 15 March 2013 18:09

    ^ 1 ^ you see of course, message Ascheffer; I was still typing the message above when you posted another.

  • Summary of the sum of the year or Analytics

    I would like to find a way to summarize some data and do calculations without Hardcoding. Trying to add up the numbers by year and then show the numbers for 4 years and 6 years without Hardcoding if possible. 4 years would be calculated by adding 390 to the cohortterm (390 + 200540 = 200930) 6 years would be calculated by adding the cohortterm (200540 + 590 = 201130) 590.

    The last 2 passes, that I haven't done yet would be a percentage based on Gradyr4 divided by the staff and the other for Gradyr6 divided by membership. If the WPS is too complicated, I can copy the results to excel for this part. Eventually, I'll have to further break down charges according to sex and the membership ethnic, but if all goes well, I can understand it once I know the syntax to make the number.

    BANNER
    Oracle Database 11 g Enterprise Edition Release 11.2.0.2.0 - 64 bit Production

    Example of output based on the query I've built so far using sum and hard coding for one year only. Actual results would show account and not from 0 in the lines of the year displayed below:
    COHORTTERM     HEADCOUNT     GRADYR4     GRADYR6     PCT4Yr  PCT6Yr
    200340          0          0     0
    200440          0          0     0
    200540          600          403     539     (403/600)
    200640          0          0     0
    200740          0          0     0
    SQL
    select cohortterm, 
             sum(case when cohortterm = '200540' then 1 else 0 end) as headcount,
             sum(
                    CASE WHEN cohortterm = '200540' and gradtermban < ''200930' then 1 else 0 end
                    ) as gradyr4,
                    
             sum(
                    CASE WHEN cohortterm = '200540' and gradtermban < '201130' then 1 else 0 end
                    ) as gradyr6
             null as pct4yr, null as pct6yr       
                   
    from cohorts
    where cohortterm between '200340' and '201240'
    and substr(ftpt,1,1) = 'F'
    group by cohortterm
    order by 1

    Based on your code example, here's a guess at what you want. You just need to extract the part of the year, add 4 or 6, then replace with the correct value for the period. Put two bits of information in the same column is also against the theory of database "normal form".

    DROP TABLE T;
    CREATE TABLE T(COHORTTERM VARCHAR2(6), GRADTERMBAN VARCHAR2(6));
    INSERT ALL
    into t values('200304', '200620')
    INTO T VALUES('200304', '200720')
    INTO T VALUES('200304', '200820')
    INTO T VALUES('200304', '200920')
    INTO T VALUES('200404', '200620')
    INTO T VALUES('200404', '200720')
    INTO T VALUES('200404', '200820')
    INTO T VALUES('200404', '200920')
    SELECT NULL FROM DUAL;
    
    SELECT
    COUNT(*) CNT,
    SUM(CASE WHEN GRADTERMBAN < TO_NUMBER(SUBSTR(COHORTTERM,1,4))+4||'03' THEN 1 END) YEAR4,
    sum(case when gradtermban < TO_NUMBER(SUBSTR(COHORTTERM,1,4))+6||'03' then 1 end) year6
    FROM T
    group by cohortterm;
    
           CNT      YEAR4      YEAR6
    ---------- ---------- ----------
             4          2          4
             4          1          3
    

Maybe you are looking for

  • I can't click on anything on the top of my screen... Help!

    HelloWhen I arrived at firefox, it worked great, and I fell in love with her. But recently, something happened. For some reason I can't click on anything in the top of my screen. For example, if I go on Facebook, you know how to type you email and pa

  • After the 3.5 update, my facebook page has been reduced to about 1/2 normal sie

    After update to Firefox 3.5, my Facebook page has been reduced to about 1/2 the normal size. How to restore it normal?

  • Show SOUND

    Salvation Skype community.My Skype does not-then on my sound troubleshooting I studiedWIFIOn my bar of laptop on the right of ITSThe above mentioned parameters works very well, since I was also listening to music on the internet.Then, I tried to foll

  • toDataURL("image/jpeg") production distorted images

    I do an application that allows the user to crop an image taken with the phone or selected from the system files using HTML5 canvas. The jpeg base64 resulting however, often has distortion, discoloration or pixelation on the lower part of the image a

  • Launch a VPN from a cisco router on the LAN behind the ASA?

    We currently have an ASA with used site to site VPN and anyconnect VPN. We received a third party cisco router that will be used to launch their own VPN site to site of inside our LAN to their local network through our ASA. 1 NAT Traversal would call