Help needed in select query to take date

Hello,
I am using Oracle 11g.
I am using this query
select to_date(to_char(to_date((SOURCE_MONTH||'-'||SOURCE_YEAR),'MM-YYYY'),'Mon-YYYY'),'MON-YYYY') date_
from tab1 a,tab2 b
where b.col2=a.col1;
I have a table with 2 separate varchar columns month and year,
I want to insert the data into another table which has the col type as timestamp.
This query gives me the data as
01-NOV-06 but
i want the data as
01-NOV-2006
What should i do for that?
Thanks

First, get rid of the extra conversions from date to char and back to date:
select to_date((SOURCE_MONTH||'-'||SOURCE_YEAR),'MM-YYYY'),'Mon-YYYY')
from tab1 a,tab2 b
where b.col2=a.col1;Now, your data is safely rendered as a DATE. It knows the full year (i.e.2006). You are just not seeing it formatted the way you like. You could
alter session set nls_date_format = 'mm/dd/yyyy hh24:mi:ss';or give the explicit format you want when you query it (with TO_CHAR).
Please read and understand about DATE datatypes....
http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/sql_elements001.htm#i54330
In particular, search that page for: "DATE Datatype "

Similar Messages

  • Help needed in select query

    Hi,
    I need to select the max time for the transaction using the below query. I’m able to get the o/p. Here the problem is I need to fetch the corrletorid (primary key) as well. if try to get that I’m getting the o/p like case2.
    I want the o/p to be like case 1 with the corrleator id . plz help me to get the o/p.
    Case 1: select b.TransactionName, max(to_char(b.duration, 'mm/dd/yyyy hh24:mi:ss.ff')) as average from trandetails a,subtrandetails b where a.CORRELATORID=b.PCORRELATORID and b.PCORRELATORID='11' group by b.transactionname
    TRANSACTIONNAME MAXTIME
    FINT3 12/10/2007 19:53:09.042000
    FINT 12/10/2007 19:31:07.042000
    FINT2 12/10/2007 19:31:07.042000
    SQL> /
    Case 2: select b.TransactionName,b.correlatorid, max(to_char(b.duration, 'mm/dd/yyyy hh24:mi:ss.ff')) as average from trandetails a,subtrandetails b where a.CORRELATORID=b.PCORRELATORID and b.PCORRELATORID='11' group by b.transactionname,b.correlatorid
    SQL> /
    TRANSACTIONNAME CORRELATORID AVERAGE
    FINT2 102 12/10/2007 19:31:07.042000
    FINT3 108 12/10/2007 19:53:09.042000
    FINT3 103 12/10/2007 19:31:07.042000
    FINT 101 12/10/2007 19:31:07.042000
    in the above output i should have only record(max time) for FINT3.
    Thank

    Sharma,
    Please find the below sample data(insert stmt and create table):
    ======================================
    create table trandetails(
    correlatorid varchar2(20) CONSTRAINT correlatorid_FKey REFERENCES Subtrandetails(correlatorid),
    username varchar2(25) NOT NULL,
    applicationname varchar2(25) NOT NULL,CONSTRAINT composite1_pkey1 PRIMARY KEY(correlatorid ,applicationname));
    create table Subtrandetails(
    correlatorid varchar2(20) PRIMARY KEY,
    PCORRELATORID      varchar2(20),
    TransactionName varchar2(25) NOT NULL,
    Machinename varchar2(15) NOT NULL,
    STARTDATE timestamp NOT NULL,
    ENDDATE timestamp NOT NULL,
    SourceName varchar2(25),
    FunctionName varchar2(25),
    LOC number(5),
    CONTEXTPROPERTY1 varchar2(25),
    CONTEXTPROPERTY2 varchar2(25),
    CONTEXTPROPERTY3 varchar2(25),
    TransactionStatus varchar2(25) NOT NULL CONSTRAINT Transaction1_Status_chk Check (TransactionStatus in ('Success', 'Failure', 'Abort')));
    INSERT INTO trandetails VALUES ('11','FINAPP','ANUAPP1');
    INSERT INTO trandetails VALUES ('13','FINTEST','ANUAPP2');
    INSERT INTO
    Subtrandetails(correlatorid,PCORRELATORID ,TransactionName,Machinename,STARTDATE,ENDDATE ,TransactionStatus )
    VALUES ('10','1','ARM','blrkec95931d','10-DEC-07 03.24.07.042000 PM','10-DEC-07 03.31.07.042000 PM','Success');
    INSERT INTO
    Subtrandetails(correlatorid,PCORRELATORID ,TransactionName,Machinename,STARTDATE,ENDDATE ,TransactionStatus )
    VALUES ('11','1','ARM1','blrkec95931d','10-DEC-07 04.24.07.042000 PM','10-DEC-07 04.31.07.042000 PM','Success');
    INSERT INTO
    Subtrandetails(correlatorid,PCORRELATORID ,TransactionName,Machinename,STARTDATE,ENDDATE ,TransactionStatus )
    VALUES ('12','1','ARM2','blrkec95931d','10-DEC-07 05.24.07.042000 PM','10-DEC-07 05.31.07.042000 PM','Success');
    INSERT INTO
    Subtrandetails(correlatorid,PCORRELATORID ,TransactionName,Machinename,STARTDATE,ENDDATE ,TransactionStatus )
    VALUES ('13','1','ARM3','blrkec95931d','10-DEC-07 06.24.07.042000 PM','10-DEC-07 06.31.07.042000 PM','Success');
    INSERT INTO
    Subtrandetails(correlatorid,PCORRELATORID ,TransactionName,Machinename,STARTDATE,ENDDATE ,TransactionStatus )
    VALUES ('14','1','ARM4','blrkec95931d','10-DEC-07 07.24.07.042000 PM','10-DEC-07 07.31.07.042000 PM','Success');
    INSERT INTO
    Subtrandetails(correlatorid,PCORRELATORID ,TransactionName,Machinename,STARTDATE,ENDDATE ,TransactionStatus )
    VALUES ('101','11','FINT','blrkec95931d','10-DEC-07 07.24.07.042000 PM','10-DEC-07 07.31.07.042000 PM','Success');
    INSERT INTO
    Subtrandetails(correlatorid,PCORRELATORID ,TransactionName,Machinename,STARTDATE,ENDDATE ,TransactionStatus )
    VALUES ('102','11','FINT2','blrkec95931d','10-DEC-07 07.24.07.042000 PM','10-DEC-07 07.31.07.042000 PM','Success');
    INSERT INTO
    Subtrandetails(correlatorid,PCORRELATORID ,TransactionName,Machinename,STARTDATE,ENDDATE ,TransactionStatus )
    VALUES ('103','11','FINT3','blrkec95931d','10-DEC-07 07.24.07.042000 PM','10-DEC-07 07.31.07.042000 PM','Success');
    INSERT INTO
    Subtrandetails(correlatorid,PCORRELATORID ,TransactionName,Machinename,STARTDATE,ENDDATE ,TransactionStatus )
    VALUES ('104','13','SAP1','blrkec95931d','10-DEC-07 07.24.07.042000 PM','10-DEC-07 07.31.07.042000 PM','Success');
    INSERT INTO
    Subtrandetails(correlatorid,PCORRELATORID,TransactionName,Machinename,STARTDATE,ENDDATE ,TransactionStatus )
    VALUES ('105','13','SAP2','blrkec95931d','10-DEC-07 07.24.07.042000 PM','10-DEC-07 07.31.07.042000 PM','Success');
    INSERT INTO
    Subtrandetails(correlatorid,PCORRELATORID ,TransactionName,Machinename,STARTDATE,ENDDATE ,TransactionStatus )
    VALUES ('106','13','SAP3','blrkec95931d','10-DEC-07 07.24.07.042000 PM','10-DEC-07 07.31.07.042000 PM','Success');
    INSERT INTO
    Subtrandetails(correlatorid,PCORRELATORID ,TransactionName,Machinename,STARTDATE,ENDDATE ,TransactionStatus )
    VALUES ('107','13','SAP2','blrkec95931d','10-DEC-07 07.2.10.042350 PM','10-DEC-07 07.50.10.042050 PM','Success');
    INSERT INTO
    Subtrandetails(correlatorid,PCORRELATORID ,TransactionName,Machinename,STARTDATE,ENDDATE ,TransactionStatus )
    VALUES ('108','11',' FINT3 ','blrkec95931d','10-DEC-07 07.16.07.042000 PM','10-DEC-07 07.53.09.042000 PM','Success');
    INSERT INTO
    Subtrandetails(correlatorid,PCORRELATORID ,TransactionName,Machinename,STARTDATE,ENDDATE ,TransactionStatus )
    VALUES ('109','108','FINT3','blrkec95931d','10-DEC-07 07.20.07.042000 PM','10-DEC-07 07.59.09.042000 PM','Success');
    INSERT INTO
    Subtrandetails(correlatorid,PCORRELATORID ,TransactionName,Machinename,STARTDATE,ENDDATE ,TransactionStatus )
    VALUES ('110','108','FINT3','blrkec95931d','10-DEC-07 07.18.07.042000 PM','10-DEC-07 07.57.09.042000 PM','Success');
    INSERT INTO
    Subtrandetails(correlatorid,PCORRELATORID ,TransactionName,Machinename,STARTDATE,ENDDATE ,TransactionStatus )
    VALUES ('111','108','FINT4','blrkec95931d','10-DEC-07 07.18.07.042000 PM','10-DEC-07 07.57.09.042000 PM','Success');
    INSERT INTO
    Subtrandetails(correlatorid,PCORRELATORID ,TransactionName,Machinename,STARTDATE,ENDDATE ,TransactionStatus )
    VALUES ('112','108','FINT5','blrkec95931d','10-DEC-07 07.18.07.042000 PM','10-DEC-07 07.57.09.042000 PM','Success');
    INSERT INTO
    Subtrandetails(correlatorid,PCORRELATORID ,TransactionName,Machinename,STARTDATE,ENDDATE ,TransactionStatus )
    VALUES ('113','108','FINT6','blrkec95931d','10-DEC-07 07.18.07.042000 PM','10-DEC-07 07.57.09.042000 PM','Success');
    INSERT INTO
    Subtrandetails(correlatorid,PCORRELATORID ,TransactionName,Machinename,STARTDATE,ENDDATE ,TransactionStatus )
    VALUES ('114','108','FINT7','blrkec95931d','10-DEC-07 07.18.07.042000 PM','10-DEC-07 07.57.09.042000 PM','Success');
    ======================================
    Thanks.

  • Help need in selection query

    Hi ,
    I have a field(test) in ztable(zxxx) whose length is 12 char and while updating it
    only 10 digits were updated and there are no zeros in front of the value in order to make it
    12 digit.
    Now i need this parameter to be included in another program selection screen.
    Now i have given it as
    parameter :  test(12) type n.
    it works fine when it has an entry , when it is blank i has to retrieve all records but in this case
    i won't because it has a value of 000000000000
    noe when i declare like this
    paramter : test like zxxx-test.
    when my input is 123456, it won't fetch me any value because it has to be 0000123456 because it has been updated like in the table.
    There was a purpose in updating this way.
    Now my code is like this
    parameters : test(10) type n.
    select-options :   kostl for zxxx-kostl NO-EXTENSION NO INTERVALS,
          p_uname for zxxx-uname NO-EXTENSION NO INTERVALS.
    if test is initial.
    select * from zxxx into table itab where kostl in kostl and uname in p_uname.
    else.
    select * from zxxx into table itab where aufnr  = order and
                                   kostl in kostl and uname in p_uname.
    endif
    PLEASE SUGGEST ME IF THERE IS AN ALTERNATE BETTER WAY.
    or this is the best way
    Thanks

    Hi
    as per my knowledge what you have done is right.
    Thanks
    KAJOL

  • Select Query Between two dates...

    Hi Guru's,
    I need a Select Query between two dates, also if the record not found for any in between date then it should return NULL or 0 ...
    for Example
    1. I am having two records in DB for date 2-10-2008 & 4-10-2008
    2. Now suppose I have given Query for date between 1-10-2008 to 5-10-2008
    Then it should return me 5 records with valid values for 2 & 4 and NULL for other 1,3,5
    Thanks.

    Try like this:
    with
      t as
          select date '2008-10-02' as dt, 'Record #1 (in DB)' as str from dual union all
          select date '2008-10-04' as dt, 'Record #2 (in DB)' as str from dual
    select v.dt, t.str
      from (
             select date '2008-10-01' + level - 1 as dt
               from dual
             connect by level <= (date '2008-10-05' - date '2008-10-01') + 1
           ) v
      left join t
        on v.dt = t.dt
    order by 1

  • Need some help with the Select query.

    Need some help with the Select query.
    I had created a Z table with the following fields :
    ZADS :
    MANDT
    VKORG
    ABGRU.
    I had written a select query as below :
    select single vkorg abgru from ZADS into it_rej.
    IT_REJ is a Work area:
    DATA : BEGIN OF IT_REJ,
            VKORG TYPE VBAK-VKORG,
            ABGRU TYPE VBAP-ABGRU,
           END OF IT_REJ.
    This is causing performance issue. They are asking me to include the where condition for this select query.
    What should be my select query here?
    Please suggest....
    Any suggestion will be apprecaiated!
    Regards,
    Developer

    Hello Everybody!
    Thank you for all your response!
    I had changes this work area into Internal table and changed the select query. PLease let me know if this causes any performance issues?
    I had created a Z table with the following fields :
    ZADS :
    MANDT
    VKORG
    ABGRU.
    I had written a select query as below :
    I had removed the select single and insted of using the Structure it_rej, I had changed it into Internal table 
    select vkorg abgru from ZADS into it_rej.
    Earlier :
    IT_REJ is a Work area:
    DATA : BEGIN OF IT_REJ,
    VKORG TYPE VBAK-VKORG,
    ABGRU TYPE VBAP-ABGRU,
    END OF IT_REJ.
    Now :
    DATA : BEGIN OF IT_REJ occurs 0,
    VKORG TYPE VBAK-VKORG,
    ABGRU TYPE VBAP-ABGRU,
    END OF IT_REJ.
    I guess this will fix the issue correct?
    PLease suggest!
    Regards,
    Developer.

  • Help needed for writing query

    help needed for writing query
    i have the following tables(with data) as mentioned below
    FK*-foregin key (SUBJECTS)
    FK**-foregin key (COMBINATION)
    1)SUBJECTS(table name)     
    SUB_ID(NUMBER) SUB_CODE(VARCHAR2) SUB_NAME (VARCHAR2)
    2           02           Computer Science
    3           03           Physics
    4           04           Chemistry
    5           05           Mathematics
    7           07           Commerce
    8           08           Computer Applications
    9           09           Biology
    2)COMBINATION
    COMB_ID(NUMBER) COMB_NAME(VARCHAR2) SUB_ID1(NUMBER(FK*)) SUB_ID2(NUMBER(FK*)) SUB_ID3(NUMBER(FK*)) SUBJ_ID4(NUMBER(FK*))
    383           S1      9           4           2           3
    384           S2      4           2           5           3
    ---------I actually designed the ABOVE table also like this
    3) a)COMBINATION
    COMB_ID(NUMBER) COMB_NAME(VARCHAR2)
    383           S1
    384           S2
    b)COMBINATION_DET
    COMBDET_ID(NUMBER) COMB_ID(FK**) SUB_ID(FK*)
    1               383          9
    2               383          4
    3               383          2
    4               383          3
    5               384          4
    6               384          2          
    7               384          5
    8               384          3
    Business rule: a combination consists of a maximum of 4 subjects (must contain)
    and the user is less relevant to a COMB_NAME(name of combinations) but user need
    the subjects contained in combinations
    i need the following output
    COMB_ID COMB_NAME SUBJECT1 SUBJECT2      SUBJECT3      SUBJECT4
    383     S1     Biology Chemistry      Computer Science Physics
    384     S2     Chemistry Computer Science Mathematics Physics
    or even this is enough(what i actually needed)
    COMB_ID     subjects
    383           Biology,Chemistry,Computer Science,Physics
    384           Chemistry,Computer Science,Mathematics,Physics
    you can use any of the COMBINATION table(either (2) or (3))
    and i want to know
    1)which design is good in this case
    (i think SUB_ID1,SUB_ID2,SUB_ID3,SUB_ID4 is not a
    good method to link with same table but if 4 subjects only(and must) comes
    detail table is not neccessary )
    now i am achieving the result by program-coding in C# after getting the rows from oracle
    i am using oracle 9i (also ODP.NET)
    i want to know how can i get the result in the stored procedure itsef.
    2)how it could be designed in any other way.
    any help/suggestion is welcome
    thanks for your time --Pradeesh

    Well I forgot the table-alias, here now with:
    SELECT C.COMB_ID
    , C.COMB_NAME
    , (SELECT SUB_NAME
    FROM SUBJECTS
    WHERE SUB_ID = C.SUB_ID1) AS SUBJECT_NAME1
    , (SELECT SUB_NAME
    FROM SUBJECTS
    WHERE SUB_ID = C.SUB_ID2) AS SUBJECT_NAME2
    , (SELECT SUB_NAME
    FROM SUBJECTS
    WHERE SUB_ID = C.SUB_ID3) AS SUBJECT_NAME3
    , (SELECT SUB_NAME
    FROM SUBJECTS
    WHERE SUB_ID = C.SUB_ID4) AS SUBJECT_NAME4
    FROM COMBINATION C;
    As you need exactly 4 subjects, the columns-solution is just fine I would say.

  • Select Query Based on date condition

    Hi ,
    Is it Possible.
    i want to run select query based on date condition.
    Eg...
    if the date between 01-jan-01 and 01-jan-05 then
    select * from table1;
    if the date between 02-jan-05 and 01-jan-08 then
    select * from table2;
    Becaz i have data in 2 diffrent tables , based on the date condition i wnt to run the select statement to diffrent tables.
    i dont want plsql here Just SQL needed.
    thanks,
    -R
    Edited by: infant_raj on May 5, 2009 11:48 PM

    Helo Kanish,
    this is not the one i was asking..
    wht i mean was .
    i use bind variable to get date while running the select statement , once i get the date then i want to choose any one of the table to run select query.
    EG..
    select col1,col2 from table1 where date between only if 01-jan-01 and 01-jan-05;
    select col1,col2 from table2 where date between only if 02-jan-05 and 01-jan-08;
    Run any one of the two . not all
    thanks,
    _raj                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • I need a select query

    I have a dataset like below in two columns ( col1,col2 in table Mytable)
    A | ab
    A | cd
    A | ef
    B | xl
    B | ik
    E | 23
    E | op
    C | tk
    I need a select query which will return first set of every group which I ask for .
    For example, if I ask for data for A , B , the select query will return records
    A | ab
    B | xl
    For example, if I ask for data for B ,E the select query will return records..
    B | xl
    E | 23
    For example, if I ask for data for C the select query will return records..
    C | tk
    How do I write my select query ?

    Something like this ?
    SCOTT@orcl> create table test(col1 varchar2(1),col2 varchar2(2));
    Table created.
    SCOTT@orcl> insert into test values('A','ab');
    1 row created.
    SCOTT@orcl> insert into test values('A','cd');
    1 row created.
    SCOTT@orcl> insert into test values('A','ef');
    1 row created.
    SCOTT@orcl> insert into test values('B','xl');
    1 row created.
    SCOTT@orcl> insert into test values('B','ik');
    1 row created.
    SCOTT@orcl> insert into test values('E','23');
    1 row created.
    SCOTT@orcl> insert into test values('E','op');
    1 row created.
    SCOTT@orcl> insert into test values('C','tk');
    1 row created.
    SCOTT@orcl> commit;
    Commit complete.
    SCOTT@orcl>
    SCOTT@orcl> ed
    Wrote file afiedt.buf
      1  select * from (
      2  select decode(lag(col1) over (order by col1,rownum),null,col1
      3  ,col1,null
      4  ,col1) col1
      5  ,col2
      6  from test) aaa
      7* where aaa.col1='A'
    SCOTT@orcl> /
    C CO
    A abHTH
    Girish Sharma

  • In Icloud I can't see my "numbers" icon which i need to select "tick" to transfer data to my other devices via icloud. any ideas why it's not showing up?

    In Icloud on my imac I can't see my "numbers" icon which i need to select "tick" to transfer data to my other devices via icloud. any ideas why it's not showing up?

    You have to do it app wise, which means that the icon won't show up in settings, but you will be able to move files from your local drive to iCloud via the Numbers app (move... > choose the iCloud option).
    It requires that you set up the same iCloud account on all your devices and that you have the latest version of Numbers on all your devices as well.

  • I need help with a SELECT query - help!

    Hello, I need help with a select statement.
    I have a table with 2 fields as shown below
    Name | Type
    John | 1
    John | 2
    John | 3
    Paul | 1
    Paul | 2
    Paul | 3
    Mark | 1
    Mark | 2
    I need a query that returns everything where the name has type 1 or 2 but not type 3. So in the example above the qery should bring back all the "Mark" records.
    Thanks,
    Ian

    Or, if the types are sequential from 1 upwards you could simply do:-
    SQL> create table t as
      2  select 'John' as name, 1 as type from dual union
      3  select 'John',2 from dual union
      4  select 'John',3 from dual union
      5  select 'Paul',1 from dual union
      6  select 'Paul',2 from dual union
      7  select 'Paul',3 from dual union
      8  select 'Paul',4 from dual union
      9  select 'Mark',1 from dual union
    10  select 'Mark',2 from dual;
    Table created.
    SQL> select name
      2  from t
      3  group by name
      4  having count(*) <= 2;
    NAME
    Mark
    SQL>Or another alternative if they aren't sequential:
    SQL> ed
    Wrote file afiedt.buf
      1  select name from (
      2    select name, max(type) t
      3    from t
      4    group by name
      5    )
      6* where t < 3
    SQL> /
    NAME
    Mark
    SQL>Message was edited by:
    blushadow

  • Help in Join Select Query

    Hi, Please help on the Select Join query. I want to compare code_stat_system1/code_stat_system2 and code_date_system1 /code_date_system2 column from table #system1 and #system2 but there is 2 rule to get correct row from table #system1 and #system2.
    Rule 1: We need to get max(code_date_system1) record from #system1  for every code_system1 for compare.
    Rule 1: We need to get max(code_date_system2) record from #system2  BUT BEFORE THE RECORD OF 'DKNOW' OF code_stat_system2 for every code_system2 for compare.
    drop table #system1,#system2
    create table #system1
    (Code_system1 varchar(10),code_stat_system1 varchar(10),code_date_system1 datetime)
    Insert into #system1 values ('10','Dead','2013-01-01')
    Insert into #system1 values ('10','Dead','2013-12-30')
    Insert into #system1 values ('10','UnOpen','2014-10-01')
    Insert into #system1 values ('10','Open','2014-11-01')   --This record should max(code_stat_system1) for compare
    Insert into #system1 values ('20','Dead','2013-12-01')
    Insert into #system1 values ('20','Dead','2013-12-30')
    Insert into #system1 values ('20','Open','2014-08-01')
    Insert into #system1 values ('20','UnOpen','2014-09-01') --This record should max(code_stat_system1) for compare
    create table #system2
    (Code_system2 varchar(10),code_stat_system2 varchar(10),code_date_system2 datetime)
    Insert into #system2 values ('10','Dead','2013-01-01')
    Insert into #system2 values ('10','Dead','2013-12-30')
    Insert into #system2 values ('10','Open','2014-10-01')
    Insert into #system2 values ('10','UnOpen','2014-12-01') --This record should max(code_stat_system2) which is before 'DKNOW' for compare
    Insert into #system2 values ('10','DKNOW','2015-01-01')
    Insert into #system2 values ('10','DKNOW','2015-02-01')
    Insert into #system2 values ('20','Dead','2013-12-01')
    Insert into #system2 values ('20','Dead','2013-12-30')
    Insert into #system2 values ('20','Open','2014-08-01')
    Insert into #system2 values ('20','UnOpen','2014-09-01') --This record should max(code_stat_system2) which is before 'DKNOW' for compare
    Insert into #system2 values ('20','DKNOW','2015-01-01')
    Insert into #system2 values ('20','DKNOW','2015-02-01')
    --Desired Output
    Code_system1  code_stat_system1  code_date_system1  Code_system1   code_stat_system2  code_date_system1    Rrmk_code_stat         Rrmk_code_date
    10            Open           2014-11-01        10          UnOpen         
    2014-12-01         Codes not matching     Dates not matching
    20            UnOpen         2014-09-01        20          UnOpen         
    2014-09-01         Codes matching          Dates matching
    Thanks.

    drop table #system1,#system2
    create table #system1
    (Code_system1 varchar(10),code_stat_system1 varchar(10),code_date_system1 datetime)
    Insert into #system1 values ('10','Dead','2013-01-01')
    Insert into #system1 values ('10','Dead','2013-12-30')
    Insert into #system1 values ('10','UnOpen','2014-10-01')
    Insert into #system1 values ('10','Open','2014-11-01') --This record should max(code_stat_system1) for compare
    Insert into #system1 values ('20','Dead','2013-12-01')
    Insert into #system1 values ('20','Dead','2013-12-30')
    Insert into #system1 values ('20','Open','2014-08-01')
    Insert into #system1 values ('20','UnOpen','2014-09-01') --This record should max(code_stat_system1) for compare
    create table #system2
    (Code_system2 varchar(10),code_stat_system2 varchar(10),code_date_system2 datetime)
    Insert into #system2 values ('10','Dead','2013-01-01')
    Insert into #system2 values ('10','Dead','2013-12-30')
    Insert into #system2 values ('10','Open','2014-10-01')
    Insert into #system2 values ('10','UnOpen','2014-12-01') --This record should max(code_stat_system2) which is before 'DKNOW' for compare
    Insert into #system2 values ('10','DKNOW','2015-01-01')
    Insert into #system2 values ('10','DKNOW','2015-02-01')
    Insert into #system2 values ('20','Dead','2013-12-01')
    Insert into #system2 values ('20','Dead','2013-12-30')
    Insert into #system2 values ('20','Open','2014-08-01')
    Insert into #system2 values ('20','UnOpen','2014-09-01') --This record should max(code_stat_system2) which is before 'DKNOW' for compare
    Insert into #system2 values ('20','DKNOW','2015-01-01')
    Insert into #system2 values ('20','DKNOW','2015-02-01')
    ;with mycte1 as (select * ,row_number() Over (partition by Code_system1 Order by code_date_system1 Desc ) rn from #system1)
    ,mycte2 as (select *,row_number() Over (partition by Code_system2 Order by code_date_system2 Desc ) rn2 from #system2
    where code_stat_system2<>'DKNOW')
    Select m1.Code_system1, m1.code_stat_system1, m1.code_date_system1, Code_system2, code_stat_system2,code_date_system2 ,
    case When code_date_system1=code_date_system2 then 'Dates matching' else 'Dates not matching' End as checkDate,
    case When code_stat_system1=code_stat_system2 then 'Codes matching' else 'Codes not matching' End as checkStat
    from mycte1 m1 inner join mycte2 m2 on m1.Code_system1= m2.Code_system2 and m1.rn=m2.rn2
    Where m1.rn=1

  • Help regarding a select Query

    hi All,
    I have one requirement.I wonder whether i can do it in a single SQL Statement or not.
    I have tab1 with columns c1,c2,c3,c4,....,Logon_time
    I have tab2 with columns C1,c2,c3,c4.....,Logoff_time
    All the columns are same for two tables except that logon_time and logoff_time as mentioned above.
    tab1 will have all the information of user who logon to database.
    tab2 will have all the information of user who logoff from DB.
    Now i i have to generate a report which shows all the user info and logon Duration.For that i can use logoff_time - logon_time.but the challenge here is logoff_time dont contain rows for the users who logon to database byt yet to logoff.
    So for them logoff_time-logon_time showing -ve Duration.
    So I want Duration as logoff_time-logon_time for all the rows.But for those users who yet to logoff i want duration as "YET to Logoff"
    Is this possible with single Select Query.
    Thanks
    Pramod

    Hi, Pramod,
    You need to join tab_1 to tab_2. Since not every row in tab_1 will have a matching row in tab_2, make it an outer join.
    How do you know which row in tab_1 matches which row in tab_2?
    I assume there's some column (let's say c1) that identifies a user. A user can have several sessions going at the same time, however.
    The following query matches the n-th logon_time in tab_1 with the n-th logoff_time in tab_2 for the same user:
    WITH     numbered_tab_1     AS
         SELECT  c1, c2, c3, c4, ..., logon_time
         ,     ROW_NUMBER () OVER ( PARTITION BY  c1     -- or whatever identifies user
                                   ORDER BY          logon_time
                           ) AS r_num
         FROM     tab_1
    ,     numbered_tab_2     AS
         SELECT  c1, c2, c3, c4, ..., logon_time
         ,     ROW_NUMBER () OVER ( PARTITION BY  c1     -- or whatever identifies user
                                   ORDER BY          logoff_time
                           ) AS r_num
         FROM     tab_2
    SELECT     t1.c1, t1.c2, t1.c3, t1.c4, ..., t1.logon_time     -- or whatever you want
    ,     t2.c1, t2.c2, t2.c3, t2.c4, ..., t2.logoff_time
    ,     NVL ( TO_CHAR (24 * (t2.logff_time - t1.logon_time))    -- duration in hours
             , 'Yet to Logoff'
             )         AS duration
    FROM             numbered_tab_1     t1
    LEFT OUTER JOIN     numbered_tab_2     t2     ON   t1.c1     = t2.c1
                                     AND  t1.r_num  = t2.r_num
    ;I can test this if you'll post some sample data and the results you want from that data.
    The query above matches logons and logoffs only by user and time
    For example, say I log on at 10:00.
    At 10:45, I open another window and log on again, but log off at 10:50 after 5 minutes.
    At 11:30 I log off the first session.
    If you were interested in sessions that lasted over 1 hour, the query above would not be accurate. It would show one session starting at 10:00 and ending at 10:50, and another session starting at 10:45 and ending at 11:30. To know that the first session to end was the second sessiion in order of beginning, you would have to have another column, say a unique session id.
    If you're only interested in the number of sessions, or the total time connected per user, then the query above will be good enough.
    Edited by: Frank Kulash on Jul 6, 2009 12:07 PM

  • Help need in a query

    Could you please get me with a select query for getting my expected result.
    I tried to use group function in my query, but it giving all the user_id if i group, so please help me on this.
    user_id trann_name resp_time
    1 dog 0.5
    2 dog 1.0
    3 dog 0.6
    4 dd 0.4
    5 cat 0.3
    expected result:
    1 dog 0.5
    4 dd 0.4
    5 cat 0.3
    Thanks.

    Blu, that is obviously wrong ;-)
    OP wants (The ones with the fastest response time):
    SQL> with test_data as (select 1 user_id, 'dog' trann_name, 0.5 resp_time from dual union all
                       select 2 user_id, 'dog' trann_name, 1.0 resp_time from dual union all
                       select 3 user_id, 'dog' trann_name, 0.6 resp_time from dual union all
                       select 4 user_id, 'dd'  trann_name, 0.4 resp_time from dual union all
                       select 5 user_id, 'cat' trann_name, 0.3 resp_time from dual)
    -- End of test data, below is actual query
    select max(user_id) keep (dense_rank first order by resp_time) user_id,
           trann_name,
           min(resp_time) resp_time
      from test_data
    group by trann_name
    order by user_id
       USER_ID TRANN_NAME  RESP_TIME
             1 dog                ,5
             4 dd                 ,4
             5 cat                ,3
    3 rows selected.Best regards
    Peter

  • Select query to read data from a view

    Hi friends,
    We have been using a query to read data from a custom view which used to work perfectly. Now the program sits at that select query forever. We are able to extract same data from se16. Not sure what could be the problem.
    Thanks in advance.

    Dev
    Have a look at the Table Index for the tables involved in the View... I think there is some change in the Indexes.. (Add / Remove / Change)
    Thanks
    Amol Lohade

  • Select query with blank date

    Hi All,
    I have issue with my select query, this query should execute when the date is empty in the table. Fields for the date is (trrecv and trpay)
    code:
    SELECT * FROM zcat3_mov
                       INTO CORRESPONDING FIELDS OF
                       TABLE lt_invmov
                       WHERE bukrs  EQ zcat3_mov-bukrs
                       AND trans EQ zcat3_mov-trans
                       AND status EQ 'CD'
                       AND clear  EQ ' '
                       AND trrecv EQ ' '
                       AND trpay  EQ ' '.
    Thanks,
    FED

    Hi,
    to check for blank date fields in database tables always use :
    date1 = '00000000'
    so your query will be -
    SELECT * FROM zcat3_mov INTO CORRESPONDING FIELDS OF TABLE lt_invmov
                            WHERE bukrs  EQ zcat3_mov-bukrs  AND
                                  trans  EQ zcat3_mov-trans  AND
                                  status EQ 'CD'             AND
                                  clear  EQ ' '              AND
                                  trrecv EQ '00000000'       AND
                                  trpay  EQ '00000000 '.
    regards,
    Shaurabh Pandey

Maybe you are looking for

  • Lion shut down

    I have been following some of the discussions re Lion not shutting down, and have the same problem for the past three months or so (never had a problem with it before - it just suddenly started with the grey screen and the icon turning and nothing fu

  • PL/SQL: numeric or value error (character string buffer too small)

    Hi All, Below is the procedure used to send notifications. It was working fine in previous DB version :- 10.1.0.5.0 and there was recent upgrade to 11.2.0.2.0. I complies fine, but when executing it throws the error. PROCEDURE PROC_STATUS_MAIL_UNADJ

  • Passing username and password while calling the simple HTTP API

    Hi, I want to execute a graph by calling the http api from a java code. However, this gives me a 401 unauthorized http error as I am not passing the username and password to the clover http url. The following is the format of the constructed url. - h

  • Latest Windows 7 on latest Mac OS X via Boot Camp = display driver problems

    I've just done (with difficulty) a Windows 7 installation via Boot Camp on my hot new Mac Quad. But when I use Photoshop Elements for Windows-- the problem may occur with other Windows applications too but I haven't tried them yet-- I'm getting frequ

  • Problem with Outlook (client) 2010/2013 profiles on Exchange 2013

    We have recently started migrating to Exchange 2013 and have come across a problem with some of our accounts. This problem exists in both migrated mailboxes and natively created 2013 mailboxes. basically we have a lot of "shared" accounts.  We create