Help need in this query

Hi All,
I create following query which return my required results. but when i insert a new entry in the table it returns a new group of the same month and not add it in the above group where i doing mistake any idea ????
My required results
SQL> SELECT DISTINCT TO_CHAR(T.POS_DATE, 'Month') Month,count(*) Date_rec,sum(t.amount) amount,
  2   t.Asset, avg(t.amount) avg_ora_col,Sum(t.amount)/ count(*) "Normal Average"
  3  from pos t
  4  group by t.pos_date,t.asset;
MONTH      DATE_REC    AMOUNT ASSET      AVG_ORA_COL Normal Average
November          3       750 Loans              250            250
December          2       900 Loans              450            450
SQL> select * from pos;
POS_DATE  ASSET         AMOUNT
01-NOV-07 Loans            100
01-NOV-07 Loans            250
01-NOV-07 Loans            400
02-NOV-07 Loans            100
02-NOV-07 Loans            250
02-NOV-07 Loans            400
03-NOV-07 Loans            100
03-NOV-07 Loans            250
03-NOV-07 Loans            400
09-DEC-07 Loans            500
09-DEC-07 Loans            400
11 rows selected.
After insertion records in table it looks like following:
SQL> SELECT DISTINCT TO_CHAR(T.POS_DATE, 'Month') Month,count(*) Date_rec,sum(t.amount) amount,
  2   t.Asset, avg(t.amount) avg_ora_col,Sum(t.amount)/ count(*) "Normal Average"
  3  from pos t
  4  group by t.pos_date,t.asset;
MONTH      DATE_REC    AMOUNT ASSET      AVG_ORA_COL Normal Average
November          3       750 Loans              250            250
December          2       900 Loans              450            450
November          1       300 Loans              300            300
December          1       300 Loans              300            300
SQL> select * from pos;
POS_DATE  ASSET         AMOUNT
01-NOV-07 Loans            100
01-NOV-07 Loans            250
01-NOV-07 Loans            400
02-NOV-07 Loans            100
02-NOV-07 Loans            250
02-NOV-07 Loans            400
03-NOV-07 Loans            100
03-NOV-07 Loans            250
03-NOV-07 Loans            400
09-DEC-07 Loans            500
09-DEC-07 Loans            400
10-DEC-07 Loans            300
27-NOV-07 Loans            300
13 rows selected.
My requirment is following
SQL> SELECT DISTINCT TO_CHAR(T.POS_DATE, 'Month') Month,count(*) Date_rec,sum(t.amount) amount,
  2   t.Asset, avg(t.amount) avg_ora_col,Sum(t.amount)/ count(*) "Normal Average"
  3  from pos t
  4  group by t.pos_date,t.asset;
MONTH      DATE_REC    AMOUNT ASSET     
November          4       637 Loans             
December          2      600 Loans              I want to divide no. of occurance of dates.
which is 4 in Nov and 2 in dec.
Message was edited by:
53637

hi,
try this query
Instead of grouping by t.pos_date group by TO_CHAR(T.POS_DATE, 'Month')
SELECT DISTINCT TO_CHAR(T.POS_DATE, 'Month') Month,count(*) Date_rec,sum(t.amount) amount,
t.Asset, avg(t.amount) avg_ora_col,Sum(t.amount)/ count(*) "Normal Average"
from pos t
group by TO_CHAR(T.POS_DATE, 'Month'),t.asset
hope this will work
Regards,
Sridhar

Similar Messages

  • Help needed on this QUERY

    Hello everyone,
    We have a table with around 2-3 million records. There are two columns, ID (Primary Key) and NEXT_ID like this:
    select ID, NEXT_ID from table_id;
    ID NEXT_ID
    100 200
    150 220
    160
    180
    200 300
    220
    300 20000
    20000
    We expect to get a output like following:
    ID SAME_ID
    100 100
    200 100
    300 100
    20000 100
    150 150
    220 150
    160 160
    180 180
    Could anybody please help us. Thanks in advance!!!

    Hi,
    Here's one way:
    SELECT     id
    ,     CONNECT_BY_ROOT     id     AS same_id
    FROM     table_x
    START WITH     id NOT IN (
                                  SELECT  next_id
                     FROM       table_x
                     WHERE       next_id     IS NOT NULL
    CONNECT BY     id = PRIOR next_id;

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

  • Could you please help me make this query less complicated

    could you please help me make this query less complicated
    select t1.R_OBJECT_ID
    from dm_relation_sp a, ddt_resolution_sp t1
    where a.parent_id = '0900000283560456' -----------ID
    and a.child_id = t1.R_OBJECT_ID
    union all
    select t3.R_OBJECT_ID
    from ddt_resolution_sp t3
    where t3.R_OBJECT_ID in (select t2.child_id
    from dm_relation_sp t2
    where t2.parent_id in (select a.child_id
    from asud_fsk.dm_relation_sp a, asud_fsk.ddt_resolution_sp t1
    where a.parent_id = '0900000283560456' -----------ID
    and a.child_id = t1.R_OBJECT_ID))
    union all
    select t4.R_OBJECT_ID
    from ddt_resolution_sp t4
    where t4.R_OBJECT_ID in(
    select t3.child_id from dm_relation_sp t3
    where t3.parent_id in (
    select t2.child_id
    from asud_fsk.dm_relation_sp t2
    where t2.parent_id in (select a.child_id
    from asud_fsk.dm_relation_sp a, asud_fsk.ddt_resolution_sp t1
    where a.parent_id = '0900000283560456' -----------ID
    and a.child_id = t1.R_OBJECT_ID))
    and t3.relation_name = 'RESOLUTION_RELATION')
    union all
    select t5.R_OBJECT_ID
    from ddt_resolution_sp t5
    where t5.R_OBJECT_ID in
    (select t4.child_id
    from dm_relation_sp t4
    where t4.parent_id in(
    select t3.child_id from dm_relation_sp t3
    where t3.parent_id in (
    select t2.child_id
    from asud_fsk.dm_relation_sp t2
    where t2.parent_id in (select a.child_id
    from asud_fsk.dm_relation_sp a, asud_fsk.ddt_resolution_sp t1
    where a.parent_id = '0900000283560456' -----------ID
    and a.child_id = t1.R_OBJECT_ID))
    and t3.relation_name = 'RESOLUTION_RELATION')
    and t4.relation_name = 'RESOLUTION_RELATION')
    Edited by: user13025450 on 29.04.2010 16:23

    Hi,
    Welcome to the forum! You'll find that there are many qualified people (such as Tubby) willing to help you. Will you do what you can to help them? In order to simplify the query you posted,someone will first have to understand what it does, what the tables that it uses are like, and what tools you have to work with (that is, what version of Oracle, and any other software you are using). To actually test their ideas, people will need versions of your tables.
    Many people, if they spent enough time, might be able to figure out roughly what you are doing, make some tables themselves and test a solution. You can help with all of that. I assume you already know what the appliction is, and what this particular query is supposed to do: you don't have to figure out any of that, you just have to say it. You know what all your tables are, what the datatypes of all the columns are, and what kinds of data are in the tables: you don't have to guess at any of that.
    Describe what you're doing. Think about it: how do we know that
    SELECT  NULL
    FROM    dual;doesn't do what you want?
    Post CREATE TABLE and INSERT statements for a little sample data.
    Post the results that this query is supposed to produce from those results. (Is it producing the right output now? Then it should be easy to post the correct results.)
    Describe, as well as you can, how the present query is doing it.
    Format your existing code, so it's easy to see what the different branches of the UNION are, and what the main clauses are in each one.
    When posting formatted text (code or results) type these 6 characters
    \(all small letters, inside curly brackets) before and after each formatted section, to keep this site from compressing the spaces.
    Say what versions of Oracle (e.g. 10.2.0.3.0) and any other relevant software you are using.
    I know it's a lot of work, but it really helps. You can do it as well (probably better) than anyone else, and if you're unwilling to do it, why should anyone else be willing?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Help me in this query

    My Dear
    if I have table like emp table in scott plus new column month_sal
    And I write qery like this
    select ename,avg(sal) from emp
    where month_sal=2
    group by ename
    no Problem up to now
    I need Improve this query to be like this
    select ename,avg(sal) current_month,avg(sal) previous_month from emp
    where month_sal=2
    group by ename
    I need make another avgearge but for previuos parametre month
    thanks in advance

    user222 wrote:
    PLZ I need your helpIt would help if you provided some suitable example data and expected output for us so it was clear what you wanted, as described in the FAQ: {message:id=9360002}
    Perhaps this helps...
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (select 1 as item_code, 20 as item_price, 1 as mnth, 2011 as yr from dual union all
      2             select 2, 30, 1, 2011 from dual union all
      3             select 3, 15, 1, 2011 from dual union all
      4             select 4, 7, 2, 2011 from dual union all
      5             select 5, 8, 2, 2011 from dual union all
      6             select 6, 9, 2, 2011 from dual union all
      7             select 7, 10, 2, 2011 from dual union all
      8             select 8, 14, 3, 2011 from dual union all
      9             select 9, 21, 3, 2011 from dual union all
    10             select 10, 7, 4, 2011 from dual union all
    11             select 11, 10, 4, 2011 from dual union all
    12             select 12, 13, 4, 2011 from dual)
    13  --
    14  -- end of test data
    15  --
    16  select mnth, yr, mnth_avg_price
    17        ,lag(mnth_avg_price) over (order by yr, mnth) as prev_mnth_avg_price
    18  from (
    19        select mnth, yr, avg(item_price) as mnth_avg_price
    20        from t
    21        group by mnth, yr
    22       )
    23* order by yr, mnth
    SQL> /
          MNTH         YR MNTH_AVG_PRICE PREV_MNTH_AVG_PRICE
             1       2011     21.6666667
             2       2011            8.5          21.6666667
             3       2011           17.5                 8.5
             4       2011             10                17.5
    SQL>

  • I need that this query return only one value

    Hi, please. I need your help. I have this query:
    SELECT c.charvalue "Comentario", max(pi.id) as "id" --into :gcpe_last_comment
    FROM twflprocessinstances pi, twfliprocessparameters c
    WHERE pi.id = c.iprocess_id
    AND c.param_id = 1002286
    AND c.charvalue IS NOT NULL
    AND pi.processdef_id = 1600
    AND to_number(pi.id) <> 3817940
    AND to_number(pi.seeparameter1) =80137377
    group by c.charvalue
    having max(pi.id)
    This query return 3 rows, but I need that this query return only one row. The row that this query should return is the row before at the max id. Thanks

    Mmmm...I don't get it.
    You might need to post some sample data and expected results.

  • HELP TO TUNE THIS QUERY

    Hi,
    I'm using below query in procedure.It's taking more more time can some one help to tune this query or advice to rewrite the query.
    Databse :10.1
    SELECT   'Reading Comprehension' TEST_NAME,T.TEST_END_DATE TEST_SESSION_DATE,
            C.POOL_VERSION_ID, I.CREATED_ON POOL_CREATED_DT,
            C.ITEM_ID, C.ITEM_RESPONSE_ID, S.STUDENT_ID_PK, C.RESPONSE_KEY, C.IS_CORRECT RESPONSE_IS_CORRECT,
            T.SCORE SCALE_SCORE, C.RESPONSE_DURATION, P.ITEM_KEY,
            T.TEST_SESSION_DETAIL_ID, SYSDATE CREATED_ON
           -- BULK COLLECT INTO TV_PSYCHO_DET
            FROM
            CAT_ITEM_PARAMETER P, CAT_ITEM_USER_RESPONSE C, TEST_SESSION_DETAIL T,
            TEST_SESSION S, ITEM_POOL_VERSION I, TEST_DETAIL D
            ,INSTITUTION E
            WHERE  TRUNC(T.TEST_END_DATE) BETWEEN TO_DATE('01-11-09','dd-mm-yy') AND TO_DATE('30-11-09','dd-mm-yy')
            AND D.TEST_NAME =  'Reading Comprehension'
            AND T.TEST_SESSION_STATUS_ID = 3
            AND I.POOL_AVAILABILITY='Y'
            AND P.PRETEST=0 AND C.RESTART_FLAG=0
            AND T.TEST_DETAIL_ID = D.TEST_DETAIL_ID
            AND S.TEST_SESSION_ID = T.TEST_SESSION_ID
            AND C.TEST_SESSION_DETAIL_ID = T.TEST_SESSION_DETAIL_ID
            AND S.INSTITUTION_ID=E.INSTITUTION_ID
            AND SUBSTR(E.INSTITUTION_ID_DISPLAY,8,3) <> '000'
            AND I.ITEM_ID = C.ITEM_ID
            AND P.ITEM_ID = I.ITEM_ID;expln plan
    Plan hash value: 3712814491                                                                                                      
    | Id  | Operation                                 | Name                        | Rows  | Bytes | Cost (%CPU)| Time     | Pstart|
    Pstop |                                                                                                                          
    |   0 | SELECT STATEMENT                          |                             | 50857 |  7151K| 93382   (1)| 00:18:41 |       |
          |                                                                                                                          
    |*  1 |  FILTER                                   |                             |       |       |            |          |       |
          |                                                                                                                          
    |*  2 |   HASH JOIN                               |                             | 50857 |  7151K| 93382   (1)| 00:18:41 |       |
          |                                                                                                                          
    |   3 |    PARTITION HASH ALL                     |                             |  2312 | 23120 |    25   (0)| 00:00:01 |     1 |
        5 |                                                                                                                          
    |*  4 |     TABLE ACCESS FULL                     | CAT_ITEM_PARAMETER          |  2312 | 23120 |    25   (0)| 00:00:01 |     1 |
        5 |                                                                                                                          
    |*  5 |    HASH JOIN                              |                             | 94938 |    12M| 93356   (1)| 00:18:41 |       |
          |                                                                                                                          
    |*  6 |     TABLE ACCESS FULL                     | ITEM_POOL_VERSION           |  9036 |   132K|    30   (0)| 00:00:01 |       |
          |                                                                                                                          
    |*  7 |     TABLE ACCESS BY GLOBAL INDEX ROWID    | CAT_ITEM_USER_RESPONSE      |     9 |   279 |    18   (0)| 00:00:01 | ROWID |
    ROWID |                                                                                                                          
    |   8 |      NESTED LOOPS                         |                             | 45349 |  5270K| 93325   (1)| 00:18:40 |       |
          |                                                                                                                          
    |*  9 |       HASH JOIN                           |                             |  4923 |   423K| 11377   (1)| 00:02:17 |       |
          |                                                                                                                          
    |* 10 |        INDEX FAST FULL SCAN               | INSTI_ID_NAME_COUN_DISP_IDX |  8165 |   111K|    18   (0)| 00:00:01 |       |
          |                                                                                                                          
    |* 11 |        HASH JOIN                          |                             |  4923 |   355K| 11359   (1)| 00:02:17 |       |
          |                                                                                                                          
    |* 12 |         TABLE ACCESS BY GLOBAL INDEX ROWID| TEST_SESSION_DETAIL         |  4107 |   148K|  6804   (1)| 00:01:22 | ROWID |
    ROWID |                                                                                                                          
    |  13 |          NESTED LOOPS                     |                             |  4923 |   278K|  6806   (1)| 00:01:22 |       |
          |                                                                                                                          
    |* 14 |           INDEX RANGE SCAN                | TEST_DETAIL_AK_1            |     1 |    21 |     2   (0)| 00:00:01 |       |
          |                                                                                                                          
    |* 15 |           INDEX RANGE SCAN                | TEST_SESSION_DETAIL_FK2_I   | 39737 |       |   102   (0)| 00:00:02 |       |
          |                                                                                                                          
    |  16 |         PARTITION HASH ALL                |                             |  1672K|    25M|  4546   (1)| 00:00:55 |     1 |
        5 |                                                                                                                          
    |  17 |          TABLE ACCESS FULL                | TEST_SESSION                |  1672K|    25M|  4546   (1)| 00:00:55 |     1 |
        5 |                                                                                                                          
    |* 18 |       INDEX RANGE SCAN                    | CAT_ITEM_USER_RESP_IDX1     |    18 |       |     3   (0)| 00:00:01 |       |
          |                                                                                                                          
    Predicate Information (identified by operation id):                                                                              
       1 - filter(TO_DATE('01-11-09','dd-mm-yy')<=TO_DATE('30-11-09','dd-mm-yy'))                                                    
       2 - access("P"."ITEM_ID"="I"."ITEM_ID")                                                                                       
       4 - filter("P"."PRETEST"=0)                                                                                                   
       5 - access("I"."ITEM_ID"="C"."ITEM_ID")                                                                                       
       6 - filter("I"."POOL_AVAILABILITY"='Y')                                                                                       
       7 - filter(TO_NUMBER("C"."RESTART_FLAG")=0)                                                                                   
       9 - access("S"."INSTITUTION_ID"="E"."INSTITUTION_ID")                                                                         
      10 - filter(SUBSTR("E"."INSTITUTION_ID_DISPLAY",8,3)<>'000')                                                                   
      11 - access("S"."TEST_SESSION_ID"="T"."TEST_SESSION_ID")                                                                       
      12 - filter(TRUNC(INTERNAL_FUNCTION("T"."TEST_END_DATE"))>=TO_DATE('01-11-09','dd-mm-yy') AND "T"."TEST_SESSION_STATUS_ID"=3   
                  AND TRUNC(INTERNAL_FUNCTION("T"."TEST_END_DATE"))<=TO_DATE('30-11-09','dd-mm-yy'))                                 
      14 - access("D"."TEST_NAME"='Reading Comprehension')                                                                           
      15 - access("T"."TEST_DETAIL_ID"="D"."TEST_DETAIL_ID")                                                                         
      18 - access("C"."TEST_SESSION_DETAIL_ID"="T"."TEST_SESSION_DETAIL_ID")                                                         
    43 rows selected.Edited by: user575115 on Dec 18, 2009 12:31 AM

    When you see something like ...
       7 - filter(TO_NUMBER("C"."RESTART_FLAG")=0)                                                                                    It means that Oracle had to do a conversion for you since you aren't using the proper data type in your query.
    That would mean IF there is an index on that column, it won't be useable...

  • Help needed with this form in DW

    Hi, i have created this form in dreamweaver but ive got this problem.
    In the fields above the text field, the client needs to fill in some info such as name, email telephone number etc.
    But the problem is when ill get the messages. Only the text from the large text field is there.
    What did i do wrong??
    http://www.hureninparamaribo.nl/contact.html
    Thank you
    Anybody??

    Thank you for your response. So what do i have to do to fix this?
    Date: Sun, 20 Jan 2013 07:57:56 -0700
    From: [email protected]
    To: [email protected]
    Subject: Help needed with this form in DW
        Re: Help needed with this form in DW
        created by Ken Binney in Dreamweaver General - View the full discussion
    You have several duplicate "name" attributes in these rows which also appears in the first row
    Telefoon:
    Huurperiode:
    Aantal personen:
         Please note that the Adobe Forums do not accept email attachments. If you want to embed a screen image in your message please visit the thread in the forum to embed the image at http://forums.adobe.com/message/5008247#5008247
         Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page: http://forums.adobe.com/message/5008247#5008247
         To unsubscribe from this thread, please visit the message page at http://forums.adobe.com/message/5008247#5008247. In the Actions box on the right, click the Stop Email Notifications link.
         Start a new discussion in Dreamweaver General by email or at Adobe Community
      For more information about maintaining your forum email notifications please go to http://forums.adobe.com/message/2936746#2936746.

  • Help to Simplify this query

    i am using
    Oracle8i Enterprise Edition Release 8.1.7.4.0 - Production
    With the Partitioning option
    JServer Release 8.1.7.4.0 - Production
    Please help to simplify this query. I also want to make this as a procedure.
    select
    RH.resort as RH_RESORT,
    RH.rate_code as RH_RATE_CODE,
    RD.rate_detail_id as RD_RATE_DETAIL_ID,
    RD.rate1 as RD_RATE1,
    RD.rate2 as RD_RATE2,
    RD.rate3 as RD_RATE3,
    RD.rate4 as RD_RATE4,
    RD.rate5 as RD_RATE5,
    RRC.room_category as RM_CAT,
    initcap(RRMCAT.long_description) as RM_DESC,
    RH.DAY1,
    RH.DAY2,
    RH.DAY3,
    RH.DAY4,
    RH.DAY5,
    RH.DAY6,
    RH.DAY7,
    RH.WEEKEND1,
    RH.WEEKEND2,
    RH.WEEKEND3,
    RH.WEEKEND4,
    RH.WEEKEND5,
    RH.WEEKEND6,
    RH.WEEKEND7
    from pms_rate_header RH,
    pms_rate_detail RD,
    pms_rate_room_cat RRC,
    resort_room_category RRMCAT
    where RH.inactive_date is NULL
    and RH.RESORT='FABGOI'
    and RH.RATE_CODE ='CRRRACK'
    and ('15-Jan-2007' >= RH.BEGIN_DATE
    and '16-Jan-2007' <= RH.END_DATE)
    and RD.rate_header_id=RH.rate_header_id
    and RD.inactive_date is NULL
    and ('15-Jan-2007' >= RD.BEGIN_DATE
    and '16-Jan-2007' <= RD.END_DATE)
    and RRC.rate_detail_id=RD.rate_detail_id
    and RRC.resort=RH.resort
    and RRMCAT.resort=RH.resort
    and RRMCAT.room_category=RRC.room_category
    and RRC.inactive_date is NULL
    and RRMCAT.inactive_date IS NULL
    regards
    lee1212

    where RH.inactive_date is NULL
    and RH.RESORT='FABGOI'
    and RH.RATE_CODE ='CRRRACK'
    and ('15-Jan-2007' >= RH.BEGIN_DATE
    and '16-Jan-2007' <= RH.END_DATE)
    and RD.rate_header_id=RH.rate_header_id
    and RD.inactive_date is NULL
    and ('15-Jan-2007' >= RD.BEGIN_DATE
    and '16-Jan-2007' <= RD.END_DATE)
    and RRC.rate_detail_id=RD.rate_detail_id
    and RRC.resort=RH.resort
    and RRMCAT.resort=RH.resort
    and RRMCAT.room_category=RRC.room_category
    and RRC.inactive_date is NULL
    and RRMCAT.inactive_date IS NULL
    in the above part i am finding repeated joins and lot of 'and's
    i want to know any line can be eliminated or the the query can be rewritten in a simple way without affecting it's accuracy?
    regards
    Lee1212

  • Plz help me design this query

    Hi,
    Can you’ll please help me with this query.
    Here is a little bit of background:
    One title can have multiple items associated with it.
    Table: TITLE has the master list of titles. TITLE_ID is the primary key.
    Table: ITEM has the master list of all items. ITEM_ID is the primary. This table also has the TITLE_ID which stores title for this item.
    Table: ITEM_STATUS has fields ITEM_ID and STATUS_ID. This field contains statuses for items. But not all items contained in the table ITEM are in this table.
    I want to find TITLE_ID’s whose all items (all ITEM_ID in table ITEM having same value for TITLE_ID) have a particular status (for example STATUS_ID = 2) in table ITEM_STATUS.
    Let’s say TITLE_ID = 1 has 5 items in table ITEM_ID and only 4 items out of it in table ITEM_STATUS with STATUS_ID = 2, then this TITLE_ID should not come. OR
    Let’s say TITLE_ID = 1 has 5 items in table ITEM_ID and all these 5 items are in table ITEM_STATUS but only 1 has STATUS_ID = 2, then this TITLE_ID should also not come
    In the above case only if all 5 items are contained in table ITEM_STATUS have STATUS_ID = 2 then this TITLE_ID should be reported by the query.
    What should be the query like for this one, I am fairly new to SQL so plz guide me.
    Thank you,
    Raja

    I haven't tested the query below. Try it and let me know for any issues:
    SELECT     DISTINCT t.title_id
         FROM     title t,
                        item i,
                        item_status its
    WHERE     t.title_id = i.title_id
         AND     i.item_id = its.item_id
         AND     NOT EXISTS     (
                                                           SELECT 1
                                                                FROM item_status its1
                                                           WHERE its1.item_id = i.item_id
                                                                AND status_id <> 'YOUR_ITEM_STATUS'
                                                      )

  • Please help to modifiy this query for better performance

    Please help to rewrite this query for better performance. This is taking long time to execute.
    Table t_t_bil_bil_cycle_change contains 1200000 rows and table t_acctnumberTab countains  200000 rows.
    I have created index on ACCOUNT_ID
    Query is shown below
    update rbabu.t_t_bil_bil_cycle_change a
       set account_number =
           ( select distinct b.account_number
             from rbabu.t_acctnumberTab b
             where a.account_id = b.account_id
    Table structure  is shown below
    SQL> DESC t_acctnumberTab;
    Name           Type         Nullable Default Comments
    ACCOUNT_ID     NUMBER(10)                            
    ACCOUNT_NUMBER VARCHAR2(24)
    SQL> DESC t_t_bil_bil_cycle_change;
    Name                    Type         Nullable Default Comments
    ACCOUNT_ID              NUMBER(10)                            
    ACCOUNT_NUMBER          VARCHAR2(24) Y    

    Ishan's solution is good. I would avoid updating rows which already have the right value - it's a waste of time.
    You should have a UNIQUE or PRIMARY KEY constraint on t_acctnumberTab.account_id
    merge rbabu.t_t_bil_bil_cycle_change a
    using
          ( select distinct account_number, account_id
      from  rbabu.t_acctnumberTab
          ) t
    on    ( a.account_id = b.account_id
           and decode(a.account_number, b.account_number, 0, 1) = 1
    when matched then
      update set a.account_number = b.account_number

  • Query Issue in Creating a View....Please help me with this query..

    I would like to create a view on this table with four columns
    1. PN#_EXP_DATE
    2. PN#
    3. PN#_EFF_DATE
    4. PN#
    P_S_C     A_C     P     EXP_DT
    21698     13921     1     5/29/2009 3:15:41 PM     
    21698     13921     1     5/29/2009 3:54:57 PM     
    21698     1716656     4     5/29/2009 3:15:41 PM     
    21698     3217     3     5/29/2009 3:15:40 PM     
    21698     3217     3     5/29/2009 3:54:57 PM     
    21698     60559     2     5/29/2009 3:15:41 PM     
    21698     60559     2     5/29/2009 3:54:57 PM     
    I have a trigger on A, which inserts the DML records into B. (Table A and B structure is the almost the same,
                                       where table B will have one extra column exp_dt)
    NOw Table B can have records with same P_S_C and A_C columns and exp_dt will capture the history.
    for example: from the above sample data, let us take first two records..
    P_S_C     A_C     P     EXP_DT
    21698     13921     1     5/29/2009 3:15:41 PM     --- Record 1
    21698     13921     1     5/29/2009 3:54:57 PM     --- Record 2
    from this..
    Note: 1. Table A and Table C can be joined using A.P_S_C and C.R_C to get the start_date.
    2. PN# comes from table D. It contains numbers for the resp. weeks.
    3. PN#_EFF_DATE is the previous immediate previous date for that record in history table (Table B).
    I wanted the data like..
    ---- this is for the second record in the above..
    PN#_EXP_DATE PN# PN#_EFF_DATE PN#
    5/29/2009 3:54:57 PM     214 5/29/2009 3:15:41 PM     214
    ---- this is for the first record in the above..
    PN#_EXP_DATE PN# PN#_EFF_DATE PN#
    5/29/2009 3:54:41 PM     214 ( for this we should query the table C to get the
                        start_date, for this combinatation of P_S_C and A_C in table B
                             where B.P_S_C = C.A_C
                             and that value should be the EFF_DT for this record)
    Please help me with this....
    Thanks in advance..

    Hi All,
    select d.P# as "PN#_EXP_DATE", exp_date
    from daily_prd d, cbs1_assoc_hist b
    where to_char(d.day_date,'MM/DD/YYYY') = to_char(b.exp_date,'MM/DD/YYYY')
    and d.period_type = 'TIMEREPORT';
    This above query gives the output as follows:
    pn#_exp_date exp_date
    214     5/29/2009 3:15:40 PM
    214     5/29/2009 3:15:41 PM
    214          5/29/2009 3:15:41 PM
    214          5/29/2009 3:15:41 PM
    214          5/29/2009 3:54:57 PM
    214          5/29/2009 3:54:57 PM
    214          5/29/2009 3:54:57 PM
    This below is the data from history table (Table B).
    P_S_C     A_C PLACE EXP_DATE
    21698          3217          3     5/29/2009 3:15:40 PM     
    21698          13921          1     5/29/2009 3:15:41 PM     
    21698          1716656          4     5/29/2009 3:15:41 PM     
    21698          60559          2     5/29/2009 3:15:41 PM     
    21698          13921          1     5/29/2009 3:54:57 PM     
    21698          3217          3     5/29/2009 3:54:57 PM     
    21698          60559          2     5/29/2009 3:54:57 PM     
    I got the pn#_exp_date from the Table 'D', for the given exp_date from Table B.
    My question is again....
    CASE - 1
    from the given records above, I need to look for exp_date for the given same P_S_C and A_C.
    in this case we can take the example...
    P_S_C     A_C PLACE EXP_DATE
    21698          3217          3     5/29/2009 3:15:40 PM
    21698          3217          3     5/29/2009 3:54:57 PM
    In this case, the
    pn#_exp_date exp_date     pn#_eff_date eff_date
    214     5/29/2009 3:15:57 PM     < PN# corresponding to the     5/29/2009 3:15:40 PM
                        eff_date .>
                        <Basically the eff_date is
                        nothing but the exp_date only.
                        we should take the immediate before one.
    In this case, our eff_date is '5/29/2009 3:15:40 PM'.
    but how to get this.
    CASE - 2
    from the above sample data, consider this
    P_S_C     A_C PLACE EXP_DATE
    21698     1716656     4     5/29/2009 3:15:41 PM
    In this case, there is only one record for the P_S_C and A_C combination.
    The expected result :
    pn#_exp_date exp_date               pn#_eff_date eff_date
    214     5/29/2009 3:15:41 PM     < PN# corresponding to the     5/29/2009 3:15:40 PM
                        eff_date .>
                   <Basically the eff_date is
                   nothing but the exp_date only.
                        we should take the immediate before one.
    In this case to get the eff_date, we should query the Table 'C', to get the eff_date, which is START_DT column in this table.
    for this join B.P_S_C and C.R_C.
    Hope I am clear now..
    Thanks in advance.....

  • Help needed in a Query

    I have a table TAB that has two colums. COL1 and COL2. Both are composite Primary key.
    The data is as
    COL1 COL2
    1 C
    2 C
    3 C
    4 G
    5 G
    1 G
    3 G
    6 G
    7 G
    I need to find out those COL1 values whose COL2 values are C and G. Ex
    COL1 COL2
    1 C
    3 C
    1 G
    3 G
    So i need the following op
    COL1
    1
    3
    Please help me to solve this query.
    Regards.
    Message was edited by:
    SID

    and what have you tried ????
    SQL> With t As
      2    (
      3    Select 1 c1, 'C' c2 From DUAL Union All
      4    Select 2,     'C'   From DUAL Union All
      5    Select 3,     'C'   From DUAL Union All
      6    Select 4,     'G'   From DUAL Union All
      7    Select 5,     'G'   From DUAL Union All
      8    Select 1,     'G'   From DUAL Union All
      9    Select 3,     'G'   From DUAL Union All
    10    Select 6,     'G'   From DUAL Union All
    11    Select 7,     'G'   From DUAL
    12    )
    13    Select t.c1, t.c2
    14    From
    15    t,
    16    (
    17    Select c1, c2
    18    From t
    19    Where c2 = 'C'
    20    ) c,
    21    (Select c1, c2
    22     From t
    23     Where c2 = 'G'
    24     ) g
    25  Where t.c1 = c.c1
    26  And   t.c1 = g.c1
    27  --And   c.c2 = g.c2;
            C1 C2
             1 C
             1 G
             3 C
             3 G
    SQL>
    /*using analytical functons*/
    SQL>
    SQL> With t As
      2    (
      3    Select 1 c1, 'C' c2 From DUAL Union All
      4    Select 2,     'C'   From DUAL Union All
      5    Select 3,     'C'   From DUAL Union All
      6    Select 4,     'G'   From DUAL Union All
      7    Select 5,     'G'   From DUAL Union All
      8    Select 1,     'G'   From DUAL Union All
      9    Select 3,     'G'   From DUAL Union All
    10    Select 6,     'G'   From DUAL Union All
    11    Select 7,     'G'   From DUAL
    12    )
    13   select c1, c2
    14         from ( select c1,c2, case when c2 = 'C' then count(decode(c2,'G',1)) over (partition by c1)
    15                                    when c2 = 'G' then count(decode(c2,'C',1)) over (partition by c1)
    16                               else 0 end count                           
    18                from t
    19            )
    20      where Count != 0
    21  /
            C1 C2
             1 C
             1 G
             3 C
             3 GMessage was edited by:
    Nicloei W

  • Help in Tuning this Query

    Hi,
    I have a query in my proj where the same table is looked up twice in the same query.
    Can anybody suggest in improving the performance of this query?
    select * from table1 a1 where (a1.column1, a1.column2, a1.column3, a1.column4, a1.column5, a1.column6, a1.column7, a1.column8, a1.column9,
    a1.column10) in ( select a2.column1, a2.column2, a2.column3, a2.column4, a2.column5, a2.column6, a2.column7, a2.column8, a2.column9,
    a2.column10 from table1 a2 where column20 = '<condn>')
    The table1 used here is same in outer query as well as the sub query. this is a example of what we use here, and the table1 contains 30 million rows. Though, creating index with 10 columns can be a option, we already have a unique index with 11 columns(which includes 10 from this query) and will that be helpful in anyway? or the same existing index can be forced?
    Thanks a lot for ur time

    Depending on the selectivity of column20 I am not sure Index is the best way to go. It might perform better with two full scans and a hash-join.
    Anyway, I do prefer the syntax:
    select /*+ leading(a2) */ a1.*
    from table1 a1, table1 a2
    where a2.column20 = '<condn>'
    and a1.column1 = a2.column1
    and a1.column2 = a2.column2
    and  a1.column3 = a2.column3
    and  a1.column4 = a2.column4
    and  a1.column5 = a2.column5
    and  a1.column6 = a2.column6
    and  a1.column7 = a2.column7
    and  a1.column8 = a2.column8
    and  a1.column9 = a2.column9
    and  a1.column10 = a2.column10;I've added a leading hint to tell oracle that the start table is a2. Might be useless.
    Ensure your stats are up to date. You might need histograms here if your column20 is skewed.
    Hope this helps,
    François
    Edited by: Francois Berger on Oct 24, 2008 1:47 AM

  • Can u help me refine this query

    create table company
    (comp_id number primary key,
    comp_name varchar2(50) not null
    create table accident
    (acc_seq number primary key,
    acc_comp_id number not null,
    acc_series smallint not null,
    acc_id varchar2(10) not null,
    acc_type varchar2(50) not null)
    -- acc_series is an auto number to tell me total accident this company has so far
    insert into company(comp_id,comp_name)
    values(10001,'A Corp')
    insert into company(comp_id,comp_name)
    values(10002,'B Corp')
    insert into company(comp_id,comp_name)
    values(10003,'C Corp')
    insert into accident(acc_seq,acc_comp_id,acc_series,acc_id,acc_type)
    values(9001,10001,1,'AC-09-0001','CAR')
    insert into accident(acc_seq,acc_comp_id,acc_series,acc_id,acc_type)
    values(9002,10002,1,'AC-09-0002','MACHINARY')
    insert into accident(acc_seq,acc_comp_id,acc_series,acc_id,acc_type)
    values(9003,10001,2,'AC-09-0009','FIELD')
    -- problem for this query : I hardcoded number 1,2. In reality, I need a way to find the upper bound first then do the decode ( I think) and adjust the column heading accordingly, can u help ?
    select
    comp_name,
    max(decode(acc_series,1,acc_id)) acc1_id,
    max(decode(acc_series,1,acc_type)) acc1_type,
    max(decode(acc_series,2,acc_id)) acc2_id,
    max(decode(acc_series,2,acc_type)) acc2_type
    from company A,accident B
    where A.comp_id=B.acc_comp_id
    group by comp_name
    output
    COMP_NAME ACC1_ID ACC1_TYPE ACC2_ID ACC2_TYPE
    A Corp      AC-09-0001      CAR      AC-09-0009      FIELD
    B Corp      AC-09-0002      MACHINARY
    Thanks in advance.
    Munshar

    It looks like you are after a dynamic pivot.
    Check out this post by Frank that goes through an example on how to create a dynamic pivot: {message:id=3227388}

Maybe you are looking for