How to do this in sql?

m doing a quarterly report and my database only collects the year and month fields.
i currently have this condition in my periodic/monthly report
WHERE
period= '@Request.Period~' and
year = '@Request.Year~'
how do you do this so that when the user inputs quarter 1, the condition becomes "from period 1 to 3". when the user inputs quarter 2, the condition becomes "from period 4 to 6" etc?

>well, the users input would only be 1,2,3,4 which stands for the quarter.
I misunderstood your original post. Please try;
old: with t as (
   select to_date('01-JAN-2007','DD-MON-YYYY') dt from dual union all
   select to_date('01-FEB-2007','DD-MON-YYYY') dt from dual union all
   select to_date('28-FEB-2007','DD-MON-YYYY') dt from dual union all
   select to_date('31-MAR-2007','DD-MON-YYYY') dt from dual union all
   select to_date('01-APR-2007','DD-MON-YYYY') dt from dual union all
   select to_date('30-APR-2007','DD-MON-YYYY') dt from dual)
select dt
from t
where trunc(dt,'Q')= add_months(trunc(to_date('&year','YYYY'),'y'),(&quarter-1)*3)
new: with t as (
   select to_date('01-JAN-2007','DD-MON-YYYY') dt from dual union all
   select to_date('01-FEB-2007','DD-MON-YYYY') dt from dual union all
   select to_date('28-FEB-2007','DD-MON-YYYY') dt from dual union all
   select to_date('31-MAR-2007','DD-MON-YYYY') dt from dual union all
   select to_date('01-APR-2007','DD-MON-YYYY') dt from dual union all
   select to_date('30-APR-2007','DD-MON-YYYY') dt from dual)
select dt
from t
where trunc(dt,'Q')= add_months(trunc(to_date('2007','YYYY'),'y'),(1-1)*3)
SQL> with t as (
   select to_date('01-JAN-2007','DD-MON-YYYY') dt from dual union all
   select to_date('01-FEB-2007','DD-MON-YYYY') dt from dual union all
   select to_date('28-FEB-2007','DD-MON-YYYY') dt from dual union all
   select to_date('31-MAR-2007','DD-MON-YYYY') dt from dual union all
   select to_date('01-APR-2007','DD-MON-YYYY') dt from dual union all
   select to_date('30-APR-2007','DD-MON-YYYY') dt from dual)
select dt
from t
where trunc(dt,'Q')= add_months(trunc(to_date('2007','YYYY'),'y'),(1-1)*3)
DT      
01-JAN-07
01-FEB-07
28-FEB-07
31-MAR-07
4 rows selected.

Similar Messages

  • How to Achieve this in SQL Query?

    How to Achieve this ?
    I have a table with numeric value populated like this
    create table random_numeral (numerals Number(10));
    insert into random_numeral values (1);
    insert into random_numeral values (2);
    insert into random_numeral values (3);
    insert into random_numeral values (4);
    insert into random_numeral values (5);
    insert into random_numeral values (6);
    insert into random_numeral values (56);
    insert into random_numeral values (85);
    insert into random_numeral values (24);
    insert into random_numeral values (11);
    insert into random_numeral values (120);
    insert into random_numeral values (114);
    Numerals
    1
    2
    3
    4
    5
    6
    56
    85
    24
    11
    120
    114
    I want to display the data as follows
    col1 / col2 / col3
    1 / 2 / 3
    4 / 5 / 6
    11 / 24 / 56
    85 / 114 / 120
    Can anyone Help me?

    I hope there might be some simple way to do this and waiting for experts to reply.
    Try the below query.
    SQL> select * from random_numeral;
      NUMERALS
             1
             2
             3
             4
             5
             6
            56
            85
            24
            11
           120
      NUMERALS
           114
           100
           140
    14 rows selected.
    SQL> select a.numerals ||' / '||b.numerals||' / '||c.numerals from
      2          (select numerals,rownum rn1 from
      3          (
      4              select numerals,mod(row_number() over(partition by 1 order by numerals),3)
      5              from random_numeral
      6          )
      7          where rn=1) a,
      8          (select numerals,rownum rn1 from
      9          (
    10              select numerals,mod(row_number() over(partition by 1 order by numerals),3)
    11              from random_numeral
    12          )
    13          where rn=2) b,
    14          (select numerals,rownum rn1 from
    15          (
    16              select numerals,mod(row_number() over(partition by 1 order by numerals),3)
    17              from random_numeral
    18          )
    19          where rn=0) c
    20  where   a.rn1=b.rn1(+)
    21  and b.rn1=c.rn1(+)
    22  /
    A.NUMERALS||'/'||B.NUMERALS||'/'||C.NUMERALS
    1 / 2 / 3
    4 / 5 / 6
    11 / 24 / 56
    85 / 100 / 114
    120 / 140 /
    SQL>Cheers,
    Mohana

  • How to Improve this PL/SQL script

    Hi All,
    I have a package/procedure that insert data into table but it takes time more than 2 days. Have any known how to modify this script to improve the performance and reduce loading time, The following code is procedure I used to insert data.....
    Procedure INSERT_DATA (p_month IN DATE, p_product_id IN NUMBER ) IS
    cursor c1 is select * from tab#1; --reference data
    cursor c2 is select * from tab#2;
    cursor c3 is select * from tab#3;
    cursor c4 is select * from tab#4;
    cursor c5 is select * from tab#5;
    v_rec claim_table%rowtype;
    Begin
    for c1rec in c1 loop
    exit when c1%notfound;
    call procedure in package....;
    open c2(c1rec.claim_no);
    fetch c2 into v_location_cd ,v_claim_type_cd ;
    close c2;
    v_rec.location_cd := v_location_cd;
    v_rec.claim_type_cd := v_claim_type_cd ;
    open c3(c1rec.claim_no);
    fetch c3 into v_col#3,v_col#4;
    close c3;
    v_rec.col#3 := v_col#3 ;
    v_rec.col#4 := v_col#4 ;
    open c4(c1rec.claim_no);
    fetch c4 into v_col#5,v_col#6;
    close c4;
    v_rec.col#5 := v_col#5 ;
    v_rec.col#6 := v_col#6 ;
    insert into claim_table values ( v_rec.location_cd, v_rec.claim_type_cd , v_rec.col#3 , .......) ;
    if (c1%rowcount/1000) = trunc(c1%rowcount /1000) then
         commit;
    end if;
    end loop;
    commit;
    Exception
    exception statement....
    commit;
    End;
    Thanks All,
    Mcka

    A copy and paste of a reply I posted just a hour or so ago to the exact same approach used by a poster in [url http://forums.oracle.com/forums/thread.jspa?threadID=636929]this thread.
    Yucky code IMO. You are using PL/SQL to drive a nested loop join. Why? SQL is by far more capable of joining tables and is not limited to a using a nested loop approach only.
    Also, as you are using PL/SQL to drive it, it means that each cursor fetch in each (nested) FOR loop is a slow context switch from PL/SQL to SQL, in order to ship the row's data from the SQL engine to the PL/SQL engine. Only then to have that very same data shipped back (via yet another context switch) to the SQL engine to be inserted into table4.
    This code violates one of the most rudimentary performance principles in Oracle - it is not maximizing SQL and it it not minimizing PL/SQL. It is doing the exact opposite.
    As for the ad-hoc commits inside the loop - this does not make much sense. This will generate more redo and more overheads. Also, when something goes pear shape in that process, some rows would have been processed and inserted and committed. Some not.
    In this case, how do you expect to restart the failed process at the very first row that failed to be committed and continue processing from there?

  • Here is one for the tekkies. How to do this in SQL?

    Input : String 541862356
    Table has 2 columns:
    SNO                             SLENGTH
    5418                                4
    54186                               5
    54321                               5
    Output should be 54186.
    We don't have any other input. i.e. we have to get the closest match for the input string.
    How to do this??
    Edited by: user12240205 on May 3, 2012 10:51 PM

    user12240205 wrote:
    We don't have any other input. i.e. we have to get the closest match for the input string.You haven't explained what "closest match" actually means.
    Something as simple as this could do what you want with the sample data you've provided...
    SQL> ed
    Wrote file afiedt.buf
      1  WITH t AS (SELECT 5418 sno, 4 cnt FROM DUAL UNION
      2             SELECT 54186, 5 FROM DUAL UNION
      3             SELECT 54321, 5 FROM DUAL)
      4      ,r as (select 541862356 as r from dual)
      5  --
      6  -- end of test data
      7  --
      8  select max(sno)
      9  from   t,r
    10* where  instr(r, sno) > 0
    SQL> /
      MAX(SNO)
         54186... without the need for any Henry Winkler matching stuff (as we affectionately call it round here).

  • How to transform this PL/SQL request into a PL/SQL function ?

    Hi nice people !
    I have a beautiful flash chart which SQL code is below. I want to display this chart only if this query returns results. I tried to put it as condition. How to transformt this SQL ciode as "SLQ Function returning a boolean" ?
    DECLARE
    X VARCHAR2 (4000);
    BEGIN
    x :=
    'SELECT NULL LINK, trunc(a.DATE1, '''||:P13_TRUNC||'''), avg(b.pourcentage_remplissage)
    FROM    EVV_'||:P13_SITE||' a, bassin_remplissage b
    WHERE   a.CLEF_VAR = (SELECT CLEF_VAR FROM SITE_ECHELLE WHERE SITE='''||:P13_SITE||''')
    AND     round(a.valeur, 1) = b.cote_ngf
    AND     DATE1 BETWEEN
    TO_DATE ('''||:P13_DATE_DEBUT||'000000'', ''DD/MM/YYYYHH24MISS'') AND
    TO_DATE ('''||:P13_DATE_FIN||'235959'', ''DD/MM/YYYYHH24MISS'')
    group by trunc(a.DATE1, '''||:P13_TRUNC||''')
    ORDER BY trunc(a.DATE1, '''||:P13_TRUNC||''')';
    RETURN (X);
    END;Thank you for your kind answers.
    Christian

    Hello Christian,
    >> I don't understand your input
    Your error is most likely because the dynamic select statement you are trying to build is not correct. As local variables, like the ‘x’ you are using, are not persistence, it’s probably hard for you to see the final value of x.
    You should define an application item, like TEMP1, and use it to hold the generated select statement:
    BEGIN
    :TEMP1 := 'SELECT COUNT(*) INTO WWV_FLOW.g_id
    FROM    EVV_'
          || :p13_site
          || ' a, bassin_remplissage b
    WHERE   a.CLEF_VAR = (SELECT CLEF_VAR FROM SITE_ECHELLE WHERE SITE='''
          || :p13_site
          || ''')
    AND     round(a.valeur, 1) = b.cote_ngf
    AND     DATE1 BETWEEN
    TO_DATE ('''
          || :p13_date_debut
          || '000000'', ''DD/MM/YYYYHH24MISS'') AND
    TO_DATE ('''
          || :p13_date_fin
          || '235959'', ''DD/MM/YYYYHH24MISS'')
    group by trunc(a.DATE1, '''
          || :p13_trunc
          || ''')
    ORDER BY trunc(a.DATE1, '''
          || :p13_trunc
          || ''')';
    return false;
    END;Now the process should be evaluated without errors, and TEMP1 should hold the select statement that produces the error. Now, using the developer toolbar, you can inspect the actual select statement ant analyze its syntax and format.
    Once you’ll find the error in the select statement, you can go back to Denes original code proposal.
    Regards,
    Arie.
    Please remember to mark appropriate posts as correct/helpful. For the long run, it will benefit us all.

  • How to Update this T-SQL Table?

    Hello,
    I am having trouble finding the correct syntax for updating this T-SQL table.
    Here is my Query which is adding an additional ' at the beginning and end.
    Query:
        Update [denodo].[dbo].[wrappers]
      SET wrapperWhereClause = '''AND County_Input=''Cheyenne & Arapaho Tribal Court'' AND WrapperID=729'''
      WHERE wrapperID = '729'
    The Outcome:
    'AND County_Input='Cheyenne & Arapaho Tribal Court' AND WrapperID=729'
    The Outcome I am hoping for:
    AND County_Input='Cheyenne & Arapaho Tribal Court' AND WrapperID=729
    Can anyone help me edit this so I can receive the outcome I am hoping for?

    >> Please learn
    I am having trouble finding the correct syntax for updating this T-SQL table. Here is my Query [sic] which is adding an additional ' at the beginning and end. <<
    Please learn basic terms so
    we can communicate. UPDATE is a statement and not a query. You do not know ISO-11179 rules, etc.
    Why are you building statements in SQL? You are doing bad SQL programming
    and need to stop. Your silly meta-data “wrapper_where_clause” and “county_input” is so fundamentally wrong I would fire you. Look at what I have done
    with SQL and see what a criticism that is!
    UPDATE Wrappers
    SET ??
    WHERE county_something =
    'Cheyenne & Arapaho Tribal Court'
    AND wrapper_id = '729';
    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
    in Sets / Trees and Hierarchies in SQL

  • How to do this in SQL or User defined Function?

    Hello All
    I have a table STATUS_CODES which has values like this
    HRKEY ITEMKEY
    HRAS 1
    HVAS 1
    HXAS 1
    SMST 2
    SLRS 2
    HWAS 2
    PAYT 3
    The HRKEY is unique
    Same ITEMKEY may be applied to different HRKEY (Item keyhas fixed values 1, 2, 3)
    Now I have a function which can return values like one of these(any combination of HRKEYseparated by a dash) which is captured in a variable called LAST_STATUS
    HRAS-PAYT
    HRAS-SMST-PAYT
    PAYT
    HRAS
    HXAS
    HRAS-HVAS-PAYT
    HRAS-HVAS-SMST-SLRS-PAYT
    HRAS-HVAS-SMST
    Now I want my query or function to do this
    If one of the values in the LAST_STATUS string matches STATUS_CODES.HRKEY, then it should return that HRKEY based on the priority below
    If Itemkey = 1 then it should return HRKEY for ITEMKEY = 1
    Then 2 and then 3
    If there are more than one HRKEY for same ITEMKEY then it should return any one of them
    SO for eg.
    If LAST_STATUS = HRAS-HVAS-SMST-SLRS-PAYT, then Output = HRAS
    If LAST_STATUS = SLRS-PAYT, then Output = SLRS
    If LAST_STATUS = PAYT, then Output = PAYT
    Thank you for your help
    Ach

    With Scott’s setup script …
    flip@FLOP> var str varchar2(1000);
    flip@FLOP> exec :str := 'HRAS-HVAS-SMST-SLRS-PAYT';
    PL/SQL procedure successfully completed.
    flip@FLOP>
    flip@FLOP> with a as
      2       ( select :str last_status from dual )
      3      ,b as
      4       (select '-'||last_status str, rownum rn from a connect by level <= length(last_status) )
      5      ,c as
      6       (select str, rn
      7              ,instr(str,'-',1,rn  ) f
      8              ,instr(str,'-',1,rn+1) t
      9        from   b
    10        where  instr(str,'-',1,rn) > 0
    11       )
    12      ,d as
    13       ( select c.*, decode(t,0,substr(str,f+1),substr(str,f+1,t-f-1)) x
    14         from  c
    15       )
    16      ,e as
    17       (
    18  select d.*, s.itemkey, row_number() over (order by s.itemkey, d.x) rnk
    19  from   d, status_codes s
    20  where  d.x = s.hrkey
    21       )
    22  select x hrkey
    23  from   e
    24  where  rnk = 1
    25  ;
    HRKEY
    HRAS
    flip@FLOP> exec :str := 'SLRS-PAYT'
    PL/SQL procedure successfully completed.
    flip@FLOP> /
    HRKEY
    SLRS
    flip@FLOP> exec :str := 'PAYT'
    PL/SQL procedure successfully completed.
    flip@FLOP> /
    HRKEY
    PAYT
    flip@FLOP> exec :str := null;
    PL/SQL procedure successfully completed.
    flip@FLOP> /
    no rows selected
    flip@FLOP> exec :str := 'HRAS-HVAS----SMST-SLRS-PAYT-HRAS-HVAS-PAYT-HRAS-HVAS-SMST-SLRS-PAYT-HRAS-HVAS-SMST-SLRS-PAYT--';
    PL/SQL procedure successfully completed.
    flip@FLOP> /
    HRKEY
    HRAS

  • How to tune this smiple SQL (takes long time to come up with results)

    the following SQL is very slow as it takes one day to complete...
    select A.* from (SELECT a.n_agent_no, a.v_agent_code, a.n_channel_no, v_iden_no, a.n_cust_ref_no, a.v_agent_type, a.v_company_code,
    a.v_company_branch, a.v_it_no, bfn_get_agent_name(a.n_agent_no) agentname,
    PKG_AGE__TAX.GET_TAX_AMT(:P_FROM_DATE,:P_TO_DATE,:P_LOB_CODE,A.N_AGENT_NO) comm,
    c.v_ird_region
    FROM agent_master a, agent_lob b, agency_region c
    WHERE a.n_agent_no = b.n_agent_no
    AND a.v_agency_region = c.v_agency_region
    --AND :p_lob_code = DECODE(:p_lob_code,'ALL', 'ALL',b.v_line_of_business)
    --AND :p_channel_no = DECODE(:p_channel_no,1000, 1000,a.n_channel_no)
    --AND :p_agency_group = DECODE(:p_agency_group,'ALL', 'ALL',c.v_ird_region)
    group by a.n_agent_no, a.v_agent_code, a.n_channel_no, v_iden_no, a.n_cust_ref_no, a.v_agent_type, a.v_company_code, a.v_company_branch, a.v_it_no, bfn_get_agent_name(a.n_agent_no) ,
    BPG_AGENCY_GEN_ACL_TAX.BFN_GET_TAX_AMOUNT(:P_FROM_DATE,:P_TO_DATE,:P_LOB_CODE,A.N_AGENT_NO),
    c.v_ird_region
    ORDER BY c.v_ird_region, a.v_agent_code DESC)
    A
    WHERE (COMM < :P_VAL_IND OR COMM >=:P_VAL_IND1);
    . .it should return all the agents with commission based on the date parameter... data is less then 50 K inside all
    the tables...
    the version is Oracle9i Enterprise Edition Release 9.2.0.5.0
    SQL>  explain plan for
      2   select A.* from (SELECT a.n_agent_no, a.v_agent_code, a.n_channel_no, v_iden_no, a.n_cust_ref_
    no, a.v_agent_type, a.v_company_code,
      3  a.v_company_branch, a.v_it_no, bfn_get_agent_name(a.n_agent_no) agentname,
      4  BPG_AGENCY_GEN_ACL_TAX.BFN_GET_TAX_AMOUNT(:P_FROM_DATE,:P_TO_DATE,:P_LOB_CODE,A.N_AGENT_NO) com
    m,
      5  c.v_ird_region
      6  FROM ammm_agent_master a, ammt_agent_lob b, gnlu_agency_region c
      7  WHERE a.n_agent_no = b.n_agent_no
      8  AND a.v_agency_region = c.v_agency_region
      9  --AND :p_lob_code = DECODE(:p_lob_code,'ALL', 'ALL',b.v_line_of_business)
    10  --AND :p_channel_no = DECODE(:p_channel_no,1000, 1000,a.n_channel_no)
    11  --AND :p_agency_group = DECODE(:p_agency_group,'ALL', 'ALL',c.v_ird_region)
    12  group by a.n_agent_no, a.v_agent_code, a.n_channel_no, v_iden_no, a.n_cust_ref_no, a.v_agent_ty
    pe, a.v_company_code, a.v_company_branch, a.v_it_no, bfn_get_agent_name(a.n_agent_no) ,
    13  BPG_AGENCY_GEN_ACL_TAX.BFN_GET_TAX_AMOUNT(:P_FROM_DATE,:P_TO_DATE,:P_LOB_CODE,A.N_AGENT_NO),
    14  c.v_ird_region
    15  ORDER BY c.v_ird_region, a.v_agent_code DESC)
    16  A
    17  WHERE (COMM < :P_VAL_IND OR COMM >=:P_VAL_IND1);
    Explained.
    SQL>  select * from table(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    | Id  | Operation                       |  Name               | Rows  | Bytes |TempSpc| Cost (%CPU)|
    |   0 | SELECT STATEMENT                |                     | 13315 |    27M|       |   859  (63)|
    |   1 |  VIEW                           |                     | 13315 |    27M|       |            |
    |   2 |   SORT GROUP BY                 |                     | 13315 |   936K|  2104K|   859  (63)|
    |   3 |    HASH JOIN                    |                     | 13315 |   936K|       |   641  (81)|
    |   4 |     MERGE JOIN                  |                     |  3118 |   204K|       |   512  (86)|
    |   5 |      TABLE ACCESS BY INDEX ROWID| AGENCY_REGION  |     8 |   152 |       |     3  (34)|
    |   6 |       INDEX FULL SCAN           | SYS_C004994         |     8 |       |       |     2  (50)|
    |   7 |      SORT JOIN                  |                     |  3142 |   147K|       |   510  (86)|
    |   8 |       TABLE ACCESS FULL         | AGENT_MASTER   |  3142 |   147K|       |   506  (86)|
    |   9 |     TABLE ACCESS FULL           | AGENT_LOB      |   127K|   623K|       |   102  (50)|
    Note: PLAN_TABLE' is old version
    17 rows selected.
    ..This is the only information i can get as i cannot access over database server (user security limitation)...
    Thank You

    Try to remove this:
    ORDER BY c.v_ird_region, a.v_agent_code DESCOr move it to the end of entire query.
    Edited by: Random on Jun 19, 2009 1:01 PM

  • How to implement this in SQL?

    Assure that there is table t1 with the following fields and values:
    Field1 field2 Field3
    A         B          10
    A        C      20
    And there is another table t2 with the following fields and values
    Field2
    B
    C
    D
    How to write a query to join the two tables with the following output
    A         B          10
    A        C      20
    A     D      NULL
    Thank you in advance
    Regards,
    Marser

    Assure that there is table t1 with the following fields and values:
    Field1 field2 Field3
    A         B          10
    A        C      20
    And there is another table t2 with the following fields and values
    Field2
    B
    C
    D
    How to write a query to join the two tables with the following output
    A         B          10
    A        C      20
    A     D      NULL
    Thank you in advance
    Regards,
    Marser
    Looks like you can achieve this by using LEFT JOIN, but have query in the logic( even though nothing is explained in the post), if the value is NULL for D, how the first column contains A? isn't is supposed to be NULL, or if it has some other logic, post it.
    Please use Marked as Answer if my post solved your problem and use
    Vote As Helpful if a post was useful.

  • How to achieve this in SQL?

    Hi Guys,
    I'm stuck in one place. Here is my problem explained below -
    Table definition is -
    Column name          Data Type          Nullable
    FLT_NBR           CHAR(5 BYTE)     No
    CARR_IATA_CD     CHAR(4 BYTE)     No
    FLT_LCL_ORIG_DT     DATE                 No
    ORIG_ARPT_CD     CHAR(3 BYTE)     No
    DEST_ARPT_CD     CHAR(3 BYTE)     No
    ORIG_OCCUR_NBR     NUMBER(3,0)          No
    DEST_OCCUR_NBR     NUMBER(3,0)          No
    SCHED_LEG_NBR     NUMBER(3,0)          No
    NOSE_NBR          CHAR(4 BYTE)     Yes
    GMT_EST_DEP_DTM     DATE               Yes
    GMT_EST_ARR_DTM     DATE               Yes
    TAIL_NBR          CHAR(6 BYTE)     Yes
    ACTIVE_IND          NUMBER(1,0)          YesEach record of the table is segment. This will applicable on all the data of that given table.
    Consider the following case -
    SQL Is -
    select o.CARR_IATA_CD,
           o.FLT_NBR,
           o.FLT_LCL_ORIG_DT,
           o.ORIG_ARPT_CD,
           o.SCHED_LEG_NBR,
           o.DEST_ARPT_CD,
           o.ORIG_OCCUR_NBR,
           o.DEST_OCCUR_NBR,
           o.NOSE_NBR,
           o.TAIL_NBR,
           to_char(o.GMT_EST_ARR_DTM,'DD-MON-YYYY hh24:mi:ss') GMT_EST_ARR_DTM,
           to_char(o.GMT_EST_DEP_DTM,'DD-MON-YYYY hh24:mi:ss') GMT_EST_DEP_DTM
    from ops_flt_leg o
    where o.flt_nbr = '109'  
    and   o.orig_arpt_cd = 'ORD'
    and   o.dest_arpt_cd = 'ORD'
    and   o.flt_lcl_orig_dt = to_date('30-APR-2008','DD-MON-YYYY')
    Output -
    CARR FLT_N FLT_LCL_O ORI SCHED_LEG_NBR DES ORIG_OCCUR_NBR DEST_OCCUR_NBR NOSE TAIL_N GMT_EST_ARR_DTM                                                             GMT_EST_DEP_DTM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
    UA   109   30-APR-08 ORD             1 ORD              0              1 5688        30-APR-2008 15:34:00                                                        30-APR-2008 14:59:00               Determination of 2nd condition is -
    SQL is -
    select o.CARR_IATA_CD,
           o.FLT_NBR,
           o.FLT_LCL_ORIG_DT,
           o.ORIG_ARPT_CD,
           o.SCHED_LEG_NBR,
           o.DEST_ARPT_CD,
           o.ORIG_OCCUR_NBR,
           o.DEST_OCCUR_NBR,
           o.NOSE_NBR,
           o.TAIL_NBR,
           to_char(o.GMT_EST_ARR_DTM,'DD-MON-YYYY hh24:mi:ss') GMT_EST_ARR_DTM
    from ops_flt_leg o
    where o.active_ind=1 
    and   o.nose_nbr  =5688
    and   o.dest_arpt_Cd = 'ORD'
    order by o.gmt_est_arr_dtm   N.B.: Check that the 2nd query is receiving values from 1st query in the following columns -
    NOSE_NBR - which will be nose_nbr from the first query.
    dest_arpt_Cd - Which will be orig_arpt_cd from the first query.
    Output -
    CARR FLT_N FLT_LCL_O ORI SCHED_LEG_NBR DES ORIG_OCCUR_NBR DEST_OCCUR_NBR NOSE TAIL_N GMT_EST_ARR_DTM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
    UA   106   28-APR-08 LAX             1 ORD              0              0 5688        28-APR-2008 20:38:00                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
    UA   906   29-APR-08 SFO             1 ORD              0              0 5688        29-APR-2008 21:38:00                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
    UA   819   30-APR-08 EWR             1 ORD              0              0 5688        30-APR-2008 13:36:00                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
    UA   109   30-APR-08 ORD             1 ORD              0              1 5688        30-APR-2008 15:34:00                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
    UA   122   02-MAY-08 LAX             1 ORD              0              0 5688        03-MAY-2008 03:31:00                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
    UA   940   04-MAY-08 DEN             1 ORD              0              0 5688        04-MAY-2008 21:52:00                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
    UA   1130  06-MAY-08 YVR             1 ORD              0              0 5688        06-MAY-2008 21:16:00                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
    UA   566   07-MAY-08 SMF             1 ORD              0              0 5688        07-MAY-2008 21:15:00                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
    UA   110   08-MAY-08 LAX             1 ORD              0              0 5688        08-MAY-2008 21:07:00                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
    UA   566   10-MAY-08 SMF             1 ORD              0              0 5688        10-MAY-2008 21:28:00                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
    UA   110   11-MAY-08 LAX             1 ORD              0              0 5688        11-MAY-2008 21:07:00                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
    UA   122   12-MAY-08 LAX             1 ORD              0              0 5688        13-MAY-2008 02:52:00                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

    This should do it. Note that the only thing that is hard coded with this query is the where clause for query 1 (aliased as q1 and joined to q1 as an inline view) Just replace the hard coded values with appropriate binds and you should be good to go.
    --<"Begin Sample Data">
    with ops_flt_leg as (
      select 'UA' CARR_IATA_CD
           , '106' FLT_NBR
           , to_date('28-APR-08','dd-mon-yy') FLT_LCL_ORIG_DT
           , 'LAX' ORIG_ARPT_CD
           , 1 SCHED_LEG_NBR
           , 'ORD' DEST_ARPT_CD
           , 0 ORIG_OCCUR_NBR
           , 0 DEST_OCCUR_NBR
           , '5688' NOSE_NBR
           , cast(null as char(6)) TAIL_NBR
           , to_date('28-APR-2008 20:38:00','dd-mon-yyyy hh24:mi:ss') GMT_EST_ARR_DTM
           , to_date('30-APR-2008 14:59:00','dd-mon-yyyy hh24:mi:ss') GMT_EST_DEP_DTM
           , 1 ACTIVE_IND
        from dual
      union all select 'UA', '906', to_date('29-APR-08','dd-mon-yy'), 'SFO', 1, 'ORD', 0, 0, '5688', null, to_date('29-APR-2008 21:38:00','dd-mon-yyyy hh24:mi:ss'), to_date('30-APR-2008 14:59:00','dd-mon-yyyy hh24:mi:ss'), 1 from dual
      union all select 'UA', '819', to_date('30-APR-08','dd-mon-yy'), 'EWR', 1, 'ORD', 0, 0, '5688', null, to_date('30-APR-2008 13:36:00','dd-mon-yyyy hh24:mi:ss'), to_date('30-APR-2008 14:59:00','dd-mon-yyyy hh24:mi:ss'), 1 from dual
      union all select 'UA', '109', to_date('30-APR-08','dd-mon-yy'), 'ORD', 1, 'ORD', 0, 1, '5688', null, to_date('30-APR-2008 15:34:00','dd-mon-yyyy hh24:mi:ss'), to_date('30-APR-2008 14:59:00','dd-mon-yyyy hh24:mi:ss'), 1 from dual
      union all select 'UA', '122', to_date('02-MAY-08','dd-mon-yy'), 'LAX', 1, 'ORD', 0, 0, '5688', null, to_date('03-MAY-2008 03:31:00','dd-mon-yyyy hh24:mi:ss'), to_date('30-APR-2008 14:59:00','dd-mon-yyyy hh24:mi:ss'), 1 from dual
      union all select 'UA', '940', to_date('04-MAY-08','dd-mon-yy'), 'DEN', 1, 'ORD', 0, 0, '5688', null, to_date('04-MAY-2008 21:52:00','dd-mon-yyyy hh24:mi:ss'), to_date('30-APR-2008 14:59:00','dd-mon-yyyy hh24:mi:ss'), 1 from dual
      union all select 'UA', '1130',to_date('06-MAY-08','dd-mon-yy'), 'YVR', 1, 'ORD', 0, 0, '5688', null, to_date('06-MAY-2008 21:16:00','dd-mon-yyyy hh24:mi:ss'), to_date('30-APR-2008 14:59:00','dd-mon-yyyy hh24:mi:ss'), 1 from dual
      union all select 'UA', '566', to_date('07-MAY-08','dd-mon-yy'), 'SMF', 1, 'ORD', 0, 0, '5688', null, to_date('07-MAY-2008 21:15:00','dd-mon-yyyy hh24:mi:ss'), to_date('30-APR-2008 14:59:00','dd-mon-yyyy hh24:mi:ss'), 1 from dual
      union all select 'UA', '110', to_date('08-MAY-08','dd-mon-yy'), 'LAX', 1, 'ORD', 0, 0, '5688', null, to_date('08-MAY-2008 21:07:00','dd-mon-yyyy hh24:mi:ss'), to_date('30-APR-2008 14:59:00','dd-mon-yyyy hh24:mi:ss'), 1 from dual
      union all select 'UA', '566', to_date('10-MAY-08','dd-mon-yy'), 'SMF', 1, 'ORD', 0, 0, '5688', null, to_date('10-MAY-2008 21:28:00','dd-mon-yyyy hh24:mi:ss'), to_date('30-APR-2008 14:59:00','dd-mon-yyyy hh24:mi:ss'), 1 from dual
      union all select 'UA', '110', to_date('11-MAY-08','dd-mon-yy'), 'LAX', 1, 'ORD', 0, 0, '5688', null, to_date('11-MAY-2008 21:07:00','dd-mon-yyyy hh24:mi:ss'), to_date('30-APR-2008 14:59:00','dd-mon-yyyy hh24:mi:ss'), 1 from dual
      union all select 'UA', '122', to_date('12-MAY-08','dd-mon-yy'), 'LAX', 1, 'ORD', 0, 0, '5688', null, to_date('13-MAY-2008 02:52:00','dd-mon-yyyy hh24:mi:ss'), to_date('30-APR-2008 14:59:00','dd-mon-yyyy hh24:mi:ss'), 1 from dual
    --<"Cut Here">
    select CARR_IATA_CD,
           FLT_NBR,
           FLT_LCL_ORIG_DT,
           ORIG_ARPT_CD,
           SCHED_LEG_NBR,
           DEST_ARPT_CD,
           ORIG_OCCUR_NBR,
           DEST_OCCUR_NBR,
           NOSE_NBR,
           TAIL_NBR,
           to_char(GMT_EST_ARR_DTM,'DD-MON-YYYY hh24:mi:ss') GMT_EST_ARR_DTM
      from (select o.*,
                   row_number() over (order by o.gmt_est_arr_dtm desc) rn
              from ops_flt_leg o
              join (select *
                      from ops_flt_leg o
                     where o.flt_nbr = '109'  
                       and o.orig_arpt_cd = 'ORD'
                       and o.dest_arpt_cd = 'ORD'
                       and o.flt_lcl_orig_dt = to_date('30-APR-2008','DD-MON-YYYY')) q1
                on o.nose_nbr  = q1.nose_nbr
               and o.dest_arpt_Cd = q1.orig_arpt_cd
               and o.GMT_EST_ARR_DTM < q1.GMT_EST_DEP_DTM
             where o.active_ind=1)
    where rn = 1
    CARR_IATA_CD FLT_NBR FLT_LCL_ORIG_DT           ORIG_ARPT_CD SCHED_LEG_NBR          DEST_ARPT_CD ORIG_OCCUR_NBR         DEST_OCCUR_NBR         NOSE_NBR TAIL_NBR GMT_EST_ARR_DTM              
    UA           819     30-APR-2008 00:00         EWR          1                      ORD          0                      0                      5688              30-APR-2008 13:36:00         
    1 rows selected

  • How to avoid this in SQL Plus

    Hi All,
    I am creting a Stored Procedure and when i run this sentence the SQL Plus requests my the value of the variable 'lolo' and the i want to compile the Procedure, without giving that value.
    1 create procedure hh
    2 as
    3 begin
    4 DBMS_OUTPUT.PUT_LINE('&lolo');
    5* end;
    SQL> /
    Enter value for jhsdfsd: df
    old 4: DBMS_OUTPUT.PUT_LINE('hshfdhs&jhsdfsd');
    new 4: DBMS_OUTPUT.PUT_LINE('hshfdhsdf');
    Nelson Soler

    Although V's solution (V, nice to see you back by the way), will allow you to compile the proc with the ampersand character, I suspect that the stored proc will not do what you expect.
    If you are expecting it to prompt for a value of lolo when you run it, it won't.
    SQL> SET DEFINE ^
    SQL> CREATE PROCEDURE hh AS
      2  BEGIN
      3     DBMS_OUTPUT.Put_Line('&lolo');
      4  END;
      5  /
    Procedure created.
    SQL> SET SERVEROUTPUT ON
    SQL> exec hh
    &lolo
    PL/SQL procedure successfully completed.You cannot prompt for values in PL/SQL.
    TTFN
    John

  • How to amend this PL/SQL COMMANDS?

    if :TRANSACTION.SHOW_DATE> TRANSACTION.TRANSACTION_DATE
    THEN
         MESSAGE ('MOVIE SHOW_DATE SHOULD BE at most 3 DAYS after TRANSACTION');
         RAISE FORM_TRIGGER_FAILURE;
         else
         message('OK');
         end if;
    (TABLE, VIEW OR SEQUENCE REFERENCES 'TRANSACTION.TRANSACTION_DATE' NOT ALLOWED IN THIS CONTEXT.
    In fact, I want to do the thing quoted in the MESSAGE of the command.

    Prefix transaction.transaction_date with colon.
    if :transaction.show_date > :transaction.transaction_date
    (I am assuming that transaction is a block in Oracle Forms).

  • How to Encrypt Column in SQL Server?

    Hi all,
    I am using ColdFusion MX 7 and Microsoft SQL Server 2000
    database. I have a column that contains social security numbers of
    my users and am feeling the need to encrypt that column.
    Trouble is, I have no clue how to do this in SQL Server or
    how I could display the data from that column in my web app once
    the column is encrypted.
    Can anyone offer any suggestions?
    Thanks in advance!

    Here's one way...
    To encrypt:
    URLEncodedFormat(Encrypt(yourSStoencrypt,
    application.encKey)) then
    store this value in MSSQL
    To decrypt:
    Decrypt(URLDecode(yourSStodecryptfromDB), application.encKey)
    HTH
    Tim Carley
    www.recfusion.com
    [email protected]

  • How to acheive this?

    Hi,
    I have a table X
    Date columnA columnB
    02/26/07 10 1
    02/28/07 1
    03/01/07 5 2
    03/06/07 10 100
    required out put
    Date columnA columnB
    02/26/07 10 1
    02/28/07 10 2
    03/01/07 15 4
    03/06/07 25 104
    Can some one guide me how to achieve this!

    SQL> create table x
      2  as
      3  select date '2007-02-26' mydate, 10 columna, 1 columnb from dual union all
      4  select date '2007-02-28', null, 1 from dual union all
      5  select date '2007-03-01', 5, 2 from dual union all
      6  select date '2007-03-06', 10, 100 from dual
      7  /
    Tabel is aangemaakt.
    SQL> select mydate
      2       , sum(columna) over (order by mydate) columna
      3       , sum(columnb) over (order by mydate) columnb
      4    from x
      5   order by mydate
      6  /
    MYDATE                 COLUMNA    COLUMNB
    26-02-2007 00:00:00         10          1
    28-02-2007 00:00:00         10          2
    01-03-2007 00:00:00         15          4
    06-03-2007 00:00:00         25        104
    4 rijen zijn geselecteerd.Regards,
    Rob.

  • How to write this SQL query

    We have a table, the structure as below: (I put a blank row in the middle to make it clear)
    Product_ID     Retailer     Retail_Price
    1001          A          1.2
    1001          B          1.5
    1001          C          1.4
    1002          B          2.0
    1002          C          2.1
    1002          E          1.8
    1002          F          2.0
    1003          A          1.7
    1003          C          1.5
    Basically we use the table to compare the retail price for different retailers in terms of different products. My customer asked me to give a report based on above table but only shows the retail prices which contain retailer A.
    For example, on above table I need to give a report like below:
    Product_ID     Retailer     Retail_Price
    1001          A          1.2
    1001          B          1.5
    1001          C          1.4
    1003          A          1.7
    1003          C          1.5
    Can anybody give some ideas on how I should write a SQL to achieve this.
    Many thanks!

    SQL> create table product(
    2 Product_ID varchar2(10),
    3 Retailer varchar2(10),
    4 Retail_Price number(7,2)
    5 )
    6 /
    Table created.
    SQL> insert into product values('1001','A',1.2)
    2 /
    1 row created.
    SQL> insert into product values('1001','B',1.5)
    2 /
    1 row created.
    SQL> insert into product values('1001','C',1.4)
    2 /
    1 row created.
    SQL> insert into product values('1002','B',2.0)
    2 /
    1 row created.
    SQL> insert into product values('1002','C',2.1)
    2 /
    1 row created.
    SQL> insert into product values('1002','E',1.8)
    2 /
    1 row created.
    SQL> insert into product values('1002','F',2.0)
    2 /
    1 row created.
    SQL> insert into product values('1003','A',1.7)
    2 /
    1 row created.
    SQL> insert into product values('1003','C',1.5)
    2 /
    1 row created.
    SQL> select * from product
    2 /
    PRODUCT_ID RETAILER RETAIL_PRICE
    1001 A 1.2
    1001 B 1.5
    1001 C 1.4
    1002 B 2
    1002 C 2.1
    1002 E 1.8
    1002 F 2
    1003 A 1.7
    1003 C 1.5
    9 rows selected.
    SQL> select * from product where PRODUCT_ID in (
    2 select distinct PRODUCT_ID from product where RETAILER='A')
    3 /
    PRODUCT_ID RETAILER RETAIL_PRICE
    1001 A 1.2
    1001 B 1.5
    1001 C 1.4
    1003 A 1.7
    1003 C 1.5

Maybe you are looking for

  • How can I delete an iCloud-Account that uses a not anymore existing email-adress?

    Months ago my GMX-Account got blocked because it was hijacked by some SPAM-sending-********. I recognized that when I was not able to log into my iTunes account and buy stuff anymore. I changed the emailadress by setting up a new Account with a new I

  • Should TouchPad be left on all the time? Battery life?

    Hello, sorry if this question has been asked before but I have searched this forum (before asking this question) and could not find an answer. Is it recommended to leave your TouchPad on all the time (which I have been doing) or should it be turned o

  • WLAN Intel AC-7260 doesn't work sometimes

    Hey, sometimes when I start my laptop, the wireless network card isn't properly recognized. Usually it has the name (ip link) wlp3s0. But often it is ony listed as wlan0. Everytime when it is recognized as wlan0, I can't connect to any network, but s

  • Reverding of the service entry sheet

    hi there I have one issue regarding the service entry sheet the user have created the service po & entry sheet in year 2006 now he want to cancel the service entry sheet but when i m trying to cancel the service entry sheet the system is showing me 3

  • The annotation features to be added

    The annotation features are excellent, but a few new features could be added to operate the annotations. The highlight annotation specified in the text body could be temporally invisible so that another annotation could be easily added to the part of