Help in writing query

There are 2 tables
EMP Table
empno salary grade
100 1000
200 2000
300 2200
400 3500
500 3700
600 5200
700 4000
800 6500
Grade table
grade losal hisal
A 0 1000
B 1001 2000
C 2500 3500
D 4000 5000
E 5500 6500
Now query's output should like
100 1000 A
200 2000 B
300 2200 B
400 3500 C
500 3700 C
600 5200 D
700 4000 D
800 6500 E
If the salary is not in given range then preceding grade should be displayed.
example is empno with 300
After retrieving the query results, Update emp table with grades
Thanks in advance
Vinay

Hi,
this should work
SQL> update EMP a
SQL> set a.grade = (select grade from grade b where a.salary between b.losal and b.hisal);
SQL> select * from EMP;
EMPNO SALARY GRADE
100 1000 A
200 2000 B
300 2200
400 3500 C
500 3700
600 5200
700 4000 D
800 6500 E
SQL> update EMP a
SQL> set a.grade = (select max(grade) from grade b where a.salary > b.hisal )
SQL> where a.grade is null;
SQL> select * from EMP;
EMPNO SALARY GRADE
100 1000 A
200 2000 B
300 2200 B
400 3500 C
500 3700 C
600 5200 D
700 4000 D
800 6500 E
null

Similar Messages

  • Need helping in writing query for finding percentage of duration

    Can any one please help in writing query for this.
    The table is like this :-
    ID     Region     Month     Duration
    I1 R1     Jan     80
    I2     R2     Jan     70
    I3     R1     Jan     70
    I4     R3     Jan     40
    I5     R1     Feb     80
    I6     R2     Feb     30
    I7     R3     Mar     100
    I want to write a query to find
    % of duration for each and every region against each and every month.
    Please help in solving this query. I am in urgent need of this.
    Thanks in advance.

    I also have to do in MS Access 2003You also have to ask into an other forum since here it's an Oracle forum, to try to find Oracle solution.
    Nicolas.

  • Help in writing  query few schema objects  from another schema.

    Hi Gurus,
    Could some one help in writing an sql which will give list of all the object of a schema "genp" visible in another schema "genp_v" and "gen_con" there is an dblink and few grants on those schemas .
    i dont have the password of any of those users i have connected as sys and i can not change the password of any of those users..
    Any help on this is highly apprciated.
    Thanks in advance .

    could you please update me for all the objects apart from just the tables .
    thank you so much .. it was just out of my head at that moment .
    cheers

  • Help Needed Writing Query

    Hi Users,
    I have a table which has the records inserted on every Saturday (i:e on a weekly basis).
    The table "NCIM" has the following records in it
    NODE              INTERFACE      CREATEDATE
    172.29.58.195     4     1/1/2011
    172.29.60.1     11     1/1/2011
    172.16.10.44     3     1/1/2011
    172.29.58.195     4     25/12/2010
    172.29.60.1     11     25/12/2010
    172.16.10.44     3     25/12/2010
    172.29.58.195     2     18/12/2010
    172.29.60.1     8     18/12/2010
    172.16.10.44     5     18/12/2010
    172.29.58.195     6     11/12/2010
    172.29.60.1     22     11/12/2010
    172.16.10.44     7     11/12/2010The requirement is to find, what was the "INTERFACE" value for the particular NODE on the particular window period.
    1) When I run the query any time in the current week (between Jan 1st and Jan 7th 2011) then
    my output should be
    NODE             INTERFACE      CREATEDATE
    172.29.58.195     4     1/1/2011
    172.29.60.1     11     1/1/2011
    172.16.10.44     3     1/1/20112) When I run the query with the condition for the last two week from the current date (subtracting 14 days from current date 4th Jan is Dec 22nd 2010) falling between Dec 18th and Dec 25th. So the output should be
    NODE             INTERFACE       CREATEDATE
    172.29.58.195     2     18/12/2010
    172.29.60.1     8     18/12/2010
    172.16.10.44     5     18/12/2010Can some please help how to accomplish this with the query.
    Thanks.
    RR

    Hi,
    you can use the solution from frank or if you have real dates for the select, you can do it with a simple date_field <to_date('25.12.2010','DD.MM.YYYY') and date_field>=to_date('18.12.2010','DD.MM.YYYY')
    Regards
    Udo
    SQL> create table a_table (interface number,date_field date);
    Tabelle wurde angelegt.
    SQL> insert into a_table (interface,date_field) values(1,to_date('11.12.2010','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(2,to_date('11.12.2010','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(3,to_date('11.12.2010','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(4,to_date('18.12.2010','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(5,to_date('18.12.2010','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(6,to_date('18.12.2010','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(7,to_date('25.12.2010','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(8,to_date('25.12.2010','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(9,to_date('25.12.2010','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(10,to_date('01.01.2011','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(11,to_date('01.01.2011','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(12,to_date('01.01.2011','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(13,to_date('08.01.2011','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(14,to_date('08.01.2011','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(15,to_date('08.01.2011','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL>
    SQL> select * from a_table where date_field <=sysdate and date_field>=sysdate-7;
    INTERFACE DATE_FIE
    10 01.01.11
    11 01.01.11
    12 01.01.11
    SQL> select * from a_table where date_field <to_date('25.12.2010','DD.MM.YYYY') and date_field>=to_date('18.12.2010','DD.MM.YYYY');
    INTERFACE DATE_FIE
    4 18.12.10
    5 18.12.10
    6 18.12.10

  • Need help in writing query

    EMP_ID     EMP_NAME
    1     PAULA
    2     JOHN
    3     RADHA
    4     RADHA
    5     RAM
    6     RAM
    7     RAM
    8     PATRICK
    9     PADMA
    10     SALMAN
    Write a query that gives all names that occur more than three times in the company.

    Hi,
    Also, look up the aggregate COUNT function.
    COUNT, GROUP BY and HAVING are all described in the [SQL Language Reference manual|http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/index.htm], though, if you have a textbook, you should look at the textbook first.

  • 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 in writing a query!

    Dear All,
    My database is 11gR2 on Linux.
    I am struck in writing a difficult query, need help from your guys.
    I have a table, an application is controlling the columns of this table. Columns are added by the application.
    This is the structure of the table:
    create table imran_test(
    2 username varchar2(100),
    3 layer1 number(1),
    4 layer2 number(2),
    5 layer3 number(2));
    -- where layer1, layer2, layer3.... are increased up to layer 22 and could be increased more...
    Now each username will have 1 in any one of the layer, and all other layers will have 0
    Like if I consider the above table this could be the sample data:
    imran 1 0 0
    hafeez 0 1 0
    james 0 0 1
    Now the result my query should return is:
    select username, <column name where value is 1> and value of it, as per sample data, this should be return by the query:
    imran layer1 1
    hafeez layer2 1
    james layer 3 1
    Note: Please remember the columns are not fixed, they are added/altered by application.
    Regards,
    Imran

    The table design is incorrect as it is not in 3NF and has a repeating group
    You should correct the 'design' first or turn the layer... columns in a VARRAY.
    Also my feeling is you should change your subject line 'Help in writing a query!' in 'Write my query' as that is what you actually ask, and leave out the exclamation mark.
    Basically you dump everything in this forum, and being a bit less demanding might suit you better.
    Also you still act rude by not marking your questions as answered.
    Change that
    Sybrand Bakker
    Senior Oracle DBA
    Edited by: sybrand_b on 1-apr-2012 12:09

  • Need help with writing a query with dynamic FROM clause

    Hi Folks,
    I need help with an query that should generate the "FROM" clause dynamically.
    My main query is as follows
    select DT_SKEY, count(*)
    from *???*
    where DT_SKEY between 20110601 and 20110719
    group by DT_SKEY
    having count(*) = 0
    order by 1; The "from" clause of the above query should be generated as below
    select 'Schema_Name'||'.'||TABLE_NAME
    from dba_tables
    where OWNER = 'Schema_Name'Simply sticking the later query in the first query does not work.
    Any pointers will be appreciated.
    Thanks
    rogers42

    Hi,
    rogers42 wrote:
    Hi Folks,
    I need help with an query that should generate the "FROM" clause dynamically.
    My main query is as follows
    select DT_SKEY, count(*)
    from *???*
    where DT_SKEY between 20110601 and 20110719
    group by DT_SKEY
    having count(*) = 0
    order by 1; The "from" clause of the above query should be generated as below
    select 'Schema_Name'||'.'||TABLE_NAME
    from dba_tables
    where OWNER = 'Schema_Name'
    Remember that anything inside quotes is case-sensitive. Is the owner really "Schema_Name" with a capital S and a capital N, and 8 lower-case letters?
    Simply sticking the later query in the first query does not work.Right; the table name must be given when you compile the query. It's not an expression that you can generate in the query itself.
    Any pointers will be appreciated.In SQL*Plus, you can do something like the query bleow.
    Say you want to count the rows in scott.emp, but you're not certain that the name is emp; it could be emp_2011 or emp_august, or anything else that starts with e. (And the name could change every day, so you can't just look it up now and hard-code it in a query that you want to run in the future.)
    Typically, how dynamic SQL works is that some code (such as a preliminary query) gets some of the information you need to write the query first, and you use that information in a SQL statement that is compiled and run after that. For example:
    -- Preliminary Query:
    COLUMN     my_table_name_col     NEW_VALUE my_table_name
    SELECT     table_name     AS my_table_name_col
    FROM     all_tables
    WHERE     owner          = 'SCOTT'
    AND     table_name     LIKE 'E%';
    -- Main Query:
    SELECT     COUNT (*)     AS cnt
    FROM     scott.&my_table_name
    ;This assumes that the preliminary query will find exactly one row; that is, it assumes that SCOTT has exactly one table whose name starts with E. Could you have 0 tables in the schema, or more than 1? If so, what results would you want? Give a concrete example, preferably suing commonly available tables (like those in the SCOTT schema) so that the poepl who want to help you can re-create the problem and test their ideas.
    Edited by: Frank Kulash on Aug 11, 2011 2:30 PM

  • Need Help in writing a UDF

    Hi,
    Please help in writing a UDF for these fields in mapping, which I need for the object Iu2019m doing currently.
    The fields are like this:
    1)         batch_no = "TRUNC((GetMaxObjid('x_txn_sap_parts')-POWER(2,28))/5000)+1",
    2)         lot_id = "TRUNC((GetMaxObjid('x_txn_sap_parts')-POWER(2,28))/500)+1",
    3)        How to use JDBC connection factory class in the UDF.
    Some logic I can provide which I know i.e. Power (2, 28) means 2 to the power of 28 (2 multiplied 28 times), Trunc means truncate, X_TXN_SAP_Parts is a database table.The Target fields are Batch_no, lot_id & Objid.Actually, objid is mapped initially to a source field i.e. Object ID and in this function it is only being used for the calculation of the batch_no.
    Thanks in Advance,
    Sreedhar.

    Hi,
    Following with my query I've tried to use this code but still I'm unable to execute the mapping.
    import java.util.*;
    import com.sap.aii.mapping.lookup.*;
    import com.sap.aii.mapping.lookup.*;
    DataBaseAccessor accessor = null;
    DataBaseResult JDBCOutPayload = null;
    String BusinessSystem="clarify_dev_bizsys";
    //give your business system having channel name
    String CommunicationChannel="JDBC_TO_CDEV";
    //give your channel name
    String InputPayload= " select X_TXN_PRE_SITE_XFACE.nextval from dual;";
    //give your sql query
    try {
    Channel channel =
    LookupService.getChannel(BusinessSystem,CommunicationChannel);
    accessor = LookupService.getDataBaseAccessor(channel);     
    DataBaseResult resultSet = accessor.execute(InputPayload);
         for(Iterator rows = resultSet.getRows();rows.hasNext();){  
         Map rowMap = (Map)rows.next();
         Object cValue = rowMap.get("batchno");
    //field name of field required , as in database
          catch (Exception e) {}
          finally {
                  if (accessor != null)
                  accessor.close();
           result.addValue((String)cValue);
    --> I don't know what are the parameters to be used and how to be used in the UDF because this is the first time I'm writing a UDF.
    --> The problem in using this query is that both OBJID & BatchNo. are on the target side and the value for the OBJID is retrieved by a SELECT query from the database.
    Kindly help me how to resolve this query of mine.
    Thanks in Advance.
    Sreedhar.

  • Help needed in query for Materialized View

    Hi,
    I want to create a materialized view which has some precalcultaed values.
    I have four dimension tables out of which one is a Time Dimension table with levels as Year->Quarter->Month.
    The precalculations are the moving averages and cummulative values of Sales Amt on the dimension values over a period of 4 Months.
    The dimension tables are Clients, Products, Channel, Time.
    Fact Table is Sales_Fact which will have the sales amount for different members of the dimension.
    Since my fact table is very huge, i want to create a materialized view where i could store the required calculated measures, here Moving Average of Sales Amt for each Client, Product, Channel dimensions over a period of 4 Months.
    Can anybody help with writing the query for this..Any help in this regard will be appreciated..
    Please give me suggestions for this..

    Check this link.
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14223/aggreg.htm#sthref1612
    Regards
    Raj

  • Require help for a query

    Hi all..
    I required some help in writing a query..
    My table is like this
    Cust_id     Cust_type Del_status Incharge
    111     Gold     HD          
    222     Gold     
    333     Gold     HD
    444     Gold
    123     Gold     HD          
    456     Gold          
    789     Gold     HD
    987     Gold          
    555     Silver     HD
    666     Silver     HD
    777     Silver
    888     Silver
    I want a query to generate this output
    Cust_id     Cust_type Del_status Incharge
    111     Gold     HD     1
    222     Gold          1
    333     Gold     HD      1
    444     Gold          1
    555     Silver     HD     1
    777     Silver          1
    123     Gold     HD     2
    456     Gold          2
    789     Gold     HD     2
    987     Gold          2
    666     Silver     HD     2
    888     Silver          2
    The query basically allocates the customers to incharges... based on cust_type and del_status.
    there are 3 categories, Gold Customers, Silver Customers and HD customers..
    It should divide this three equally amongst the 2 incharges...
    Also this is just a sample data... actually table consists of around 3Lac customers and 12 incharges
    Sorry if its a incorrect post..
    Thanks in advance..

    Is there a way to find the value for ntile(2) dynamically?
    I tried something like
    Connected to:
    Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64bit Production
    With the Partitioning, OLAP and Oracle Data Mining options
    JServer Release 9.2.0.8.0 - Production
    SQL> with mytable as (select 111 cust_id, 'Gold' cust_type, 'HD' del_status from dual UNION ALL
      2  select 222 cust_id, 'Gold' cust_type, null del_status from dual UNION ALL
      3  select 333 cust_id, 'Gold' cust_type, 'HD' del_status from dual UNION ALL
      4  select 444 cust_id, 'Gold' cust_type, null del_status from dual UNION ALL
      5  select 123 cust_id, 'Gold' cust_type, 'HD' del_status from dual UNION ALL
      6  select 456 cust_id, 'Gold' cust_type, null del_status from dual UNION ALL
      7  select 789 cust_id, 'Gold' cust_type, 'HD' del_status from dual UNION ALL
      8  select 897 cust_id, 'Gold' cust_type, null del_status from dual UNION ALL
      9  select 555 cust_id, 'Silver' cust_type, 'HD' del_status from dual UNION ALL
    10  select 666 cust_id, 'Silver' cust_type, 'HD' del_status from dual UNION ALL
    11  select 777 cust_id, 'Silver' cust_type, null del_status from dual UNION ALL
    12  select 888 cust_id, 'Silver' cust_type, null del_status from dual UNION ALL
    13  select 1001 cust_id, 'Copper' cust_type, null del_status from dual UNION ALL
    14  select 1002 cust_id, 'Copper' cust_type, 'HD' del_status from dual UNION ALL
    15  select 1003 cust_id, 'Copper' cust_type, null del_status from dual
    16  )
    17  select t1.cust_id
    18  , t1.cust_type
    19  , t1.del_status
    20  --, ntile(3) over (partition by nvl(t1.del_status,t1.cust_type) order by t1.cust_id)
    21  , ntile((select count(distinct nvl(t2.del_status,t2.cust_type))-1 from mytable t2)) over (parti
    tion by nvl(del_status,cust_type) order by cust_id)
    22  incharge
    23  from mytable t1
    24  order by incharge, t1.cust_type, t1.cust_id
    25  /
    , ntile((select count(distinct nvl(t2.del_status,t2.cust_type))-1 from mytable t2)) over (partition
    ERROR at line 21:
    ORA-30488: argument should be a function of expressions in PARTITION BY
    SQL> The number of incharges could change during the time.
    Message was edited by:
    Sven Weller

  • Need a help in SQL query

    Hi,
    I need a help in writing an SQL query . I am actually confused how to write a query. Below is the scenario.
    CREATE TABLE demand_tmp
    ( item_id  NUMBER,
      org_id   NUMBER,
      order_line_id NUMBER,
      quantity NUMBER,
      order_type NUMBER
    CREATE TABLE order_tmp
    ( item_id  NUMBER,
       org_id   NUMBER,
       order_line_id NUMBER,
       open_flag  VARCHAR2(10)
    INSERT INTO demand_tmp
    SELECT 12438,82,821,100,30 FROM dual;
    INSERT INTO demand_tmp
    SELECT 12438,82,849,350,30 FROM dual;
    INSERT INTO demand_tmp
    SELECT 12438,82,NULL,150,29 FROM dual;
    INSERT INTO demand_tmp
    SELECT 12438,82,0,50,-1 FROM dual;
    INSERT INTO order_tmp
    SELECT 12438,82,821,'Y' FROM dual;
    INSERT INTO order_tmp
    SELECT 12438,82,849,'N' FROM dual;
    Demand_tmp:
    Item_id        org_id   order_line_id       quantity       order_type     
    12438     82                 821                 100       30     
    12438     82                 849                 350       30     
    12438     82              NULL                 150       29     
    12438     82                    0                  50       -1     
    Order_tmp :
    Item_id        org_id        order_line_id      open_flag     
    12438     82                  821                Y     
    12438     82                  849                N     I need to fetch the records from demand_tmp table whose order_line_id is present in order_tmp and having open_flag as 'Y' or if order_type in demand_tmp table is 29.
    The below query will give the records whose order line id is present in order_tmp. But, If i need records which are having order_type=29 the below query wont return any records as order_line_id is NULL. If I place outer join I will get other records also (In this example order_type -1 records) . Please help me how can we write a query for this. Expected o/p is below.
    Query :
    Select item_id,org_id,order_line_id,quantity,order_type,open_flag
    from demand_tmp dt , order_tmp ot
    where dt.order_line_id = ot.order_line_id
    AND dt.item_id=ot.item_id
    AND dt.org_id = ot.org_id
    AND ot.open_flag = 'Y';
    Expected Output :                         
    item_id     org_id     order_line_id     quantity     order_type   open_flag
    12438     82                 821               100                    30             Y
    12438     82              NULL               150                29         NULL Thanks in advance,
    Rakesh
    Edited by: Venkat Rakesh on Oct 7, 2012 6:32 PM
    Edited by: Venkat Rakesh on Oct 7, 2012 8:39 PM

    Hi Rakesh,
    the query is not working as you would like ( but IS working as expected ) since your trying to compare null to another value.
    Comparing null always results in FALSE, also if you compare null to null. This is because null means undefined.
    select 1 from dual where null=null results in no data found.
    I would suggest using a non natural key to join the tables.
    For example include a column ID in the master table which is filled with a sequence and include that field as a foreign key in the detail table.
    This way you can easily join master and detail on ID = ID, and you don't have to worry about null values in this column since it's always filled with data.
    Regards,
    Bas
    btw, using the INNER JOIN and OUTER JOIN syntax in your SQL makes it better readable, since you're separating join conditions from the where clause, just a tip ;)

  • Assistance in writing Query / Query Logic.

    I have a peculiar requirement to fetch data.
    Table Structure ( Sample ... although real table has more fields)
    Order ( orderid varchar2(30), custid varchar2(30), status varchar2(1), createdon datetime )
    Sample Data:
    OrderID CustID AccID Status createdon
    123 111 Y 20-Jan-2012 01:29:22
    124 111 100 Y 22-Jan-2012 01:23:02
    125 111 100 N 23-Jan-2012 02:34:24
    126 111 100 N 28-Jan-2012 04:23:22
    127 111 100 N 29-Jan-2012 07:32:26
    123Cancel 111 100 N 31-Jan-2012 03:27:29
    I want to write 2 Queries.
    a. input is OrderId, Need to find out how many order's present with status = 'N' after that orderid provided.
    b. input is OrderId, Need to find out what is the previous order with status = 'Y'
    Note: There can be 100's of orders and Status would be Y on first come first serve basis... if one does not have status = 'Y' ie it is 'N' the following order's will never turn to final status = 'Y'.
    There are millions of records in the table.
    Your help for writing this query / logic would be highly appreciated. Am intending to do thru single query.
    Best Regards
    MH Doshi

    select count(*) from order
    where createdon >( select created from order where order_id = &order_id);
    and status = 'N';
    select * from (select a.*, rownum rn from (select order_id from order
    where status = 'Y'
    and createdon<(seelect createdon from order where order_id = &order_id)
    order by createdon)) where rn=1

  • Need Help on below Query.

    Hi All,
    Need Help on below Query.
    Consider,
    "test9" Table Data in COLUMN "Name" AS
    Name
    =====
    'a'
    'b'
    'c'
    'd'
    'e'
    I am writing a query as :
    SELECT * FROM test9 WHERE Name IN ('a','b','c','d','e','f','g')
    I want result set as , It should show data as -
    'f'
    'g'
    i.e. data which does not exists in the table and which is give in in clause
    Is it possible in a single query.

    You can put the data that is to be checked for into a table instead or an inline view, for example:
    with t as
    (select 'a' as c1 from dual
    union all
    select 'b' from dual
    union all
    select 'c' from dual
    union all
    select 'd' from dual
    union all
    select 'e' from dual)
    select c1 from (select 'a' as c1 from dual
    union all
    select 'b' from dual
    union all
    select 'c' from dual
    union all
    select 'd' from dual
    union all
    select 'e' from dual
    union all
    select 'f' from dual
    union all
    select 'g' from dual)
    minus
    select c1 from t
    C
    f
    g
    2 rows selected.

  • I need help with a query

    Hello everyone,
    First, some background information.  We have in place a table which records status changes on a work order.  The orders normally go through each status only once, however they do occasionally reuse status indicators.  For example, an order is placed on hold, released from hold, placed back on hold, released again and so on.  The sample data provided is an example of an order with data repeating itself.  I need some help with writing a query on this table.
    LOC_CODE
    WO_NO
    UPDATETIME
    WO_STATUS_OLD
    WO_STATUS_NEW
    xxx
    12345
    05-01-2013 10:24:00
    WR
    SP
    xxx
    12345
    05-01-2013 10:39:00
    SP
    PM
    xxx
    12345
    05-01-2013 11:52:00
    PM
    ES
    xxx
    12345
    05-01-2013 11:58:00
    ES
    MO
    xxx
    12345
    05-01-2013 12:03:00
    MO
    ES
    xxx
    12345
    05-01-2013 12:38:00
    ES
    AT
    xxx
    12345
    05-01-2013 12:48:00
    AT
    RS
    xxx
    12345
    05-01-2013 13:01:00
    RS
    RA
    xxx
    12345
    05-01-2013 13:26:00
    RA
    RS
    xxx
    12345
    05-01-2013 13:36:00
    RS
    RA
    xxx
    12345
    05-01-2013 15:35:00
    RA
    RS
    xxx
    12345
    05-01-2013 15:42:00
    RS
    RA
    xxx
    12345
    05-01-2013 16:04:00
    RA
    RS
    xxx
    12345
    05-01-2013 16:42:00
    RS
    RA
    xxx
    12345
    05-01-2013 19:28:00
    RA
    FD
    xxx
    12345
    05-01-2013 19:28:00
    FD
    SO
    The query (which will in turn be used for a view) will display the elapsed time between status updates (subtract updatetime from the record preceeding).  Only the first record for each order at a location would have no elapsed time.  The result should look like this:
    LOC_CODE
    WO_NO
    UPDATETIME
    WO_STATUS_OLD
    WO_STATUS_NEW
    MINUTES_ELAPSED
    xxx
    12345
    05-01-2013 10:24:00
    WR
    SP
    {null}
    xxx
    12345
    05-01-2013 10:39:00
    SP
    PM
    15
    xxx
    12345
    05-01-2013 11:52:00
    PM
    ES
    73
    xxx
    12345
    05-01-2013 11:58:00
    ES
    MO
    6
    xxx
    12345
    05-01-2013 12:03:00
    MO
    ES
    5
    xxx
    12345
    05-01-2013 12:38:00
    ES
    AT
    35
    xxx
    12345
    05-01-2013 12:48:00
    AT
    RS
    10
    xxx
    12345
    05-01-2013 13:01:00
    RS
    RA
    13
    xxx
    12345
    05-01-2013 13:26:00
    RA
    RS
    25
    xxx
    12345
    05-01-2013 13:36:00
    RS
    RA
    10
    xxx
    12345
    05-01-2013 15:35:00
    RA
    RS
    119
    xxx
    12345
    05-01-2013 15:42:00
    RS
    RA
    7
    xxx
    12345
    05-01-2013 16:04:00
    RA
    RS
    22
    xxx
    12345
    05-01-2013 16:42:00
    RS
    RA
    38
    xxx
    12345
    05-01-2013 19:28:00
    RA
    FD
    166
    xxx
    12345
    05-01-2013 19:28:00
    FD
    SO
    0
    I have been trying various queries, but no luck as of yet.  I would appreciate your input.
    Thank you,
    Patrick

    Sorry about the late reply.  I had an unexpected meeting to attend.  Here is the requested information
    We are running Oracle Database 11g Release 11.2.0.3.0 - 64bit Production.
    --  DDL for Table WO_STATUS
      CREATE TABLE "WO_STATUS"
       ( "LOC_CODE" VARCHAR2(3 BYTE),
      "WO_NO" NUMBER,
      "UPDATE_DATETIME" DATE,
      "WO_STATUS_OLD" VARCHAR2(2 BYTE),
      "WO_STATUS_NEW" VARCHAR2(2 BYTE)
    INSERT INTO WO_STATUS (LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12345,{ts '2013-05-01 10:24:00'},'WR','SP');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12345,{ts '2013-05-01 10:39:00'},'SP','PM');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12345,{ts '2013-05-01 11:52:00'},'PM','ES');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12345,{ts '2013-05-01 11:58:00'},'ES','MO');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12345,{ts '2013-05-01 12:03:00'},'MO','ES');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12345,{ts '2013-05-01 12:38:00'},'ES','AT');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12345,{ts '2013-05-01 12:48:00'},'AT','RS');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12345,{ts '2013-05-01 13:01:00'},'RS','RA');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12345,{ts '2013-05-01 13:26:00'},'RA','RS');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12345,{ts '2013-05-01 13:36:00'},'RS','RA');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12345,{ts '2013-05-01 15:35:00'},'RA','RS');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12345,{ts '2013-05-01 15:42:00'},'RS','RA');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12345,{ts '2013-05-01 16:04:00'},'RA','RS');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12345,{ts '2013-05-01 16:42:00'},'RS','RA');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12345,{ts '2013-05-01 19:28:00'},'RA','FD');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12345,{ts '2013-05-01 19:28:00'},'FD','SO');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12346,{ts '2013-06-18 09:35:00'},'PM','ES');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12346,{ts '2013-06-18 09:37:00'},'ES','AT');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12346,{ts '2013-06-18 09:45:00'},'AT','RS');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12346,{ts '2013-06-18 09:51:00'},'RS','RA');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12346,{ts '2013-06-18 10:01:00'},'RA','FD');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12346,{ts '2013-06-18 10:02:00'},'FD','SO');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12346,{ts '2013-06-18 10:23:00'},'SO','MP');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12347,{ts '2013-06-18 08:29:00'},'WR','SP');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12347,{ts '2013-06-18 09:07:00'},'SP','PM');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12347,{ts '2013-06-18 09:48:00'},'PM','ES');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12347,{ts '2013-06-18 09:51:00'},'ES','AT');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12347,{ts '2013-06-18 10:19:00'},'AT','FD');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12347,{ts '2013-06-18 10:20:00'},'FD','SO');
    INSERT INTO WO_STATUS(LOC_CODE,WO_NO,UPDATE_DATETIME,WO_STATUS_OLD,WO_STATUS_NEW) VALUES ('xxx',12347,{ts '2013-06-18 10:24:00'},'SO','PY');

Maybe you are looking for