Help needed on writing a SQL query

Here is my table that shows records of 3 ORDER_ID (10, 20 and 30). All I need to do is, pick the first record of each order_id and check the event_id, if the event_id is same for the next record, ignore it, else show it and ignore rest all records for that order_id. This way my query output will show only two records for each ORDER_ID. Query should produce records that are in BOLD in the sample data. (Database - 11g)
Thanks for your help in advance
ORDER_ID     EVENT_ID     EVNT_DATE     STATE_CODE
*10     16937555     20100212     COMPLETE*
10     16937555     20100212     ACTIVE
*10     16308004     20100129     OCCURRED*
10     16131904     20100125     ACTIVE
10     16270684     20100128     OCCURRED
10     14899116     20091213     ACTIVE
10     16085672     20100123     COMPLETE
10     16085673     20100123     OCCURRED
10     14899119     20100123     COMPLETE
10     14899120     20100123     COMPLETE
*20     17134164     20100223     COMPLETE*
20     17134164     20100223     ACTIVE
20     17134164     20100223     STARTED
*20     15479131     20100105     OCCURRED*
20     15478409     20100105     OCCURRED
20     15478408     20100105     ACTIVE
20     15119404     20100105     COMPLETE
20     14346123     20091129     ACTIVE
20     15467821     20100104     OCCURRED
20     14346125     20091216     COMPLETE
20     14346126     20091215     COMPLETE
20     14346126     20091214     COMPLETE
*30     18814670     20100412     COMPLETE*
30     18814670     20100412     ACTIVE
*30     18029509     20100320     OCCURRED*
30     16853720     20100211     ACTIVE
30     17965764     20100319     OCCURRED
30     16386708     20100211     COMPLETE
30     16804451     20100211     OCCURRED
30     15977897     20100121     ACTIVE
Edited by: sarvan on Aug 12, 2011 7:16 AM

try this [Not fully tested]
SQL> select * from v$version;
BANNER
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
PL/SQL Release 11.2.0.2.0 - Production
CORE    11.2.0.2.0      Production
TNS for Linux: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 - Production
Elapsed: 00:00:00.00
SQL> SELECT *
  2    FROM (SELECT order_id, event_id, evnt_date, state_code, next_evntid,
  3                 ROW_NUMBER () OVER (PARTITION BY event_id ORDER BY NULL)
  4                                                                       AS rownm
  5            FROM (WITH t AS
  6                       (SELECT 10 AS order_id, 16937555 AS event_id,
  7                               20100212 AS evnt_date, 'COMPLETE' AS state_code
  8                          FROM DUAL
  9                        UNION ALL
10                        SELECT 10, 16937555, 20100212, 'ACTIVE'
11                          FROM DUAL
12                        UNION ALL
13                        SELECT 10, 16308004, 20100129, 'OCCURRED'
14                          FROM DUAL
15                        UNION ALL
16                        SELECT 10, 16131904, 20100125, 'ACTIVE'
17                          FROM DUAL
18                        UNION ALL
19                        SELECT 10, 16270684, 20100128, 'OCCURRED'
20                          FROM DUAL
21                        UNION ALL
22                        SELECT 20, 17134164, 20100223, 'COMPLETE'
23                          FROM DUAL
24                        UNION ALL
25                        SELECT 20, 17134164, 20100223, 'ACTIVE'
26                          FROM DUAL
27                        UNION ALL
28                        SELECT 20, 17134164, 20100223, 'STARTED'
29                          FROM DUAL
30                        UNION ALL
31                        SELECT 20, 15479131, 20100105, 'OCCURRED'
32                          FROM DUAL)  -- End of test data
33                  SELECT order_id, event_id, evnt_date, state_code,
34                         LEAD (event_id, 1, 0) OVER (PARTITION BY order_id ORDER BY NULL)
35                                                                 AS next_evntid
36                    FROM t)
37           WHERE event_id = next_evntid)
38   WHERE rownm <= 2
39  /
  ORDER_ID   EVENT_ID  EVNT_DATE STATE_CO NEXT_EVNTID      ROWNM
        10   16937555   20100212 COMPLETE    16937555          1
        20   17134164   20100223 COMPLETE    17134164          1
        20   17134164   20100223 ACTIVE      17134164          2
Elapsed: 00:00:00.00
SQL> PS - You should seriously think about ordering the data before you do this kind off operations.
Edited by: Sri on Aug 12, 2011 9:12 AM

Similar Messages

  • Help needed in building a  sql query

    Hello,
    I am using Oracle 10g db.
    I have 3 tables table1 and table2 and table3
    I am writing one sql query which is like
    select table1.a a1,(select distinct b from table2,table3 where table2.id=table3.id and table1.id=table2.id) b1
    from table1
    Now the b1 value may give more then 1 values so when i am trying to execute the query its giving me error.
    What i would like to have is if it gives returns more then 1 value then add that value as a new column means if b1 gives like abc and def as values.
    Then i want the sql to return like
    acolvalue abc def as a single row.
    Is this possible to do?
    Thanks

    Hello,
    The approach which i took is i wrote a function which gives me the b values , sseparated.
    Then i am building a outer query considering the max of b so i just found there are max 10 values which one row is showing.
    select b11,b12,b13,,,b10
    from (
    select table1.a a1,func(select distinct b from table2,table3 where table2.id=table3.id and table1.id=table2.id) b1
    from table1)
    but now i am facing problem like the value of b1 is a,b,c
    i want to use the substr and instr function to get
    a as b11
    b as b12
    c as b13
    can anyone pls help me out to write a query? i am getting b11 but other values are somehow not coming.
    for b11
    i used
    substr(b1,1,instr(b1,',',1,1)-1)
    Thanks

  • Help needed in the a sql query

    hi,
    i have small but tricky piece of requirements,
    following is the input
    Id     id_type     id_level     id_name
    1     F     A_1     Beta1
    2     W     A_1     Beta2
    3     W     A_1     Beta3
    4     L     A_1     Alpha0
    5     W     A_1     Beta4
    6     W     A_1     Beta5
    now whenever
    Id_level = A_1, and id_name = Alpha0 and id_type = L then               
    id_level = B_1               
    and the coming next 2 levels after this should have id_level C_1
    the output should be as follows
    Id     id_type     id_level     id_name
    1     F     A_1     Beta1
    2     W     A_1     Beta2
    3     W     A_1     Beta3
    4     L     B_1     Alpha0
    5     W     C_1     Beta4
    6     W     C_1     Beta5
    kindly let me know if this could be possible in sql,
    if i am missing something here plz highlight it to me,
    kindly give me some tips or hints,thanks and regards
    nic

    SQL> WITH Test_Data AS (
      2    SELECT 1 AS Id, 'F' AS Id_Type, 'A_1' AS Id_Level, 'Beta1' AS Id_Name FROM Dual UNION ALL
      3    SELECT 2,       'W',            'A_1',             'Beta2'            FROM Dual UNION ALL
      4    SELECT 3,       'W',            'A_1',             'Beta3'            FROM Dual UNION ALL
      5    SELECT 6,       'L',            'A_1',             'Alpha0'           FROM Dual UNION ALL
      6    SELECT 8,       'W',            'A_1',             'Beta4'            FROM Dual UNION ALL
      7    SELECT 10,      'W',            'A_1',             'Beta4'            FROM Dual UNION ALL
      8    SELECT 11,      'W',            'A_1',             'Beta5'            FROM Dual
      9  )
    10  SELECT Id, Id_Type,
    11    CASE WHEN Id_Level = 'A_1' AND Id_Name = 'Alpha0' AND Id_Type = 'L'
    12           THEN 'B_1'
    13         WHEN COUNT(CASE WHEN Id_Level = 'A_1' AND Id_Name = 'Alpha0' AND Id_Type = 'L' THEN 1 ELSE NULL END)
    14                OVER(ORDER BY Id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) = 0
    15           THEN Id_Level
    16         WHEN COUNT(CASE WHEN Id_Level = 'A_1' AND Id_Name = 'Alpha0' AND Id_Type = 'L' THEN 1 ELSE NULL END)
    17                OVER(ORDER BY Id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) > 0
    18           THEN 'C_1'
    19     END AS Id_Level,
    20     Id_Name
    21  FROM Test_Data
    22  ORDER BY Id;
            ID I ID_ ID_NAM
             1 F A_1 Beta1
             2 W A_1 Beta2
             3 W A_1 Beta3
             6 L B_1 Alpha0
             8 W C_1 Beta4
            10 W C_1 Beta4
            11 W C_1 Beta5
    7 rows selected.Regards,
    Dima

  • 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.

  • Help required in writing an sql to fetch the the difference of the data.

    Hi Gurus,
    Could some one help me in writing an sql to pull data the difference between two same structured tables from two different database and to store that data in an temperory table.
    many many thanks in advance

    Lets say you have two db SOURCE and DEST
    SOURCE is the DB that you have Logged in into. Now you do the following.
    1. Createa DBLink to DEST database.
    2. Check if the DBLink works properly.
    3. Then execute the following sql.
    select <column_list> from <table_name>
    minus
    select <column_list> from <table_name>@<dblink>
    union all
    select <column_list> from <table_name>@<dblink>
    minus
    select <column_list> from <table_name>

  • Please help me in writing the SQL

    Hi,
    I am new to oracle.. Can you please help me in writing a SQL
    I have a table which has the following columns
    Start_date m1 ---- Start month of each quarter (Jan,Apr,Jul,oct)
    end_date m3---- End month of each quarter
    m1_start_date,
    m1_end_date,
    m2_start_date,
    m2_end_date,
    m3_start_date,
    m3_end_date,
    M1_act_rev,
    m2_act_rev,
    m3_act_rev
    If a user selects the dates from Jan,2011 to Jun, 2011
    I should get the aggregate of the revenues (m1+m2+m3+m1+m2+m3)

    Hi Gurus,
    Will this work
    select
    b.DISTRICT_NAME,
    count(c.CONTRACT_NUMBER),
    sum(C.M1_ACT_REV),
    sum(C.M2_ACT_REV),
    sum(C.M3_ACT_REV),
    sum(C.M1_EXP_PRICE),
    sum(C.M2_EXP_PRICE),
    sum(C.M3_EXP_PRICE)
    from
    clm_mn_compliance_data c,
    CLM_MN_CUSTOMER_ALIGNMENT_DATA b
    where
    ((m1_start_date between '01-01-2011' and '03-31-2011' ) and (m1_end_date between '01-01-2011' and '03-31-2011')) or
    ((m2_start_date between '01-01-2011' and '03-31-2011' ) and (m3_end_date between '01-01-2011' and '03-31-2011')) or
    ((m3_start_date between '01-01-2011' and '03-31-2011' ) and (m3_end_date between '01-01-2011' and '03-31-2011')) and
    b.CUSTOMER_ID = C.CUST_CTRT_ID
    group by
    B.DISTRICT_NAME;

  • Help needed in writing query

    Hi,
    Could anyone help me in writing below query without syntax errors.
    I tried but no luck
    select xmlelement("g", XMLATTRIBUTES(g.contentgroup_id as "id",g.groupname as "label",
    (select xmlagg(xmlelement ("c",XMLATTRIBUTES(c.title as "title",c.content_id as "id")))) as "A"))
    as "A" from
    (SELECT g.contentgroup_id AS id, g.groupname AS label, c.title AS label, c.content_id AS id
    FROM content_ec c FULL OUTER JOIN contentgroup_ec g ON c.group_id = g.contentgroup_id
    oRDER BY g.groupname ,c.title ASC );
    Any help really appreciated.
    Thanks

    Few tips to get your question answered here
    1. Give your database version. Some thing that does not work in one version works fine in the next. And 8i,9i or 10g is not version. best way to give the version is to query your v$version table like this.
    SQL> select * from v$version where rownum = 1
      2  /
    BANNER
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod2. You should understand we don't have what you have, meaning you should provide your data structure and some sample data for us to help.
    3. "I got an error"... "Its not working"... and the list goes on. Sentences like this does not help. If you got a error past the entire error. If stuff does not work, tell us the details.
    4. Just don't throw some output and say i want it. Please explain to every one how you derive it.
    5. And the most important thing. Use \...\ tag to format. Please.. Please.. Please.. use it. It helps every one a lot. When you use it your SQL looks some thing like this.
    SELECT xmlelement("g", XMLATTRIBUTES(g.contentgroup_id as "id",g.groupname as "label",
         (select xmlagg(xmlelement ("c",XMLATTRIBUTES(c.title as "title",c.content_id as "id")))) as "A")) as "A"
       FROM (SELECT g.contentgroup_id AS id,
                 g.groupname AS label,
                 c.title AS label,
                 c.content_id AS id
            FROM content_ec c
            FULL OUTER JOIN contentgroup_ec g
              ON c.group_id = g.contentgroup_id
           ORDER BY g.groupname ,c.title ASC );6. And also do search this forum. Most of your my queries are answered by simple search.
    Thanks,
    Karthick.

  • Help needed in writing SQL CASE or DECODE statement

    Hi experts,
    I need to write a SQL to select order_num, cntry_cde, prod_id and Qty by joining order_num on PROD_ORDER and PROD_ORDER_TXT.
    Here is my sample data
    PRODORDER_               
    order_num     cntry_cde     Prod_id     Qty
    100     US     A1     5
    101     US     A2     10
    102     AU     A3     4
    103     AU     A4     9
    104     IN     A5     3
    PRODORDER_TXT_               
    order_num     cntry_cde     Prod_id     
    100     US     A1     
    101     US     A2     
    102     NZ     A3     
    103     AU     A4     
    104          A5     
    Here is the requirement,
    1) If the cntry_cde in PROD_ORDER is same as cntry_cde in PROD_ORDER_TXT then select PROD_ORDER.cntry_cde (orders 100, 101, 103)
    2) If they are different, pick the country code from PROD_ORDER_TXT (order 102, AU <> NZ)
    3) If they are different and PROD_ORDER_TXT.cntry_cde is NULL, I cannot use it as cntry_cde in my report (order 104). It happenend just because of the bad data at source.
    I cannot avoid it. Then simply use the cntry_cde from PROD_ORDER
    Output expected
    100     US     A1     5
    101     US     A2     10
    102     NZ     A3     4 -- AU changed to NZ
    103     AU     A4     9
    104     IN     A5     3 -- IN retained as PROD_ORDER_TXT.cntry_cde is null
    sample table creation and insert statements are below
    create table prod_order
    (order_num number,
    cntry_cde CHAR(2),
    prod_id VARCHAR2(6),
    qty number)
    create table prod_order_txt
    (order_num number,
    cntry_cde CHAR(2),
    prod_id VARCHAR2(6))
    insert into prod_order values (100, 'US', 'A1',5);
    insert into prod_order values (101, 'US', 'A2',1);
    insert into prod_order values (102, 'AU', 'A3',4);
    insert into prod_order values (103, 'AU', 'A4',9);
    insert into prod_order values (104, 'IN', 'A5',3);
    insert into prod_order_txt values (100,'US','A1');
    insert into prod_order_txt values (101,'US','A2');
    insert into prod_order_txt values (102,'NZ','A3');
    insert into prod_order_txt values (103,'AU','A4');
    insert into prod_order_txt values (104,NULL,'A5');
    commit;
    Thanks for your help in advance
    Edited by: sarvan on Mar 28, 2012 1:39 PM

    Hello
    Thank you for posting all of the ddl and test data along with your expected output - very helpful!. One small point would be to remember to type {noformat}{noformat} before and after any section of code or data in your post so that formatting is retained.  Anyway, this should be a simple join and a combination of CASE and NVL...Select
    po.order_num,
    CASE
    WHEN po.cntry_cde != NVL(pot.cntry_cde,po.cntry_cde)
    THEN
    pot.cntry_cde
    ELSE
    po.cntry_cde
    END cntry_code,
    po.prod_id,
    po.qty
    FROM
    prod_order po
    JOIN
    prod_order_txt pot
    ON
    ( po.order_num = pot.order_num
    ORDER_NUM CN PROD_I QTY
    100 US A1 5
    101 US A2 1
    102 NZ A3 4
    103 AU A4 9
    104 IN A5 3
    5 rows selected.
    HTH
    David
    Edited by: Bravid on Mar 28, 2012 8:32 AM
    corrected !=                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Help needed in writing a function.

    I am using Oracle 11g and SQL plus. I am a complete newbie, so I need some help here in writing a function. I guess my question is more about writing the trigonometric functions within the function.
    latA, longA latB, longB // these are the four input parameters,
    theta = longA - longB
    distX = sin( latA * PI / 180) * sin ( latB * PI /180) + cos ( latA * PI/180) * cos ( latB * PI/180) * cos ( theta * PI / 180)
    distY = acos(distX) // this is arc cosine
    distZ = distY * 180 / PI // PI refers to the mathematical PI
    distP = distZ * 60 * 1.1515; // this value should be returned. Of course the intermediate variable names don't matter.
    Please help. Thanks.

    CREATE OR REPLACE FUNCTION fucntion_name(latA IN NUMBER, longA IN NUMBER, latB IN NUMBER, longB IN NUMBER) RETURN NUMBER
    IS
    pi      CONSTANT NUMBER:=3.14159;
    theta NUMBER;
    distX  NUMBER;
    distY  NUMBER;
    distZ  NUMBER;
    distP  NUMBER;
    BEGIN
    theta :=longA - longB;
    distX :=sin( latA * PI /180) * sin ( latB * PI /180) + cos ( latA * PI/180) * cos ( latB * PI/180) * cos ( theta * PI / 180);
    distY :=acos(distX); --this is arc cosine
    distZ :=distY * 180/PI;  --PI refers to the mathematical PI
    distP :=distZ * 60 * 1.1515; --this value should be returned. Of course the intermediate variable names don't matter.
    RETURN distP;
    END;Edited by: Ora on May 3, 2011 11:46 PM

  • Need to run a sql query in the background and display the output in HTML

    Hi Guys,
    I have a link in my iprocurement webpage, when i click this link, a query (sql query) should be run and the output should be displayed in a HTML page. Any ideas of how this can be done. Help appreciated.
    We dont have OA Framework and we r using 11.5.7.
    help needed
    thanx

    Read Metalink Note 275880.1 which has the link to developer guide and personalization guide.

  • Help Required -- Can we use SQL Query to READ data from SAP MDM Tables

    Hi All,
    Please help.........
    Can we use SQL Query to READ(No Creation/Updation/Deletion  just Read) Data from SAP MDM tables directly, without using MDM Syndicator.
    Or direct SQL access to SAP MDM tables is not possible. Only through MDM Syndicator can we export data.
    Thanks in Advance
    Regards

    All the tables you create in Repository comes under A2i_CM_Tables in Database named as your repository name. So the tables names are fields of table A2i_CM_Tables. Now i tried it but cant make it.
    Now, I dont think its possible to extract all fields in tables and there values using select query. May be pure sql guy can do that or not.
    But there is no relation of data extraction and syndicator. Data is viewed in Data Manager. and you can also store data in a file from DM also.
    BR,
    Alok

  • Problem writing a sql query for a select list based on a static LOV

    Hi,
    I have the following table...
    VALIDATIONS
    ID          Number     (PK)
    APP_ID          Number     
    REQUESTED     Date          
    APPROVED     Date          
    VALID_TIL     Date
    DEPT_ID          Number     (FK)
    I have a search form with the following field item variables...
    P11_DEPT_ID (select list based on dynamic LOV from depts table)
    P11_VALID (select list based on static Yes/No LOV)
    A report on the columns of the Validations table is shown based on the values in the search form. So far, my sql query for the report is...
    SELECT v.APP_ID,
    v.REQUESTED,
    v.APPROVED,
    v.VALID_TIL,
    d.DEPT
    FROM DEPTS d, VALIDATIONS v
    WHERE d.DEPT_ID = v.DEPT_ID(+)
    AND (d.DEPT_ID = :P11_DEPT_ID OR :P11_DEPT_ID = -1)
    This query works so far. My problem is that I don't know how to do a search based on the P11_VALID item - if 'yes' is selected, then the VALID_TIL date is still valid. If 'no' is selected then the VALID_TIL date has passed.
    Can anyone help me to extend my query to include this situation?
    Thanks.

    Hello !
    Let's have a look at my example:create table test
    id        number
    ,valid_til date
    insert into test values( 1, sysdate-3 );
    insert into test values( 2, sysdate-2 );
    insert into test values( 3, sysdate-1 );
    insert into test values( 4, sysdate );
    insert into test values( 5, sysdate+1 );
    insert into test values( 6, sysdate+2 );
    commit;
    select * from test;
    def til=yes
    select *
      from test
      where decode(sign(trunc(valid_til)-trunc(sysdate)),1,1,0,1,-1)
           =decode('&til','yes',1,-1);
    def til=no
    select *                                                                               
      from test                                                                            
      where decode(sign(trunc(valid_til)-trunc(sysdate)),1,1,0,1,-1)
           =decode('&til','yes',1,-1);  
    drop table test;  It's working fine, I've tested it.
    The above changes to my first idea I did because of time portion of the DATE datatype in Oracle and therefore the wrong result for today.
    For understandings:
    1.) TRUNC removes the time part of DATE
    2.) The difference of to date-values is the number of days between.
    3.) SIGN is the mathematical function and gives -1,0 or +1 according to an negative, zero or positiv argument.
    4.) DECODE is like an IF.
    Inspect your LOV for the returning values. According to my example they shoul be 'yes' and 'no'. If your values are different, you may have to modify the DECODE.
    Good luck,
    Heinz

  • Help required in writing an sql querry

    Hi guys,
    Could any one hlep me out in writing an sql querry for the below requirement.
    the requirement is: for a perticular ID if we have different names then we need to get it out as multiple else the name itselef. I have given the sample input and the expected output.
    ID Name
    1 A
    1 B
    2 F
    2 F
    3 C
    4 D
    4 E
    Out put should look like
    ID Name
    1 Multiple
    2 F
    3 C
    4 Multiple
    Thanks in advance
    Shylender.
    Edited by: 959345 on Nov 22, 2012 10:13 PM

    May be this:
    WITH t AS
            (SELECT 1 id, 'A' str FROM DUAL
             UNION ALL
             SELECT 1, 'B' FROM DUAL
             UNION ALL
             SELECT 2, 'F' FROM DUAL
             UNION ALL
             SELECT 2, 'F' FROM DUAL
             UNION ALL
             SELECT 3, 'C' FROM DUAL
             UNION ALL
             SELECT 4, 'D' FROM DUAL
             UNION ALL
             SELECT 4, 'E' FROM DUAL),
         t1 AS
            (SELECT id,
                    CASE
                       WHEN COUNT (DISTINCT str) OVER (PARTITION BY id) > 1 THEN
                          'MULTIPLE'
                       ELSE
                          str
                    END
                       str
               FROM t)
      SELECT id, str
        FROM t1
    GROUP BY id, str
    ORDER BY 1;Cheers,
    Manik.

  • Need suggestions for improving sql query.

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

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

  • Help needed  in writing a Query/Procedure

    Hello All,
    Need all ur help in writing a query or procedure
    Lets say the Table name is DEMO
    There i have one column like num it has values
    1
    2
    3
    4
    5
    8
    9
    10
    my query output should be
    1-5
    8-10
    i,e .. if the diff between two rows is greater than 1 then it result should be in a separate group
    I need a query/Procedure for this. Kindly help
    Regards,
    Chandra

    Tried obtaining the output using SQL and the result is as follows:
    SQL> WITH T AS
      2  (
      3  SELECT 1 COL1 FROM DUAL
      4  UNION
      5  SELECT 2 COL1 FROM DUAL
      6  UNION
      7  SELECT 3 COL1 FROM DUAL
      8  UNION
      9  SELECT 4 COL1 FROM DUAL
    10  UNION
    11  SELECT 5 COL1 FROM DUAL
    12  UNION
    13  SELECT 8 COL1 FROM DUAL
    14  UNION
    15  SELECT 9 COL1 FROM DUAL
    16  UNION
    17  SELECT 10 COL1 FROM DUAL
    18  UNION
    19  SELECT 13 COL1 FROM DUAL
    20  UNION
    21  SELECT 14 COL1 FROM DUAL
    22  UNION
    23  SELECT 15 COL1 FROM DUAL
    24  UNION
    25  SELECT 16 COL1 FROM DUAL
    26  UNION
    27  SELECT 23 COL1 FROM DUAL
    28  UNION
    29  SELECT 24 COL1 FROM DUAL
    30  )
    31  SELECT OUTPUT FROM
    32  (
    33  SELECT DECODE(COL3,NULL,COL1, COL2)  || '-' || LEAD(DECODE(COL3,NULL,COL3, COL1)) OVER (ORDER BY DECODE(COL3,NULL,COL1, COL2)) OUTPUT  FROM
    34  (
    35  SELECT COL1, LEAD(COL1) OVER (ORDER BY COL1) COL2, LAG(COL1) OVER (ORDER BY COL1) COL3 FROM T
    36  )
    37  WHERE
    38  (COL2 - COL1 > 1 OR COL2 IS NULL OR COL3 IS NULL)
    39  )
    40  WHERE OUTPUT != '-';
    OUTPUT                                                                         
    1-5                                                                            
    8-10                                                                           
    13-16                                                                          
    23-24

Maybe you are looking for