Better way to write the simple query?

I'm trying to get the date of 'busy' max 'mansion '.

It is an example, I imagined, since I can't post our actual data. The following query works, but is their path easier.
CREATE TABLE TEST_TABLE (  
LOAN_NUMBER                 VARCHAR2(15 Byte),
UN_ID                       NUMBER,
CHANGE_DATE                 DATE,
PROP_TYPE                   VARCHAR2(25 Byte),
OCCSTAT                     VARCHAR2(25 Byte)
); 
COMMIT;


INSERT INTO TEST_TABLE VALUES (123456,  1,'01-JAN-09','Tent','Occupied');
INSERT INTO TEST_TABLE VALUES (123456,  2,'01-FEB-09','Shack','Occupied');
INSERT INTO TEST_TABLE VALUES (123456,  3,'01-JUN-08','Single Family','Occupied');
INSERT INTO TEST_TABLE VALUES (123456,  4,'01-OCT-08','Single Family Plus','Occupied');
INSERT INTO TEST_TABLE VALUES (123456,  5,'01-DEC-08','Mansion','Occupied');
INSERT INTO TEST_TABLE VALUES (123456,  6,'05-JAN-09','Mansion','Unoccupied');
COMMIT;
Reason, I take the ID's for the second join because I know that the ID max = max.
select     
i2.UN_ID,
i2.CHANGE_DATE,
i2.PROP_TYPE,
i2.OCCSTAT
from 
(
    select 
        distinct(LOAN_NUMBER) AS "LOAN_ID", 
        max(UN_ID) AS "ID_MAX"
    from(
    select 
        LOAN_NUMBER,
        UN_ID
  from
        TEST_TABLE
    where OCCSTAT = 'Occupied'
    group by LOAN_NUMBER, UN_ID
    ) 
    group by LOAN_NUMBER
)i
left join TEST_TABLE i2 on i.ID_MAX = i2.UN_ID
easier way without the second join?

Thanks in advance.

R

Try this query, it should be equivalent to your:

select UN_ID, CHANGE_DATE, PROP_TYPE, OCCSTAT
from (
    select LOAN_NUMBER, UN_ID, CHANGE_DATE, PROP_TYPE, OCCSTAT, rank() over(partition by LOAN_NUMBER order by un_id desc) rn
    from test_table
     where OCCSTAT = 'Occupied'
)
where rn=1

Max

Tags: Database

Similar Questions

  • Best way to write the following query

    Hello
    I have following table structures and data...
    And I wrote the request in order to obtain records that are superior to BBB-
    But could you please me to write in a simpler way.
    create table obj (ob_id )
    as select 1 from dual union all
    select 2 from dual union all
    select 3 from dual union all
    select 4 from dual union all
    select 5 from dual union all
    select 6 from dual
    
    create table og_dt (or_id , rt_cd,rt_ct_cd)
    AS SELECT 1 ,'B','BRID' FROM DUAL UNION ALL
       SELECT 1 ,'B','BRD' FROM DUAL UNION ALL
       SELECT 2 ,'BB-','ACR' FROM DUAL UNION ALL
       SELECT 2 ,'BB-','AQCR' FROM DUAL UNION ALL
       SELECT 3 ,'BBB','QYRE' FROM DUAL UNION ALL
       SELECT 4 ,'BB+','TUR' FROM DUAL UNION ALL
       SELECT 5 ,'BBB-','KUYR' FROM DUAL 
       
       
    create table rt_srt (srt_ord,rt_cd,rt_ct_cd)
    as select 50 ,'B','VID' FROM DUAL UNION ALL
       SELECT 50 ,'B','BRD' FROM DUAL UNION ALL
       SELECT 40 ,'BB-','ACR' FROM DUAL UNION ALL
       SELECT 41 ,'BB-','AQCR' FROM DUAL UNION ALL
       SELECT 30 ,'BBB','QYRE' FROM DUAL UNION ALL
       SELECT 33 ,'BB+','TUR' FROM DUAL UNION ALL
       SELECT 20 ,'BBB-','KUYR' FROM DUAL 
       
          
    select distinct 
    *
       from obj,og_dt,rt_srt
      where obj.ob_id=og_dt.or_id
      and og_dt.rt_cd = rt_srt.rt_cd
      and og_dt.rt_ct_cd=rt_srt.rt_ct_cd
    and rt_srt.srt_ord > all (select rt_srt.srt_ord from rt_srt
    where rt_cd='BBB-' 
    I used the table rt_srt twise in the above query
    Could you advice please write it in simple way.

    Thank you

    Here's the implementation plans for 3 possible solutions (including the one you posted). Solutions of second & third assumes that rt_srt.srt_ord is not null:

    SQL> explain plan for
      2  select  distinct *
      3    from  obj,
      4          og_dt,
      5          rt_srt
      6    where obj.ob_id = og_dt.or_id
      7      and og_dt.rt_cd = rt_srt.rt_cd
      8      and og_dt.rt_ct_cd = rt_srt.rt_ct_cd
      9      and rt_srt.srt_ord > all (
     10                                select  rt_srt.srt_ord
     11                                  from  rt_srt
     12                                  where rt_cd = 'BBB-'
     13                               )
     14  /
    
    Explained.
    
    SQL> @?\rdbms\admin\utlxpls
    
    PLAN_TABLE_OUTPUT
    ---------------------------------------------------------------------------------------
    Plan hash value: 3210303028
    
    ---------------------------------------------------------------------------------
    | Id  | Operation              | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
    ---------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT       |        |     7 |   504 |    16  (25)| 00:00:01 |
    |   1 |  HASH UNIQUE           |        |     7 |   504 |    16  (25)| 00:00:01 |
    |   2 |   MERGE JOIN ANTI NA   |        |     7 |   504 |    15  (20)| 00:00:01 |
    |   3 |    SORT JOIN           |        |     7 |   385 |    11  (19)| 00:00:01 |
    |*  4 |     HASH JOIN          |        |     7 |   385 |    10  (10)| 00:00:01 |
    |*  5 |      HASH JOIN         |        |     7 |   238 |     7  (15)| 00:00:01 |
    
    PLAN_TABLE_OUTPUT
    ---------------------------------------------------------------------------------------
    |   6 |       TABLE ACCESS FULL| OBJ    |     6 |    78 |     3   (0)| 00:00:01 |
    |   7 |       TABLE ACCESS FULL| OG_DT  |     7 |   147 |     3   (0)| 00:00:01 |
    |   8 |      TABLE ACCESS FULL | RT_SRT |     7 |   147 |     3   (0)| 00:00:01 |
    |*  9 |    SORT UNIQUE         |        |     1 |    17 |     4  (25)| 00:00:01 |
    |* 10 |     TABLE ACCESS FULL  | RT_SRT |     1 |    17 |     3   (0)| 00:00:01 |
    ---------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       4 - access("OG_DT"."RT_CD"="RT_SRT"."RT_CD" AND
    
    PLAN_TABLE_OUTPUT
    ---------------------------------------------------------------------------------------
                  "OG_DT"."RT_CT_CD"="RT_SRT"."RT_CT_CD")
       5 - access("OBJ"."OB_ID"="OG_DT"."OR_ID")
       9 - access("RT_SRT"."SRT_ORD"<="RT_SRT"."SRT_ORD")
           filter("RT_SRT"."SRT_ORD"<="RT_SRT"."SRT_ORD")
      10 - filter("RT_CD"='BBB-')
    
    Note
    -----
       - dynamic sampling used for this statement (level=2)
    
    31 rows selected.
    
    SQL> explain plan for
      2  select  distinct *
      3    from  obj,
      4          og_dt,
      5          rt_srt
      6    where obj.ob_id = og_dt.or_id
      7      and og_dt.rt_cd = rt_srt.rt_cd
      8      and og_dt.rt_ct_cd = rt_srt.rt_ct_cd
      9      and rt_srt.srt_ord > (
     10                            select  max(rt_srt.srt_ord)
     11                              from  rt_srt
     12                              where rt_cd = 'BBB-'
     13                           )
     14  /
    
    Explained.
    
    SQL> @?\rdbms\admin\utlxpls
    
    PLAN_TABLE_OUTPUT
    ---------------------------------------------------------------------------------------
    Plan hash value: 3391900174
    
    ---------------------------------------------------------------------------------
    | Id  | Operation              | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
    ---------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT       |        |     1 |    55 |    14  (15)| 00:00:01 |
    |   1 |  HASH UNIQUE           |        |     1 |    55 |    14  (15)| 00:00:01 |
    |*  2 |   HASH JOIN            |        |     1 |    55 |    10  (10)| 00:00:01 |
    |   3 |    MERGE JOIN CARTESIAN|        |     2 |    68 |     6   (0)| 00:00:01 |
    |*  4 |     TABLE ACCESS FULL  | RT_SRT |     1 |    21 |     3   (0)| 00:00:01 |
    |   5 |      SORT AGGREGATE    |        |     1 |    17 |            |          |
    
    PLAN_TABLE_OUTPUT
    ---------------------------------------------------------------------------------------
    |*  6 |       TABLE ACCESS FULL| RT_SRT |     1 |    17 |     3   (0)| 00:00:01 |
    |   7 |     BUFFER SORT        |        |     6 |    78 |     3   (0)| 00:00:01 |
    |   8 |      TABLE ACCESS FULL | OBJ    |     6 |    78 |     3   (0)| 00:00:01 |
    |   9 |    TABLE ACCESS FULL   | OG_DT  |     7 |   147 |     3   (0)| 00:00:01 |
    ---------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       2 - access("OBJ"."OB_ID"="OG_DT"."OR_ID" AND
                  "OG_DT"."RT_CD"="RT_SRT"."RT_CD" AND
    
    PLAN_TABLE_OUTPUT
    ---------------------------------------------------------------------------------------
                  "OG_DT"."RT_CT_CD"="RT_SRT"."RT_CT_CD")
       4 - filter("RT_SRT"."SRT_ORD"> (SELECT MAX("RT_SRT"."SRT_ORD") FROM
                  "RT_SRT" "RT_SRT" WHERE "RT_CD"='BBB-'))
       6 - filter("RT_CD"='BBB-')
    
    Note
    -----
       - dynamic sampling used for this statement (level=2)
    
    30 rows selected.
    
    SQL> explain plan for
      2  select  distinct obj.*,
      3                   og_dt.*,
      4                   rt_srt.srt_ord,
      5                   rt_srt.rt_cd,
      6                   rt_srt.rt_ct_cd
      7    from  obj,
      8          og_dt,
      9          (
     10           select  t.*,
     11                   max(case rt_cd when 'BBB-' then srt_ord end) over() max_srt_ord
     12             from  rt_srt t
     13          ) rt_srt
     14    where obj.ob_id = og_dt.or_id
     15      and og_dt.rt_cd = rt_srt.rt_cd
     16      and og_dt.rt_ct_cd = rt_srt.rt_ct_cd
     17      and rt_srt.srt_ord > max_srt_ord
     18  /
    
    Explained.
    
    SQL> @?\rdbms\admin\utlxpls
    
    PLAN_TABLE_OUTPUT
    ---------------------------------------------------------------------------------------
    Plan hash value: 998396165
    
    --------------------------------------------------------------------------------
    | Id  | Operation             | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
    --------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT      |        |     7 |   476 |    11  (19)| 00:00:01 |
    |   1 |  HASH UNIQUE          |        |     7 |   476 |    11  (19)| 00:00:01 |
    |*  2 |   HASH JOIN           |        |     7 |   476 |    10  (10)| 00:00:01 |
    |*  3 |    HASH JOIN          |        |     7 |   238 |     7  (15)| 00:00:01 |
    |   4 |     TABLE ACCESS FULL | OBJ    |     6 |    78 |     3   (0)| 00:00:01 |
    |   5 |     TABLE ACCESS FULL | OG_DT  |     7 |   147 |     3   (0)| 00:00:01 |
    
    PLAN_TABLE_OUTPUT
    ---------------------------------------------------------------------------------------
    |*  6 |    VIEW               |        |     7 |   238 |     3   (0)| 00:00:01 |
    |   7 |     WINDOW BUFFER     |        |     7 |   147 |     3   (0)| 00:00:01 |
    |   8 |      TABLE ACCESS FULL| RT_SRT |     7 |   147 |     3   (0)| 00:00:01 |
    --------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       2 - access("OG_DT"."RT_CD"="RT_SRT"."RT_CD" AND
                  "OG_DT"."RT_CT_CD"="RT_SRT"."RT_CT_CD")
       3 - access("OBJ"."OB_ID"="OG_DT"."OR_ID")
    
    PLAN_TABLE_OUTPUT
    ---------------------------------------------------------------------------------------
       6 - filter("RT_SRT"."SRT_ORD">"MAX_SRT_ORD")
    
    Note
    -----
       - dynamic sampling used for this statement (level=2)
    
    27 rows selected.
    
    SQL> 
    

    SY.

    Published by: Solomon Yakobson on May 7, 2012 16:46

  • Better way to write this sql?

    Hi guru, I was able to get what I want, but I find there must be a better way/more efficient way to write this sql?

    Database: Oracle 11g

    This is the create for the test database statement:

    create table sample_test (prog_id number (9) DEFAULT 0 NOT NULL, chan_rights CHAR (2) DEFAULT ' ' NOT NULL)

    This is the insert statement:

    INSERT INTO sample_test (prog_id, chan_rights) VALUES (555633, 'A1')

    INSERT INTO sample_test (prog_id, chan_rights) VALUES (555633, 'A2')

    INSERT INTO sample_test (prog_id, chan_rights) VALUES (555633, 'A3')

    INSERT INTO sample_test (prog_id, chan_rights) VALUES (555633, 'A4')

    INSERT INTO sample_test (prog_id, chan_rights) VALUES (555633, 'A5')

    INSERT INTO sample_test (prog_id, chan_rights) VALUES (555633, 'A6')

    INSERT INTO sample_test (prog_id, chan_rights) VALUES (555633, 'A7')

    INSERT INTO sample_test (prog_id, chan_rights) VALUES (495641, 'A1')

    INSERT INTO sample_test (prog_id, chan_rights) VALUES (495641, 'A2')

    INSERT INTO sample_test (prog_id, chan_rights) VALUES (495641, 'A3')

    INSERT INTO sample_test (prog_id, chan_rights) VALUES (495641, 'B1')

    INSERT INTO sample_test (prog_id, chan_rights) VALUES (495641, 'B2')

    INSERT INTO sample_test (prog_id, chan_rights) VALUES (495641, 'B3')

    INSERT INTO sample_test (prog_id, chan_rights) VALUES (495641, 'B4')

    Here's what I did to get the data:

    Select distinct a.prog_id, rt_cnt, CASE

    WHEN a.rt_cnt = 7

    and there are (select 'Y' b sample_test where a.prog_id = b.prog_id and b.chan_rights = 'A1')

    and there are (select 'Y' b sample_test where a.prog_id = b.prog_id and b.chan_rights = "A2")

    and there are (select 'Y' b sample_test where a.prog_id = b.prog_id and b.chan_rights = "A3")

    and there are (select 'Y' b sample_test where a.prog_id = b.prog_id and b.chan_rights = 'A4')

    and there are (select 'Y' b sample_test where a.prog_id = b.prog_id and b.chan_rights = 'A5')

    and there are (select 'Y' b sample_test where a.prog_id = b.prog_id and b.chan_rights = 'A6')

    and there are (select 'Y' b sample_test where a.prog_id = b.prog_id and b.chan_rights = 'A7')

    THEN "A_ONLY".

    else 'SINGLE '.

    end CHAN_GROUP

    from (select prog_id, count (chan_rights) rt_cnt

    of sample_test

    Prog_id group) a, b sample_test

    where a.prog_id = b.prog_id

    That appears as follows:

    PROG_ID RT_CNT CHAN_GROUP
    4956417UNIQUE
    5556337A_ONLY

    As seen:

    1 / I count how many rights is available, and in this case, each program gets a "7"

    Set 2 / from these data, for each programme, I try to make sure it belongs to the company chan_rights right, for example, "A_ONLY". Therefore, as shown, Prog_ID 495641 does not contain "A_ONLY" channels listed in the case statement and there is unique. "A_ONLY" should only contain A1 to A7 inclusive and nothing else.

    Can I create a function that returns the value "Chan_Group", but is there a better way to rewrite the statement 'BOX' like a LOOP or something? I have millions of records to go through and someone told me that using "is" slows down the database so just thought that I could ask ahead...

    Please indicate if there is a better and more efficient method to get what I need?

    Thank you

    John

    I would do something like

    select prog_id,
          rt_cnt,
          (case when rt_cnt = 7 and num_a = 7
                then 'A_ONLY'
                else 'UNIQUE'
            end) chan_group
      from (select prog_id,
                  count(chan_rights) rt_cnt,
                  sum( case when chan_rights in ('A1','A2','A3','A4','A5','A6','A7')
                            then 1
                            else 0
                        end ) num_a
              from sample_test
            group by prog_id)
    

    View of inline, I count the number of values chan_rights, as well as the number that are in your list of A1 - A7.  In the outer query, I implement the logic that checks that the two charges are 7.

    Here is an example of sqlfiddle this http://www.sqlfiddle.com/#! 4/95438/2

    Justin

  • Looking for a better way to count the responses to the survey questions

    Hello all - I have created a survey for a site and now want to display the results to the admins of the site. I am trying to display the countdown - the number of times that a question has been answered in a certain way. I realize that I may need to build the investigation itself differently and how it stores data, but here is how it goes far.

    There are 10 questions, each with 4 options of radio button groups. The database table contains a column for each issue and stores the value of the selected option button. So it's pretty simple.

    Now on the results page, that it's the only way I can think to do, but there must be a better way. For each answer, I create a Recordset filtered on the issue and the value option, then display the total number of records. Which works great, but count each option, which means that 40 recordsets on the page - al simply determine "how many times was the Question 1, Option was selected and how many times has the Question 1, selected Option B and so on.»

    The only other thing I can think it maybe is a better way to count the occurrences of these values in the table is with PHP or in the SQL itself.  Or, perhaps, if the values themselves are entirely digital and follow a kind of model I can use a solution of math.

    Thoughts, solutions, and ideas are welcome!  Thank you.

    Your problem is a bad design. You should have a separate line for each answer, not to separate the columns. It also makes the very rigid investigation. A simple design would include a few tables - a master of the investigation that stores the questions and response table that contains a foreign key to the question number in the master, the answer, as well as any other details you need to capture. You might get more elaborate, but it's essential. Your result page and then just be a single recordset with a simple query with a group by clause.

    I urge you to review this before going any further.

  • Best way to write the Pl/Sql

    Hi all
    Can someone say best written below stored proc:

    procedure missing_authorized_services is
    v_truncate_sql varchar2 (200);
    v_sql varchar2 (2000);
    BEGIN
    v_truncate_sql: = "truncate table missing_authorized_services;
    immediately run v_truncate_sql;
    commit;

    v_sql: = "INSERT into missing_authorized_services select distinct trim (service_group_Cd) as service_group_Cd, trim (service_cd) as stage_1_mg_service_request service_cd
    where (service_group_cd, service_cd) not in)
    Select distinct service_group_cd, stage_3_servcd_servgrp_dim service_cd)';

    immediately run v_sql;
    commit;

    END missing_authorized_services;


    / * I do select the table and then try to insert into another table the result set * /.

    Please help,
    Thank you
    J

    Hello

    The best way to write the PL/SQL (or any code) is by small steps.
    Start with a very simple procedure that does something (anything), just enough to make sure it works.
    Add a lot of statements of output so you can see what made the procedure. Don't forget to delete them after that trial is over.

    For example:

    CREATE OR REPLACE procedure missing_authorized_services IS
            v_truncate_sql  VARCHAR2 (200);
    BEGIN
         v_truncate_sql := 'truncate table missing_authorized_services';
         dbms_output.put_line (  v_truncate_sql
                        || ' = v_truncate_sql inside missing_authorized_services'
                        );
    END      missing_authorized_services;
    

    If you get any errors (for example, ORA-00955, because you try to give the same name to a procedure that you already use for a table), then fix the error and try again.
    When he worls perfectly, then add another baby step. For example, you can add the line

    EXECUTE IMMEDIATE v_truncate_sql;
    

    and test again.

    Do not use SQL dynamic (EXECUTE IMMEDIATE) unless you have to.
    Is there a reason to use dynamic SQL for INSERT statements?

  • Is there a better way to remove the toolbar "Frequently used tools" (which I've never used!) to open whenever I open Acrobat reader. rather than uninstall Acrobat and use another PDF reader?

    Is there a better way to eliminate the "frequently used tools.
    toolbar (which I've never used!) to open whenever I open Acrobat
    drive. rather than uninstall Acrobat and use another PDF reader?

    Hi jg49392310,

    You can disable the tool pane with Adobe Acrobat Reader DC was last updated, see this note cover hide the tools Panel in Acrobat and Acrobat Reader DC at all times.

    Kind regards

    Nicos

  • Is there a better way to make the selection on this slider?

    Is there a better way to make the selection on this slider?
    I need to retrieve the test scores max (tesc_code SO1, S02, S03 etc... etc...)
    I get the results presented here, but I wonder if it's a better way to do this.
    The results should be back in the same cursor... e
    CURSOR c_sortest_SAT_scores(p_pidm IN saturn.sortest.sortest_pidm%TYPE,
    p_term in saradap.saradap_term_code_entry%TYPE)
    IS
    SELECT   s01.sortest_pidm       pidm_s01,
             s01.sortest_tesc_code  tesc_code_s01,
             s01.sortest_test_score score_s01,
             s02.sortest_pidm       pidm_s02,
             s02.sortest_tesc_code  tesc_code_s02,
             s02.sortest_test_score score_s02,
             s07.sortest_pidm       pidm_s07,
             s07.sortest_tesc_code  tesc_code_s07,
             s07.sortest_test_score score_s07,
             s08.sortest_pidm        pidm_s08,
             s08.sortest_tesc_code   tesc_code_s08,
             s08.sortest_test_score score_s08,
             s09.sortest_pidm        pidm_s09,
             s09.sortest_tesc_code   tesc_code_s09,
             s09.sortest_test_score  score_s09
      FROM   saturn.sortest s01,
             saturn.sortest s02,
             saturn.sortest s07,
             saturn.sortest s08,
             saturn.sortest s09
     WHERE       s01.sortest_tesc_code IN ('S01')
             AND s01.sortest_pidm = p_pidm
             AND s01.sortest_term_code_entry = p_term
             AND s01.sortest_test_score =
                   (SELECT   MAX (s01a.sortest_test_score)
                      FROM   saturn.sortest s01a
                     WHERE   S01.sortest_pidm = s01a.sortest_pidm
                             AND S01A.sortest_tesc_code IN ('S01'))
             AND s02.sortest_tesc_code IN ('S02')
             AND s02.sortest_pidm = p_pidm
             AND s02.sortest_term_code_entry = p_term
             AND s02.sortest_test_score =
                   (SELECT   MAX (S02A.sortest_test_score)
                      FROM   saturn.sortest s02a
                     WHERE   S02.sortest_pidm = s02a.sortest_pidm
                             AND S02A.sortest_tesc_code IN ('S02'))
             AND s07.sortest_tesc_code IN ('S07')
             AND s07.sortest_pidm = p_pidm 
             AND s07.sortest_term_code_entry = p_term
             AND s07.sortest_test_score =
                   (SELECT   MAX (S07A.sortest_test_score)
                      FROM   saturn.sortest S07A
                     WHERE   S07.sortest_pidm = S07A.sortest_pidm
                             AND S07A.sortest_tesc_code IN ('S07'))
             AND S08.sortest_tesc_code IN ('S08')
             AND S08.sortest_pidm = p_pidm 
             AND S08.sortest_term_code_entry = p_term
             AND S08.sortest_test_score =
                   (SELECT   MAX (S08A.sortest_test_score)
                      FROM   saturn.sortest S08A
                     WHERE   S08.sortest_pidm = S08A.sortest_pidm
                             AND S08A.sortest_tesc_code IN ('S08'))
                     AND S09.sortest_tesc_code IN ('S09')
             AND S09.sortest_pidm = p_pidm 
             AND S09.sortest_term_code_entry = p_term
             AND S09.sortest_test_score =
                   (SELECT   MAX (S09A.sortest_test_score)
                      FROM   saturn.sortest S09A
                     WHERE   S09.sortest_pidm = S09A.sortest_pidm
                             AND S09A.sortest_tesc_code IN ('S09'));

    Hello

    The problem is that you to act as a Cartesian product with all the tables (you will get: S01 * S02 * S08 * S09 lines!) Is it really what you want?
    I don't think...

    Wharton, you can do (with no Cartesian product) is:

    CURSOR c_sortest_SAT_scores(p_pidm IN saturn.sortest.sortest_pidm%TYPE,
    p_term in saradap.saradap_term_code_entry%TYPE)
    IS
    SELECT sortest_pidm pidm, sortest_tesc_code tesc_code,
           sortest_test_score score
      FROM sortest
     WHERE (sortest_tesc_code, sortest_test_score) IN (
              SELECT   sortest_tesc_code, MAX (sortest_test_score)
                  FROM sortest
                 WHERE sortest_tesc_code IN ('S01', 'S02', 'S07', 'S08', 'S09')
                   AND sortest_pidm = :p_pidm
                   AND sortest_term_code_entry = :p_term
              GROUP BY sortest_tesc_code)
       AND sortest_pidm = :p_pidm
       AND sortest_term_code_entry = :p_term
    

    However you absolutely need a Cartesian product, you can do:

    WITH allrows AS
         (SELECT sortest_pidm pidm, sortest_tesc_code tesc_code,
                 sortest_test_score score
            FROM sortest
           WHERE (sortest_tesc_code, sortest_test_score) IN (
                    SELECT   sortest_tesc_code, MAX (sortest_test_score)
                        FROM sortest
                       WHERE sortest_tesc_code IN
                                              ('S01', 'S02', 'S07', 'S08', 'S09')
                         AND sortest_pidm = :p_pidm
                         AND sortest_term_code_entry = :p_term
                    GROUP BY sortest_tesc_code)
             AND sortest_pidm = :p_pidm
             AND sortest_term_code_entry = :p_term)
    SELECT s01.pidm pidm_s01, s01.tesc_code tesc_code_s01, s01.score score_s01,
           s02.pidm pidm_s02, s02.tesc_code tesc_code_s02, s02.score score_s02,
           s07.pidm pidm_s07, s07.tesc_code tesc_code_s07, s07.score score_s07,
           s08.pidm pidm_s08, s08.tesc_code tesc_code_s08, s08.score score_s08,
           s09.pidm pidm_s09, s09.tesc_code tesc_code_s09, s09.score score_s09
      FROM allrows s01, allrows s02, allrows s07, allrows s08, allrows s09
     WHERE s01.tesc_code = 'S01'
       AND s02.tesc_code = 'S02'
       AND s07.tesc_code = 'S07'
       AND s08.tesc_code = 'S08'
       AND s09.tesc_code = 'S09'
    

    The lines will be stored in memory to a temporary table before that product happen (should be faster)...

  • Looking for a better way to write this SQL

    Oracle version 11R2
    Version of the OS (any)

    What I try to do is write a query that finds Public synonyms without a target object. I came up with this, but I think there is a better way.
    Select 
      s.owner, s.synonym_name, s.table_name, s.table_owner, s.db_link, InitCap(o.object_type) object_type
    from   
      sys.DBA_SYNONYMS s, sys.DBA_OBJECTS o
    where  
      s.synonym_name is not null
    and    
      s.table_owner = o.owner (+)
    and    
      s.table_name = o.object_name (+)
    and    
      s.owner = 'PUBLIC'
    and
      object_type is null;  
    object_type is null appears to be weak. It seems that the target object must be better.

    Your comments, observations, questions welcome.

    I don't know exactly what 'better' means in this context (faster, easier to read, etc.), but I tend to use a NOT EXISTS

    SELECT s.*
      FROM dba_synonyms s
     WHERE owner = 'PUBLIC'
       AND s.db_link IS NULL
       AND NOT EXISTS (
        SELECT 1
          FROM dba_objects o
         WHERE o.owner = s.table_owner
           AND o.object_name = s.table_name )
    

    I added the criteria DB_LINK to filter the public synonyms referring to objects in remote databases that obviously do not exist in the local DBA_OBJECTS.

    Justin

  • A better way to rearrange the sort in Pivot mode?

    Hello
    First question - is there a better way to do it?

    Background - have a simple pivot w / requirements to the Status column so that logic to not alphabetical company.
    Example, # Service req with status should be listed as affected, accepted, completed, closed, re-assigned.

    My solution has been to create a column iin table view, use a CASE to say one time statement accepted = 1, etc.. I then sorted CSA, hidden column in a table view and then rotate view I added the status column and hid the new CASE column. I get the results that I expected, but I was wondering if there was a better way to do it?

    Thanks - years

    As far as the presentation layer is concernd (i.e., in the responses) your way is the best I can think of. The only way you can sort a list of values outside the way that the system can determine automatically (for example, in alphabetical, chronological, order numerically, etc.) is to create your own definition. Without human intervention or custom rules, there is no way any system know that 'C' must come before the "S" (as in "Assigned" and "Accepted" or "O" should come before the 'L' as in 'Complete' and 'Closed').

    Because the system knows how to order numbers (e.g., 1,2,3, etc.), it makes sense to create your 'rule' (using your CASE statement) to assign each level of the State to 'correct' number in the sequence.

    That's what I'd do. The only addition I can add is that you can do to the RPD by adding a column 'command' to your table LOVs and expose that as a column in the presentation layer. Then you can use this column in sort order.

  • A better way to treat the form as a table with the variable column type?

    I wanted to make a column to return according to the status of a flag column as readonly. I have a column defined as follows in the report query.

    Decode (sys_flag, 'Y', APEX_ITEM. DISPLAY_AND_SAVE 3, TYPE_CODE, APEX_ITEM. SELECT_LIST_FROM_LOV (3, TYPE_CODE, 'LOV_TYPE_CODE_LOV')) AS TYPE_CODE

    To get the SRM process noting that column, I had to put this 'band of HTML code' to 'No', then the column attributes, I put:
    "Expression HTML" to "#TYPE_CODE #
    ' Display under "to"text display (saves the State).

    And then it took me to get new lines of work:
    "Default of Type" to "Expression of PL/SQL.
    'Default' to APEX_ITEM. SELECT_LIST_FROM_LOV (3, NVL(:P18_TYPE_CODE,'LOV_TYPE'), NULL, 'LOV_TYPE_CODE_LOV', ' NO')

    This makes large, however submit that it seems that the checksum of line used by MRU included the raw HTML generated by the APEX_ITEM package and not just the value of the column. I worked around this by including an element hidden checksum and creating a process that replaces the checksum generated automatically with my custom.

    Is there a better way to intercept this checksum, preferably before it is rendered on the page? I now hide the sum of hidden HTML control in the output HTML of another column, so it's a bit ugly. If not, is there an easier way to achieve what I'm looking for? I tried several combinations, and it seems to work better. It's just a shame there's no way to have a standard hidden column included in the SRM process.

    Hello

    I tried different possibilities and seems to run the following:

    Create a report questioning in NORMAL SQL function to help:

    SELECT
    APEX_ITEM.HIDDEN(1,EMPNO) || APEX_ITEM.TEXT(2,ENAME) ename,
    CASE WHEN DEPTNO = 10 THEN
     APEX_ITEM.HIDDEN(3, SAL) || SAL
    ELSE
     APEX_ITEM.TEXT(3, SAL)
    END SAL,
    EMPNO,
    DEPTNO || APEX_ITEM.MD5_CHECKSUM(ENAME, SAL) DETPNO
    FROM EMP
    

    Do not specify something special for column definitions.

    Create a button that submits the page.

    Create a PL/SQL process that runs "On submit - after calculations" and Validations, triggered by this button and using the following code:

    BEGIN
    APEX_ITEM.MULTI_ROW_UPDATE('#OWNER#:EMP:EMPNO,1:,|ENAME,2:SAL,3');
    END;
    

    I've done here: http://htmldb.oracle.com/pls/otn/f?p=55041:35

    Any element with the value of DEPTNO 10 has the value SAL, read-only, as otherwise it's editable. This is done by creating a hidden input field for the actual value, followed by the text version OR by creating a single entry field. This ensures that we have the correct number of SAL elements on the page.

    APEX_ITEM. MULTI_ROW_UPDATE is the "manual" version of the MRU that you would normally get with a form of table and works for above normal as sql based query reports. The format of this very strict is that you must always allow for two primary keys. See the text of presentation here: http://download.oracle.com/docs/cd/B28359_01/appdev.111/b32258/api.htm#CHDFDACC

    Andy

  • Is there a better way to generate the custom timed digital signals

    I'm trying to generate the digital output from the top and down with delays on different lines. Each daq assistant is activate single line on a port USB 6501. There more complex high and lows that I need to generate variable time difference between high and low. There is codebelow that does what I'm trying to achieve, but for a model executing high and low signal is much of your time to do it this way. I'm sure there is a better way to do it, I'm not an expert on labview so I only discovered its potential. Anyone can suggest a more effective and a quick way to do it. I would like to hgihly appreciate. Thank you!

    I've not shown in the code below, but using the DAQ assistant, I initialized lines at low logic level.


  • Best way to write the earpiece button code?

    Hello

    I'm trying to update several fields when the user clicks a button. I have a meter static val in a separate category that determines what value is displayed when you click a field. Why should I do the final BigVector? I thought I should be impossible to add elements once a var has been marked as final? Is there a better way to achieve what I'm doing?

    Here is my code, thank you--

    final BigVector bigStringVectorA = new BigVector();
    bigStringVectorA.addElement ("a response 1 Test");
    bigStringVectorA.addElement ("a 2 response Test");
    bigStringVectorA.addElement ("a response 3 Test");

    aAnswerOptionButton.setChangeListener (new FieldChangeListener() {}
    ' Public Sub fieldChanged (field field, int context) {}
    ++ Constants.counter;
    aAnswerOptionButton.setText ((String) bigStringVectorA.elementAt (Constants.counter));
    bAnswerOptionButton.setText ((String) bigStringVectorB.elementAt (Constants.counter));
    cAnswerOptionButton.setText ((String) bigStringVectorC.elementAt (Constants.counter));
    }
    });

    If you set bigStringVectorA as a class attribute you won't have to make it final.

  • How to write the select query for it

    Hello

    I had an html form and the area I drop down and he needs to select several values in the drop-down box. When I select multiple values then I have to write the query to SQL select statement.

    When I try to write the select statement and trying to run I get the error message.

    Select * from Table

    where emo_no = '1,2,3 '.

    That's how I write the query please suggest me how to write the query to select several values in the drop-down box.

    Thank you

    Use the keyword sql 'in '.  If you don't know how, I've heard good things about the book Teach Yourself SQL in 10 Minutes by Ben Forta.

  • Unit test: is there a way to make the dynamic query of the value of running after the boot process?

    I wonder why the dynamic value query is executed before the boot process? Logically, it makes sense to run after them.

    For example, I test a stored procedure that is supposed to delete a record, and I'd like to create this test report should be deleted as part of the startup process before execution of the stored procedure call to delete this test record. Apparently the dynamic query of value not returns not the test report in as long as the query parameter to call the stored procedure under test, which makes me think that is executed before the startup process of design...

    Please advise...

    Thank you

    Val

    As this thread does no traction/attention of the team of SQL Developer for a while, I had to submit a request for formal improvement on metalink:

    RE: 19834977 - IN THE UNIT TEST REQUEST TO ALLOW TO CHANGE THE ORDER OF EXECUTION OF THE START OF THE PROCESS

    Thank you

    Val

  • Problem with the simple query.

    Hi all

    I am facing problem with the query below

    Select A.COL1, A.COL2

    B.COL1, B.COL2

    FROM TABLE1 A

    TABLE 1 B

    WHERE A.header = '123'

    AND B.header = '123'

    AND nvl (A.COL6, 'ABC') = 'ABC '.

    AND NVL (B.COL6, 'DEF') = 'DEF '.

    Basically, my requiremenyt is: I have only one table, TABLE1 here, which has a line two lines (for the same header) as "ABC" and another is "DEF". Table 1 has two columns (col1, col2) that should be displayed for both lines.

    When the header has two records in table1 top query works. and but if I do not have a record for any header example there are a record for "abc" in col6 only. so my query above does not work because there is no record for 'DEF' in col6. But I want to again request to fecth the output (for b.col1 and b.col2 should have null values)

    could you pls suggest me how to get the 4 columns.

    Thanks in advance

    Kind regards

    UVA.

    Try to place the status of outer join on column: analytical_criterion_code as

    and nvl (AUDIT.analytical_criterion_code, 'AUDIT2') = 'verification2. '

    .

    .

    and nvl (TRANS.analytical_criterion_code, 'TRANS2') = 'TRANS2.

    In the sub query based on the opinions that you have given in post # 1, although there is no value "DEF * ' for col6 due to the condition of outer join on b.col6 (+) line is extracted with b.col [1,2,3] as NULL values. Try to remove the (+) sign b.col6 and test.

    with t as)

    Select 111 col1, col2 'aaa', 'ABC' col6 123 header of all the double union

    Select 222 'bbb', 'DEF' col6, 123 double header

    )

    q as (select 123 double header)

    Select A.COL1, A.COL2, A.COL6

    B.COL1, B.COL2, b.COL6

    q.header

    T a

    t b

    q

    where a.col6 (+) = 'ABC '.

    and b.col6 (+) = "DEF."

    and q.header = a.header (+)

    and q.header = b.header (+)

Maybe you are looking for

  • The user guide and the Support Page at launch?

    Depending on how you define a product launch (or how many different "spear" in how many different places) it's time to make the section and support pages available user guide?

  • Satellite A100-153 infrared configure or disable

    Hello everyone this is my first post here.I want to know how I can configure the ir port so that I can use it with other programs as the media player.Also I have a problem with listening, the remote control of my amp seems to share some codes with th

  • valve gear

    Hello people NOR wise. I am using the below attached text file to control a series of taps. After several tries, I get errors (200524, etc.) and when it runs the sequence is far away. I have an idea were I'm messing things up, but I need some ideas o

  • Can not find the drivers for laptop

    Hello! So I reinstalled windows 7 on my laptop, serial number 584037-001.Now I can't find a single driver for it online, except for a site that sells recovery .img files for like $10 a piece.Concidering that the laptop is like 2-3 years I have no int

  • No its 5515 acer with windows vista

    I was not ressemelable aqble this question if some has solution please pass meThank you