Please help on a query

Hello

We have two tables test_a and test_b and would like to get the result of this query:
COL1 VAL_A VAL_B
------------------------------ ------------------------------ -------------
code_1 aa_1 aa_1
code_1 aa_2 aa_2
code_1 aa_3
code_2 bb_1
code_2 bb_2


Could someone please help on this, thanks a lot!


create table test_a)
col1 varchar2 (30),
col2 varchar2 (30));

insert into values test_a ('code_1', 'aa_1');
insert into values test_a ('code_1', 'aa_2');
insert into values test_a ('code_1', 'aa_3');
insert into test_a values ('code_2', 'bb_1');
commit;

create table test_b)
col1 varchar2 (30),
col2 varchar2 (30));

insert into test_b values ('code_1', 'aa_1');
insert into test_b values ('code_1', 'aa_2');
insert into test_b values ('code_2', 'bb_2');
commit;

Select * from test_a;

COL1 COL2
------------------------------ ------------------------------
code_1 aa_1
code_1 aa_2
code_1 aa_3
code_2 bb_1

Select * from test_b;

COL1 COL2
------------------------------ ------------------------------
code_1 aa_1
code_1 aa_2
code_2 bb_2

Published by: user489948 on January 5, 2010 10:13

Published by: user489948 on January 5, 2010 10:15

Tags: Database

Similar Questions

  • Please, help me to query SQL Construct.


    Hi Experts,

    Could you please help me to query SQL Construct.  Please find the details

    HOSTNAME HOSTTYPE DEM R1 R2
    RS123 P ABC 24.5 265,5

    RS123 P CYC 24.5 265,5

    RS123 P ADDS 24.5 265,5

    RS123 P ADE 24.5 265,5

    RS123 P SRC 24.5 265,5

    EXPECTED RESULTS

    HOSTNAME      HOSTTYPE                              MNE                                                                      R1          R2
    RS123 P ABC, CYC, ADD, ADE, CBC 24.5 265,5

    Concerning

    See you soon

    with t as)

    Select "RS123' hostname 'P' hosttype, 'ABC' dem, 24.5 r1, r2 265,5 Union double all the

    Select 'RS123', 'P', "CYC", 24.5, 265,5 double Union all

    Select 'RS123', 'P', 'ADD', 24.5, 265,5 double Union all

    Select "RS123', 'P', 'ADE', 24.5, 265,5 double Union all

    Select 'RS123', 'P', 'SRC', 24.5, 265,5 double

    )

    Select the host name,

    HostType,

    RTrim (XMLAGG (XmlElement(e,MNE,','). (Extract ('//Text ()')), ',') DEM,.

    R1,

    R2

    t

    Group hostname,

    HostType,

    R1,

    R2

    /

    HOSTN H DEM R1 R2
    ----- - -------------------- ---------- ----------
    RS123 P ABC, CBC, ADE, ADD, CYC 24.5 265,5

    SQL >

    SY.

  • Please help with parallel query

    Hi all

    I am "playing" with a parallel query and try to see if it could improve some more long running queries, but can't do the database that you want to use a parallel execution plan, no matter what I do! I hope someone can point me in the right direction!

    ORACLE Version is 11.2.0.2
    OS Win 2008 R2 server
    UC = 32
    64 GB OF RAM
    AMM enabled, memory_target = M 50560
    SQL > show the parallel parameter

    VALUE OF TYPE NAME
    ------------------------------------ ----------- --------------
    fast_start_parallel_rollback string LOW
    parallel_adaptive_multi_user Boolean TRUE
    parallel_automatic_tuning boolean FALSE
    parallel_degree_limit string CPU
    parallel_degree_policy string AUTO
    parallel_execution_message_size integer 16384
    parallel_force_local boolean FALSE
    parallel_instance_group string
    parallel_io_cap_enabled boolean FALSE
    PARALLEL_MAX_SERVERS integer 985
    parallel_min_percent integer 0

    VALUE OF TYPE NAME
    ------------------------------------ ----------- --------------
    parallel_min_servers integer 16
    parallel_min_time_threshold channel 5
    parallel_server boolean FALSE
    parallel_server_instances integer 1
    parallel_servers_target integer 512
    parallel_threads_per_cpu integer 2
    recovery_parallelism integer 0
    I also ran the calibration of IO which resultet
    Max e/s per second 21569
    Max Mo / second 989
    I collected statistics of the system, the 1 hour time. the results are:
    Select pname, sys.aux_stats pval1 $;
    STATUS
    DSTART
    DSTOP
    FLAGS 0
    CPUSPEEDNW 915
    IOSEEKTIM 10
    IOTFRSPEED 4096
    SREADTIM 0.589
    MREADTIM 0.841
    CPUSPEED 1355
    MBRC 11
    MAXTHR 679936
    SLAVETHR
    I changed all my tables and indexes using 'ALTER TABLE xxx PARALLEL' then when I query the dba_tables, the DEGREE is DEFAULT for all objects invoked in my queries.

    what I've learned so far, I put all the necessary parameters.
    From my understanding, all queries who believe more than 5 seconds, should be tried to run in parallel (parallel_min_time_threshold = 5). But not a single query is doing at least this forced manually with a / * + PARALLEL * / tip! It drives me crazy. Choose manually a degree of 16 for example allows to speed up some queries from 15 minutes to 1 minute, but why ORACLE does not by itself?
    Given that it is a Siebel application, that we are talking about, there is no possibility of adding tips for SQL.

    example:

    This query took 29 seconds to complete, but was executed in SERIES
    SQL_ID, atzj0dmhshb23, number of children 0
    -------------------------------------
    SELECT T7. CONFLICT_ID, T7. LAST_UPD, T7. CREATED,
    T7. LAST_UPD_BY, T7. CREATED_BY, T7. MODIFICATION_NUM,
    T7. ROW_ID, T9. MAIN_PH_NUM, T9.NAME, T9. REGION,
    T9. X_SUB_REGION, T20. ATTRIB_44, T20. ATTRIB_26,
    T20. ATTRIB_45, T20. ATTRIB_27, T20. ATTRIB_03,
    T33. SUPPRESS_MAIL_FLG, T33. EMAIL_ADDR, T33. MID_NAME,
    T33. PR_DEPT_OU_ID, T33. LAST_NAME, T33. SEX_MF,
    T33. PR_PER_ADDR_ID, T33. PR_POSTN_ID, T30. PR_ADDR_ID,
    T33. HOME_PH_NUM, T33. OWNER_PER_ID, T33. WORK_PH_NUM,
    T33. FAX_PH_NUM, T33. FST_NAME, T20. ATTRIB_07,
    T3. INTEGRATION_ID, T33. PR_PER_PAY_PRFL_ID, T33. PRIV_FLG,
    T33. PR_MKT_SEG_ID, T33. PR_REP_SYS_FLG,
    T33. PR_REP_MANL_FLG, T33. PR_REP_DNRM_FLG, T33. PR_OPTY_ID,
    T33. PR_GRP_OU_ID, T33. EMP_FLG, T8. OWN_INST_ID,
    T8. INTEGRATION_ID, T33. PERSON_UID, T7. NAM

    Hash value of plan: 35208051

    ---------------------------------------------------------------------------------------------------------------------------------
    | ID | Operation | Name | Lines | Bytes | Cost (% CPU). Time |
    ---------------------------------------------------------------------------------------------------------------------------------
    | 0 | SELECT STATEMENT | 34 (100) |
    | 1. NESTED EXTERNAL LOOPS | 10. 42440 | 34 (0) | 00:00:01 |
    | 2. NESTED EXTERNAL LOOPS | 10. 42300 | 33 (0) | 00:00:01 |
    | 3. NESTED EXTERNAL LOOPS | 10. 42160 | 32 (0) | 00:00:01 |
    | 4. NESTED EXTERNAL LOOPS | 10. 42020 | 31 (0) | 00:00:01 |
    | 5. NESTED LOOPS | 10. 41880 | 30 (0) | 00:00:01 |
    | 6. NESTED EXTERNAL LOOPS | 11. 45947 | 29 (0) | 00:00:01 |

    | 7. NESTED LOOPS | 11. 45716 | 28 (0) | 00:00:01 |
    | 8. NESTED EXTERNAL LOOPS | 11. 45364 | 27 (0) | 00:00:01 |
    | 9. NESTED EXTERNAL LOOPS | 11. 45243 | 26 (0) | 00:00:01 |
    | 10. NESTED EXTERNAL LOOPS | 11. 45122 | 25 (0) | 00:00:01 |
    | 11. NESTED EXTERNAL LOOPS | 11. 43648 | 24 (0) | 00:00:01 |
    | 12. NESTED EXTERNAL LOOPS | 11. 37070 | 23 (0) | 00:00:01 |
    | 13. NESTED EXTERNAL LOOPS | 11. 34661 | 22 (0) | 00:00:01 |
    | 14. NESTED EXTERNAL LOOPS | 11. 34430 | 21 (0) | 00:00:01 |
    | 15. NESTED EXTERNAL LOOPS | 11. 33891 | 20 (0) | 00:00:01 |
    | 16. NESTED EXTERNAL LOOPS | 11. 33253 | 19 (0) | 00:00:01 |
    | 17. NESTED EXTERNAL LOOPS | 11. 32362 | 18 (0) | 00:00:01 |
    | 18. NESTED EXTERNAL LOOPS | 11. 31999 | 17 (0) | 00:00:01 |
    | 19. NESTED EXTERNAL LOOPS | 11. 29337 | 16 (0) | 00:00:01 |
    | 20. NESTED EXTERNAL LOOPS | 11. 28556 | 15 (0) | 00:00:01 |
    | 21. NESTED EXTERNAL LOOPS | 11. 28061 | 14 (0) | 00:00:01 |
    | 22. NESTED EXTERNAL LOOPS | 11. 26400 | 13 (0) | 00:00:01 |
    | 23. NESTED EXTERNAL LOOPS | 11. 26169 | 12 (0) | 00:00:01 |
    | 24. NESTED EXTERNAL LOOPS | 11. 25465 | 10 (0) | 00:00:01 |
    | 25. NESTED EXTERNAL LOOPS | 11. 21131. 9 (0) | 00:00:01 |
    | 26. NESTED EXTERNAL LOOPS | 11. 18326. 8 (0) | 00:00:01 |
    | 27. NESTED LOOPS | 11. 13651 | 7 (0) | 00:00:01 |
    | 28. NESTED EXTERNAL LOOPS | 11. 12452. 6 (0). 00:00:01 |
    | 29. NESTED EXTERNAL LOOPS | 11. 10978. 5 (0) | 00:00:01 |
    | 30. NESTED LOOPS | 11. 9504. 4 (0) | 00:00:01 |
    | 31. NESTED EXTERNAL LOOPS | 4. 360 | 3 (0) | 00:00:01 |
    | 32. NESTED LOOPS | 4. 228. 2 (0) | 00:00:01 |
    | * 33 | INDEX UNIQUE SCAN | S_PARTY_P1 | 1. 11. 1 (0) | 00:00:01 |
    | 34. TABLE ACCESS BY INDEX ROWID | S_CONTACT_BU | 4. 184. 1 (0) | 00:00:01 |
    | * 35 | INDEX RANGE SCAN | S_CONTACT_BU_M1 | 4 | | 1 (0) | 00:00:01 |
    | 36. TABLE ACCESS BY INDEX ROWID | S_ORG_EXT | 1. 33. 1 (0) | 00:00:01 |
    | * 37 | INDEX UNIQUE SCAN | S_ORG_EXT_U3 | 1 | | 1 (0) | 00:00:01 |
    | * 38 | TABLE ACCESS BY INDEX ROWID | S_CONTACT. 3. 2322 | 1 (0) | 00:00:01 |
    | * 39 | INDEX UNIQUE SCAN | S_CONTACT_P1 | 1 | | 1 (0) | 00:00:01 |
    | 40. TABLE ACCESS BY INDEX ROWID | S_MED_SPEC | 1. 134. 1 (0) | 00:00:01 |
    | * 41. INDEX UNIQUE SCAN | S_MED_SPEC_P1 | 1 | | 1 (0) | 00:00:01 |
    | 42. TABLE ACCESS BY INDEX ROWID | S_PRI_LST | 1. 134. 1 (0) | 00:00:01 |
    | * 43. INDEX UNIQUE SCAN | S_PRI_LST_P1 | 1 | | 1 (0) | 00:00:01 |
    | * 44 | TABLE ACCESS BY INDEX ROWID | S_PARTY | 1. 109. 1 (0) | 00:00:01 |
    | * 45 | INDEX UNIQUE SCAN | S_PARTY_P1 | 1 | | 1 (0) | 00:00:01 |
    | 46. TABLE ACCESS BY INDEX ROWID | S_CONTACT_SS | 1. 425. 1 (0) | 00:00:01 |
    | * 47 | INDEX RANGE SCAN | S_CONTACT_SS_U1 | 1 | | 1 (0) | 00:00:01 |
    | 48. TABLE ACCESS BY INDEX ROWID | S_CONTACT_LOYX | 1. 255. 1 (0) | 00:00:01 |
    | * 49 | INDEX RANGE SCAN | S_CONTACT_LOYX_U1 | 1 | | 1 (0) | 00:00:01 |
    | * 50 | INDEX RANGE SCAN | S_DQ_CON_KEY_U1 | 1. 394. 1 (0) | 00:00:01 |
    | * 51 | TABLE ACCESS FULL | S_CASE | 1. 64. 0 (0) |
    | 52. TABLE ACCESS BY INDEX ROWID | S_POSTN | 1. 21. 1 (0) | 00:00:01 |
    | * 53 | INDEX UNIQUE SCAN | S_POSTN_U2 | 1 | | 1 (0) | 00:00:01 |
    | 54. TABLE ACCESS BY INDEX ROWID | S_EMP_PER | 1. 151. 1 (0) | 00:00:01 |
    | * 55 | INDEX UNIQUE SCAN | S_EMP_PER_U1 | 1 | | 1 (0) | 00:00:01 |
    | 56. TABLE ACCESS BY INDEX ROWID | S_POSTN_CON | 1. 45. 1 (0) | 00:00:01 |
    | * 57 | INDEX RANGE SCAN | S_POSTN_CON_M3 | 4 | | 1 (0) | 00:00:01 |
    | 58. TABLE ACCESS BY INDEX ROWID | S_ORG_EXT_FNX | 1. 71. 1 (0) | 00:00:01 |
    | * 59 | INDEX RANGE SCAN | S_ORG_EXT_FNX_U1 | 1 | | 1 (0) | 00:00:01 |
    | 60. TABLE ACCESS BY INDEX ROWID | S_ORG_EXT_X | 1. 242. 1 (0) | 00:00:01 |
    | * 61. INDEX RANGE SCAN | S_ORG_EXT_X_U1 | 1 | | 1 (0) | 00:00:01 |
    | 62. TABLE ACCESS BY INDEX ROWID | S_CON_ADDR | 1. 33. 1 (0) | 00:00:01 |
    | * 63. INDEX RANGE SCAN | S_CON_ADDR_M51 | 1 | | 1 (0) | 00:00:01 |
    | 64. TABLE ACCESS BY INDEX ROWID | S_ADDR_PER | 1. 51 M | 1 (0) | 00:00:01 |
    | * 65 | INDEX UNIQUE SCAN | S_ADDR_PER_P1 | 1 | | 1 (0) | 00:00:01 |
    | 66. TABLE ACCESS BY INDEX ROWID | S_ADDR_PER | 1. 58. 1 (0) | 00:00:01 |
    | * 67. INDEX UNIQUE SCAN | S_ADDR_PER_P1 | 1 | | 1 (0) | 00:00:01 |
    | 68. TABLE ACCESS BY INDEX ROWID | S_ORG_EXT | 1. 49. 1 (0) | 00:00:01 |
    | * 69 | INDEX UNIQUE SCAN | S_ORG_EXT_U3 | 1 | | 1 (0) | 00:00:01 |
    | 70. TABLE ACCESS BY INDEX ROWID | S_POSTN | 1. 21. 1 (0) | 00:00:01 |
    | * 71 | INDEX UNIQUE SCAN | S_POSTN_U2 | 1 | | 1 (0) | 00:00:01 |
    | 72. TABLE ACCESS BY INDEX ROWID | S_ORG_EXT | 1. 219. 1 (0) | 00:00:01 |
    | * 73 | INDEX UNIQUE SCAN | S_ORG_EXT_U3 | 1 | | 1 (0) | 00:00:01 |
    | 74. TABLE ACCESS BY INDEX ROWID | S_ORG_EXT | 1. 598. 1 (0) | 00:00:01 |
    | * 75 | INDEX UNIQUE SCAN | S_ORG_EXT_U3 | 1 | | 1 (0) | 00:00:01 |
    | 76. TABLE ACCESS BY INDEX ROWID | S_CONTACT_X | 1. 134. 1 (0) | 00:00:01 |
    | * 77 | INDEX RANGE SCAN | S_CONTACT_X_U1 | 1 | | 1 (0) | 00:00:01 |
    | * 78 | INDEX UNIQUE SCAN | S_PARTY_P1 | 1. 11. 1 (0) | 00:00:01 |
    | * 79 | INDEX UNIQUE SCAN | S_PARTY_P1 | 1. 11. 1 (0) | 00:00:01 |
    | 50 M | TABLE ACCESS BY INDEX ROWID | S_POSTN_CON | 1. 32. 1 (0) | 00:00:01 |
    | * 81 | INDEX RANGE SCAN | S_POSTN_CON_M3 | 1 | | 1 (0) | 00:00:01 |
    | 82. TABLE ACCESS BY INDEX ROWID | S_POSTN | 1. 21. 1 (0) | 00:00:01 |
    | * 83 | INDEX UNIQUE SCAN | S_POSTN_U2 | 1 | | 1 (0) | 00:00:01 |
    | * 84 | INDEX UNIQUE SCAN | S_PARTY_P1 | 1. 11. 1 (0) | 00:00:01 |
    | 85. TABLE ACCESS BY INDEX ROWID | S_USER | 1. 14. 1 (0) | 00:00:01 |
    | * 86 | INDEX UNIQUE SCAN | S_USER_U2 | 1 | | 1 (0) | 00:00:01 |
    | 87. TABLE ACCESS BY INDEX ROWID | S_USER | 1. 14. 1 (0) | 00:00:01 |
    | * 88. INDEX UNIQUE SCAN | S_USER_U2 | 1 | | 1 (0) | 00:00:01 |
    | 89. TABLE ACCESS BY INDEX ROWID | S_USER | 1. 14. 1 (0) | 00:00:01 |
    | * 90 | INDEX UNIQUE SCAN | S_USER_U2 | 1 | | 1 (0) | 00:00:01 |
    | 91. TABLE ACCESS BY INDEX ROWID | S_USER | 1. 14. 1 (0) | 00:00:01 |
    | * 92 | INDEX UNIQUE SCAN | S_USER_U2 | 1 | | 1 (0) | 00:00:01 |
    ---------------------------------------------------------------------------------------------------------------------------------

    Information of predicates (identified by the operation identity card):
    ---------------------------------------------------

    33 - access("T15".") ROW_ID "(=:2)"
    35 - access("T1".") BU_ID "(=:2)"
    37 - access("T2".") PAR_ROW_ID "(=:2)"
    38 - filter ((NLS_UPPER ("LAST_NAME", '= "GENERIC_BASELETTER" nls_sort') AS
    NLS_UPPER(:3,'nls_sort=''GENERIC_BASELETTER''') AND 'T33 '. "PRIV_FLG"(='N')) "
    39 - access("T33".") ROW_ID '= 'T1'.' CONTACT_ID')
    41 - access("T33".") MED_SPEC_ID '= 'T5'.' ROW_ID")
    43 - access("T33".") CURR_PRI_LST_ID "="T18"." ROW_ID")
    44 - filter("T7".") PARTY_TYPE_CD' <>'Suspect')
    45 - access("T7".") ROW_ID "= 'T33'." PAR_ROW_ID')
    47 - access("T7".") ROW_ID "="T8"." PAR_ROW_ID')
    49 - access("T7".") ROW_ID "="T12"." PAR_ROW_ID')
    50 - access("T7".") ROW_ID "="T19"." CONTACT_ID')
    51 - filter("T7".") ROW_ID "= 'T25'." PR_SUBJECT_ID')
    53 - access("T33".") PR_POSTN_ID "="T21"." PAR_ROW_ID')
    55 - access("T7".") ROW_ID "="T23"." PAR_ROW_ID')
    57 - access("T30".") POSTN_ID ' =: 1 AND "T7".» ROW_ID "= 'T30'." CON_ID')
    59 - access("T33".") PR_DEPT_OU_ID '= 'T22'.' PAR_ROW_ID')
    61 - access("T33".") PR_DEPT_OU_ID "="T14"." PAR_ROW_ID')
    63 - access("T33".") PR_OU_ADDR_ID '= 'T11'.' ADDR_PER_ID' AND 'T33 '. "PR_DEPT_OU_ID"= "T11". ("' ACCNT_ID")
    65 - access("T33".") PR_PER_ADDR_ID "="T32"." ROW_ID")
    67 - access("T33".") PR_OU_ADDR_ID "="T17"." ROW_ID")
    69 - access("T33".") PR_DEPT_OU_ID '= 'T3'.' PAR_ROW_ID')
    71 - access("T3".") PR_POSTN_ID '= 'T31'.' PAR_ROW_ID')
    73 - access("T33".") PR_DEPT_OU_ID "="T9"." PAR_ROW_ID')
    75 - access("T33".") PR_DEPT_OU_ID '= 'T13'.' PAR_ROW_ID')
    77 - access("T7".") ROW_ID "="T20"." PAR_ROW_ID')
    78 - access("T33".") PR_DEPT_OU_ID '= 'T4'.' ROW_ID")
    79 - access("T33".") PR_SYNC_USER_ID '= 'T16'.' ROW_ID")
    81 - access("T33".") PR_POSTN_ID '= 'T29'.' POSTN_ID' AND 'T33 '. "ROW_ID"= 'T29'. ("' CON_ID")
    83 - access("T29".") POSTN_ID "="T6"." PAR_ROW_ID')
    84 - access("T29".") POSTN_ID "= 'T27'." ROW_ID")
    86 - access("T6".") PR_EMP_ID "="T26"." PAR_ROW_ID')
    88 - access("T21".") PR_EMP_ID '= 'T28'.' PAR_ROW_ID')
    90 - access("T31".") PR_EMP_ID '= 'T24'.' PAR_ROW_ID')
    92 - access("T33".") PR_SYNC_USER_ID '= 'T10'.' PAR_ROW_ID')

    Note
    -----
    -dynamic sample used for this survey (level = 5)
    -Automatic DOP: calculated degree of parallelism is 1 because of the parallel threshold
    -Profile SQL SYS_SQLPROF_013b617a8f0b005f used for this statement
    Looks like ORACLE considers all my questions with '1 second' which is the parallel threshold (5 seconds) and so works in series? Or am I completely wrong?


    (continued)

    Edited by: Penky 5 December 2012 09:37

    Penky wrote:
    Randolf,

    db_file_multiblock_read_count find not at all as far as I know, so it translates the default of 128 to 11 g. I read somewhere that it's not recommended to set it manually 10 or 11 and following.

    Thank you for the values. Which is recommended, fix, but still a lot together sites of value to something by default. I don't know yet where this MB_IO_COUNT = 8 comes, however.

    Furthermore, if you do want to play with the DOP Auto, you could just stick to the old manual DOP. If you set your PARALLEL_DEGREE_POLICY MANUAL, but have the objects marked as PARALLEL, you should get a PARALLEL query, it has provided is no less available to the optimizer serial plan.

    The default DOP is very susceptible to high (64 per node with your given configuration), you can set the PARALLEL degree to something lower.

    You could also play with ALTER SESSION FORCE PARALLEL QUERY PARALLEL x if you want / can limit this to specific sessions, then you have even to mark objects as PARALLEL, such that it could have side effects to other processes that you do not want to run in parallel.

    Randolf

  • Please help improve the query with the analytic function

    The mentioned below query takes about 10 hours to complete (10.2.0.4).

    There are 3 tables (table t has a relationship 1: n with table e and k table also has a relationship 1: n with table e).
    Table a contains 200,000 lines. (this table is truncated and inserted several times a week)
    E table contains rows of 1Mio.
    K table contains rows of 170Mio.

    drop table t;
    create table t
    (
       t_id number,
       constraint t_pk primary key (t_id)
    );
    
    drop table e;
    create table e
    (
       e_id number,
       e_doc nvarchar2(16),
       e_date date,
       constraint e_pk primary key (e_id)
    );
    
    drop table k;
    create table k (
       t_id number,
       e_id number
    );
    
    create unique index k_i1 on k(t_id, e_id);
    
    exec dbms_stats.gather_table_stats(user, 'T');
    exec dbms_stats.gather_table_stats(user, 'K');
    exec dbms_stats.gather_table_stats(user, 'E');
    
    
    
    -- Sample data:
    
    insert into t(t_id) values (100);
    insert into t(t_id) values (101);
    insert into t(t_id) values (102);
    insert into t(t_id) values (103);
    
    
    insert into e(e_id, e_doc, e_date) values (200, 'doc 200', to_date('01.01.2010', 'DD.MM.YYYY'));
    insert into e(e_id, e_doc, e_date) values (201, 'doc 201', to_date('02.01.2010', 'DD.MM.YYYY'));
    insert into e(e_id, e_doc, e_date) values (202, 'doc 202', to_date('03.01.2010', 'DD.MM.YYYY'));
    insert into e(e_id, e_doc, e_date) values (203, 'doc 203', to_date('04.01.2010', 'DD.MM.YYYY'));
    insert into e(e_id, e_doc, e_date) values (204, 'doc 204', to_date('05.01.2010', 'DD.MM.YYYY'));
    insert into e(e_id, e_doc, e_date) values (205, 'doc 205', to_date('06.01.2010', 'DD.MM.YYYY'));
    insert into e(e_id, e_doc, e_date) values (206, 'doc 206', to_date('07.01.2010', 'DD.MM.YYYY'));
    insert into e(e_id, e_doc, e_date) values (207, 'doc 207', to_date('08.01.2010', 'DD.MM.YYYY'));
    
    insert into k(t_id, e_id) values (100, 200);
    insert into k(t_id, e_id) values (100, 201);
    insert into k(t_id, e_id) values (100, 202);
    insert into k(t_id, e_id) values (100, 203);
    
    insert into k(t_id, e_id) values (101, 203);
    insert into k(t_id, e_id) values (101, 204);
    
    
    
    
    
    select k.t_id, e.e_date,  e.e_id, e.e_doc
    from   e, k, t
    where  k.e_id = e.e_id
    and    k.t_id = t.t_id
    order by k.t_id, e.e_date desc;
    
    
          T_ID E_DATE         E_ID E_DOC
    ---------- -------- ---------- ----------------
           100 04.01.10        203 doc 203
           100 03.01.10        202 doc 202
           100 02.01.10        201 doc 201
           100 01.01.10        200 doc 200
           101 05.01.10        204 doc 204
           101 04.01.10        203 doc 203
    I need a query that takes the latest 3 posts for a given t_id:
          T_ID E_DOC_LIST
    ---------- -----------------------
           100 doc 200/doc 201/doc 202
           101 doc 203/doc 204
    
    
    Sample query:
    
    select t_id, e_doc_list
       from (
       select  k.t_id,
            row_number() over(partition by k.t_id order by k.t_id, e.e_date desc) r_num, 
            rtrim(       lag(e.e_doc, 0) over(partition by k.t_id order by k.t_id, e.e_date) || 
                  '/' || lag(e.e_doc, 1) over(partition by k.t_id order by k.t_id, e.e_date) || 
                  '/' || lag(e.e_doc, 2) over(partition by k.t_id order by k.t_id, e.e_date), 
                  '/') e_doc_list
         from  e,
               k,
               t
         where  k.e_id = e.e_id
         and    k.t_id = t.t_id
         order by k.t_id, e.e_date desc
    ) where  r_num = 1   ;
    
    
          T_ID E_DOC_LIST
    ---------- --------------------------------------------------
           100 doc 203/doc 202/doc 201
           101 doc 204/doc 203
    The example query takes several hours in production.
    The r_num = 1 filter is applied quite late. Is there another way to generate the query or even review the tables.
    For the sample query:
    
    -----------------------------------------------------------------------------------------
    | Id  | Operation                        | Name | Rows  | Bytes | Cost (%CPU)| Time     |
    -----------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT                 |      |     6 |   468 |     6  (50)| 00:00:01 |
    |*  1 |  VIEW                            |      |     6 |   468 |     6  (50)| 00:00:01 |
    |*  2 |   WINDOW SORT PUSHED RANK        |      |     6 |   216 |     6  (50)| 00:00:01 |
    |   3 |    WINDOW SORT                   |      |     6 |   216 |     6  (50)| 00:00:01 |
    |   4 |     NESTED LOOPS                 |      |     6 |   216 |     4  (25)| 00:00:01 |
    |   5 |      MERGE JOIN                  |      |     6 |   198 |     4  (25)| 00:00:01 |
    |   6 |       TABLE ACCESS BY INDEX ROWID| E    |     8 |   208 |     2   (0)| 00:00:01 |
    |   7 |        INDEX FULL SCAN           | E_PK |     8 |       |     1   (0)| 00:00:01 |
    |*  8 |       SORT JOIN                  |      |     6 |    42 |     2  (50)| 00:00:01 |
    |   9 |        INDEX FULL SCAN           | K_I1 |     6 |    42 |     1   (0)| 00:00:01 |
    |* 10 |      INDEX UNIQUE SCAN           | T_PK |     1 |     3 |     0   (0)| 00:00:01 |
    -----------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       1 - filter("R_NUM"=1)
       2 - filter(ROW_NUMBER() OVER ( PARTITION BY "K"."T_ID" ORDER BY
                  "K"."T_ID",INTERNAL_FUNCTION("E"."E_DATE") DESC )<=1)
       8 - access("K"."E_ID"="E"."E_ID")
           filter("K"."E_ID"="E"."E_ID")
      10 - access("K"."T_ID"="T"."T_ID")
    
    
    and for query in production
    
    ---------------------------------------------------------------------------------------
    | Id  | Operation                 | Name         | Rows  | Bytes |TempSpc| Cost (%CPU)|
    ---------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT          |              |  3118K|   425M|       |   160K  (1)|
    |   1 |  VIEW                     |              |  3118K|   425M|       |   160K  (1)|
    |   2 |   SORT ORDER BY           |              |  3118K|   163M|   383M|   160K  (1)|
    |   3 |    WINDOW SORT PUSHED RANK|              |  3118K|   163M|   383M|   160K  (1)|
    |   4 |     WINDOW SORT           |              |  3118K|   163M|   383M|   160K  (1)|
    |   5 |      HASH JOIN            |              |  3118K|   163M|    40M| 33991   (1)|
    |   6 |       TABLE ACCESS FULL   | E            |  1053K|    28M|       |  4244   (1)|
    |   7 |       NESTED LOOPS        |              |  3118K|    80M|       | 21918   (1)|
    |   8 |        TABLE ACCESS FULL  | T            |   144K|  1829K|       |   282   (2)|
    |   9 |        INDEX RANGE SCAN   | K_I1         |    22 |   308 |       |     1   (0)|
    ---------------------------------------------------------------------------------------
    
     

    TimWong765 wrote:
    ...
    Table a contains 200,000 lines. (* this table is truncated and inserted several times a week *)

    You could be in one of the rare cases where the index should be rebuild, take a look in the following thread:
    http://asktom.Oracle.com/pls/asktom/f?p=100:11:0:P11_QUESTION_ID:6601312252730 #69571308712887 (search for 'index of Sweeper')
    Make sure that you have checked if you are in this case before going for an expensive index rebuild.

    Nicolas.

  • Help with the query to select only one record from the result set in double

    Hello

    Please help with the query. Version of Oracle database we use is 10g R2.

    I have a vision that is duplicated IDS, but they are used across the different functions. See below examples of data. Please help me with a query to select only one record (based on ID regardless of the area) from the bottom of the result set of duplicate records. For what is the point of view is there unique records, given the combination of the fields ID, Org, DF, dry, Sub-Sec

    ID
    Org
    DF
    Sec Sub-Sec

    (163)CQCPDMCPDMHD(163)PCENGENGENG(163)CQASICASICIS8888TSTACTACTAC(163)TSHEHESW6789CQINFOINFOFOS6789PCSECSYSSECSYSINFO16789TSSECSYSSECSYSINFO29009PCBMSBMSBMS1

    My result set must eliminate the duplicate identifiers regardless of whoever we choose of the result set. (I mean without distinction Org, DF, s, Sub-s). My expected result set should be.

    ID
    DSB

    DF
    SEC
    Sub-Sec
    (163)CQCPDMCPDMHD8888TSTACTACTAC6789CQINFOINFOFOS9009PCBMSBMSBMS1


    Thank you

    Orton

    Hello

    This sounds like a job for ROW_NUMBER:

    WITH got_r_num AS

    (

    SELECT id, DSB, df, s, sub_sec org

    ROW_NUMBER () OVER (PARTITION BY ID.

    ORDER BY org

    ) AS r_num

    OF view_x

    )

    SELECT id, DSB, df, sub_sec s,

    OF got_r_num

    WHERE r_num = 1

    ;

    He is a Top - N query example, where you choose the elements of N (N = 1 in this case) from the top of an ordered list.

    I hope that answers your question.
    If not, post a small example of data (CREATE TABLE and INSERT, only relevant columns instructions) to your sample data and the results desired from these data.  (I know that you said that you were a view selection.  Just for this thread, pretending it is a picture and post simple CREATE TABLE and INSERT statements to simulate your point of view).
    Point where the above query is to produce erroneous results, and explain, using specific examples, how you get the right results from data provided in these places.  (I didn't quite understand the explanation above.  I don't know why you want to

    ID ORG DF DRY SUB_SEC

    --      ---     --      ---     -------

    1234 CQ DPRK DPRK HD

    and is not

    1234 IS CQ ASIC, ASIC

    or

    TS 1234 IT IT SW

    or

    1234 CQ ASIC ASIC HD

    )
    If you change the query at all, post your modified version.
    Always say what version of Oracle you are using (for example, 11.2.0.2.0).

    See the FAQ forum: https://forums.oracle.com/message/9362002

  • I have some problem in my query to sql server. If I press f5 to run it opens to record dialogue area nd do not run (I don't get successful... orders etc). Please help me

    I have some problem in my query to sql server. If I press f5 to run it opens to record dialogue area nd do not run (I don't get successful... orders etc). Please help me

    Hello

    SQL Server is not supported in these forums. I suggest that you ask your question again in one of the forums dedicated to Microsoft for him here:

    http://social.technet.microsoft.com/Forums/en-us/category/sqlserver .

    Thank you.   :)

  • Please help me find the solution for the query

    Hi Experts,

    Please help build a sql query. Thank you

    Examples of data

    -------------------

    create the table Material_tb

    (

    Detail varchar2 (20).

    Description varchar2 (200)

    )

    /

    Start

    Insert into material_tb values('Color','Red,Blue,Black,Green,White');

    Insert into material_tb values ('Material','Gold, Silver, Platinum');

    end;

    /

    Select * from material_tb;

    DETAIL DESCRIPTION

    -------------------- ------------------------------

    Color red, blue, black, green, white

    Material gold, silver, Platinum

    I want that output voltage

    DETAIL DESCRIPTION

    -------------------- ------------------------------

    Red color

    Blue color

    Black color

    Green color

    White color

    Material gold

    Silver material

    Platinum material

    You can try under sql

    select distinct detail,regexp_substr(description,'[^,]+',1,LEVEL)
     from material_tb
    connect by regexp_substr(description,'[^,]+',1,LEVEL) is not null
    order by 1
    
  • Please help build a sql query

    Hello

    Please help build a sql query


    My Table Test2015 has given below

    Header_id Line_id Ordered_item       

    723887290 199925 MAIN1

    199925 723887291 MAIN2

    199926 723887292 SH-POS-NO-BR POS-INS

    199926 723887293 MAIN2

    199927 723887294 IC-ENV-NON-BR-ENV-PXY

    199927 723887295 MAIN1

    199927 723887297 MAIN2

    199927 723887298 PRCSS SH-FAIRY-ELEC DISTR.

    199927 723887299 SH-FAIRY-SUM PRO-DE-CONS-HOUSE

    I am trying to query my Test2015 table to obtain the records with ordered_item containing 'MAIN1' and 'MAIN2' only. I tried to write a query as below

    SELECT * FROM test2015 WHERE ORDERED_ITEM in ('MAIN1', 'MAIN2');

    But it gives me all the data with the MAIN2 records found but MAIN1 is absent, I want to retrieve only records to both 'MAIN1' and 'MAIN2' present for Header_id.

    While the result below shows me header_id - 199926 and 199929 that he should assume back. I want to fetch documents only with 'MAIN1' and 'MAIN2' both present.

    Header_id Line_id Ordered_item            

    723887290 199925 MAIN1

    199925 723887291 MAIN2

    199926 723887293 MAIN2

    199927 723887295 MAIN1

    199927 723887297 MAIN2

    199929 723887299 MAIN1

    Please suggest.

    Thank you and best regards,

    Prasad.

    Hello

    Try like this...

    SELECT * FROM test2015 WHERE ORDERED_ITEM in ('MAIN1") and in header_id (select test2015 WHERE ORDERED_ITEM in ('MAIN2') header_id)

  • Ask help - please help me get the expected defined in a query result

    Dear all,

    Help with your suggestions and advice. Please see the bottom of the screenshots for the planned result sets.

    create table shift_dt 
    (name varchar2(20),
    shift_start date,
    shift_end date);
    
    insert into shift_dt values ('Brian',to_date('01-01-2015 09:10:00','dd-mm-yyyy hh24:mi:ss'), to_date('01-01-2015 22:10:00','dd-mm-yyyy hh24:mi:ss'));
    insert into shift_dt values ('Brian',to_date('02-01-2015 09:10:00','dd-mm-yyyy hh24:mi:ss'), to_date('04-01-2015 00:00:00','dd-mm-yyyy hh24:mi:ss'));
    insert into shift_dt values ('Brian',to_date('04-01-2015 00:00:00','dd-mm-yyyy hh24:mi:ss'), to_date('05-01-2015 15:20:00','dd-mm-yyyy hh24:mi:ss'));
    insert into shift_dt values ('Brian',to_date('06-01-2015 10:00:00','dd-mm-yyyy hh24:mi:ss'), to_date('06-01-2015 23:20:00','dd-mm-yyyy hh24:mi:ss'));
    insert into shift_dt values ('Brian',to_date('07-01-2015 11:00:00','dd-mm-yyyy hh24:mi:ss'), to_date('09-01-2015 00:00:00','dd-mm-yyyy hh24:mi:ss'));
    insert into shift_dt values ('Brian',to_date('09-01-2015 00:00:00','dd-mm-yyyy hh24:mi:ss'), to_date('10-01-2015 22:00:00','dd-mm-yyyy hh24:mi:ss'));
    
    select * from shift_dt;
    
    
    

    Select * from shift_dt;

    1.jpg

    Expected result set: -.

    1.jpg

    I compare date of shift_end with the next date of shift_start of the day. If the dates made 12 am (IE 00:00:00), then I ignore and display the next shift end date available as displayed in the screen game result. Please see the screen set outcome turned for more details.

    Please help get the results you want in the SQL query

    Kind regards

    Souls

    Hello

    Here's one way:

    WITH got_new_grp AS

    (

    SELECT name, shift_start, shift_end

    CASE

    WHEN shift_start = TRUNC (shift_start)

    AND shift_start = LAG (shift_end) OVER (ORDER BY shift_start)

    THEN 0

    1. OTHER

    END AS new_grp

    OF shift_dt

    )

    got_grp AS

    (

    SELECT name, shift_start, shift_end

    SUM (new_grp) OVER (ORDER BY shift_start) AS grp

    OF got_new_grp

    )

    SELECT MIN (name) DUNGEON (DENSE_RANK FIRST ORDER BY shift_start)

    As a name

    MIN (shift_start) AS shift_start

    MAX (shift_end) AS shift_end

    OF got_grp

    GROUP BY grp

    ORDER BY shift_start

    ;

    Output (from the full sample data):

    NAME SHIFT_START SHIFT_END

    -------------------- ---------------- ----------------

    01/01/2015 Brian 09:10 01/01/2015 22:10

    Brian 02/01/2015 09:10 01/05/2015 15:20

    Brian 06/01/2015 10:00 01/06/2015 23:20

    Brian 07/01/2015 11:00 10/01/2015 22:00

    Brian 01/02/2015 15:00 02/02/2015 00:00

    Brian 02/02/2015 11:00 02/02/2015 17:00

    Brian 03/02/2015 00:00 03/02/2015 08:00

    Brian 02/05/2015 15:30 06/02/2015 07:30

    Brian 07/02/2015 23:30 10/02/2015 00:30

    I guess that shift_start is unique.

    It's finally a GROUP BY problem: we want to show the first shift_start and the last shfit_end of a group of 1 or more lines.  The tricky part is to identify the groups.  If I understand the problem, line X is grouped with the previous line X-1 if shift_start on the X line is midnight and is equal to shift_end on line X-1.  The above query uses the LAG analytic to see if each line begins a new group or not and the analytic function SUM to see how many groups have already begun (and therefore, which group each line entry belongs.)

    I still don't understand why you 'Corrected name of Anne in Brian' and not vice versa.  I guess the name to display in each group is the name of the first row in the Group (i.e. the line with the shift_start earlier).

  • I want to loop through the data from two different tables using for loop where the query should be replaced at runtime, please help me

    I have the data into two table with the structure of similar column, I want to loop through the data in these two tables

    based on some condition and runtime that I want to put the query in loop for example, the example is given, please help me

    create table ab (a number, b varchar2 (20));

    Insert into ab

    Select rownum, rownum. "" sample "

    of the double

    connect by level < = 10

    create table bc (a number, b varchar2 (20));

    Insert into BC.

    Select rownum + 1, rownum + 1 | "" sample "

    of the double

    connect by level < = 10

    declare

    l_statement varchar2 (2000);

    Boolean bool;

    Start

    bool: = true;

    If it is true, then

    l_statement: =' select * ab ';

    on the other

    l_statement: =' select * from bc';

    end if

    I'm in execute immediate l_statement - something like that, but I don't know

    loop

    dbms_output.put_line (i.a);

    end loop;

    end;

    Something like that, but this isn't a peace of the code work.

    Try this and adapt according to your needs:

    declare

    l_statement varchar2 (2000);

    c SYS_REFCURSOR;

    l_a number;

    l_b varchar2 (20);

    Boolean bool;

    Start

    bool: = true;

    If it is true, then

    l_statement: = "select a, b, AB;

    on the other

    l_statement: = "select a, b from bc;

    end if;

    --

    Open c for l_statement;

    --

    loop

    extract the c in l_a, l_b;

    When the output c % notfound;

    dbms_output.put_line (l_a |') -' || l_b);

    end loop;

    close c;

    end;

    /

  • Query XML giving NULL result - please help!

    Hello

    I am a newbie in XML and I try to extract the XMLType column information.
    I am using Oracle 11g

    I use the table is

    CREATE TABLE 'PRODUCTS '.
    (
    'ID' VARCHAR2 (10 BYTE),
    "CUSTDOC" 'SYS '. "" XMLTYPE.
    )
    _________________________________________________________________

    I am the slot loading XML file in the CUSTDOC column

    < report xmlns = "http://developer.cognos.com/schemas/report/6.0/."
    expressionLocale = "en - to the" >
    [< modelPath > / content/folder[@name='Packages']/folder[@name='Prod']/package[@name='ORGS']/model[@name='model'] < / modelPath >
    < drillBehavior modelBasedDrillThru = "true" / >
    < query >
    < application name = "Query1" >
    < source >
    < model / >
    < / source >
    < selection >
    < name of dataItem = aggregate 'Incident ID' = 'none' rollupAggregate = 'none' >
    < expression > [ABC]. [XYZ]. [Incident] < / expression >
    < / dataItem >
    < / selection >
    < detailFilters >
    < detailFilter >
    < filterExpression > [ABC]. [XYZ]. [Company] = "SOCIÉTÉTEST" < / filterExpression >
    < / detailFilter >
    < / detailFilters >
    < / query >
    < / queries >
    < layouts >
    < layout >
    < reportPages >
    < name = 'Page1' page >
    < style >
    < defaultStyles >
    < defaultStyle = "pg" refStyle / >
    < / defaultStyles >
    < / style >
    < pageBody >
    < style >
    < defaultStyles >
    < defaultStyle = "pb" refStyle / >
    < / defaultStyles >
    < / style >
    < content >
    < refQuery list = "Query1" horizontalPagination = "true" name = "List1" >
    < style >
    < defaultStyles >
    < defaultStyle = 'ls' refStyle / >
    < / defaultStyles >
    < value CSS = "border-collapse: collapse" / >
    < / style >
    < more >
    < listColumn >
    < listColumnTitle >
    < style >
    < defaultStyles >
    < defaultStyle = 'lt' refStyle / >
    < / defaultStyles >
    < / style >
    < content >
    < textItem >
    < dataSource >
    < dataItemLabel refDataItem = "The Incident ID" / >
    < / dataSource >
    < / textItem >
    < / Summary >
    < / listColumnTitle >
    < listColumnBody >
    < style >
    < defaultStyles >
    < defaultStyle = 'lc' refStyle / >
    < / defaultStyles >
    < / style >
    < content >
    < textItem >
    < dataSource >
    < dataItemValue refDataItem = "The Incident ID" / >
    < / dataSource >
    < / textItem >
    < / Summary >
    < / listColumnBody >
    < / listColumn >
    < / more >
    < / list >
    < / Summary >
    < / pageBody >
    < Entetepage >
    < content >
    < block >
    < style >
    < defaultStyles >
    < defaultStyle = 'ta' refStyle / >
    < / defaultStyles >
    < / style >
    < content >
    < textItem >
    < style >
    < defaultStyles >
    < defaultStyle = "tt" refStyle / >
    < / defaultStyles >
    < / style >
    < dataSource >
    < staticValue / >
    < / dataSource >
    < / textItem >
    < / Summary >
    < / block >
    < / Summary >
    < style >
    < defaultStyles >
    < defaultStyle = "ph" refStyle / >
    < / defaultStyles >
    < value = CSS "padding-bottom: 10px" / >
    < / style >
    < / pageHeader >
    < pageFooter >
    < content >
    < table >
    < tableRows >
    < tableRow >
    < tableCells >
    < tableCell >
    < content >
    < date >
    < style >
    < dataFormat >
    < dateFormat / >
    < / dataFormat >
    < / style >
    < / date >
    < / Summary >
    < style >
    < value="vertical-align:top;text-align:left;width:25%"/ CSS >
    < / style >
    < / tableCell >
    < tableCell >
    < content >
    < pageNumber / >
    < / Summary >
    < style >
    < value="vertical-align:top;text-align:center;width:50%"/ CSS >
    < / style >
    < / tableCell >
    < tableCell >
    < content >
    < time >
    < style >
    < dataFormat >
    < timeFormat / >
    < / dataFormat >
    < / style >
    < / time >
    < / Summary >
    < style >
    < value="vertical-align:top;text-align:right;width:25%"/ CSS >
    < / style >
    < / tableCell >
    < / tableCells >
    < / tableRow >
    < / TableRow >
    < style >
    < defaultStyles >
    < defaultStyle = "CT" refStyle / >
    < / defaultStyles >
    < value="border-collapse:collapse;width:100%"/ CSS >
    < / style >
    < /table >
    < / Summary >
    < style >
    < defaultStyles >
    < defaultStyle = "pf" refStyle / >
    < / defaultStyles >
    < value = CSS "padding-top: 10px" / >
    < / style >
    < / pageFooter >
    < / print this page >
    < / reportPages >
    < / page layout >
    < / page layout >
    < XMLAttributes >
    < name XMLAttribute = "RS_CreateExtendedDataItems" value = "true" output = "no" / >
    < name XMLAttribute = "listSeparator' value = ',' output ="no"/ >
    < / XMLAttributes >
    < / report >

    ____________________________________________________________________________________

    I run the query below

    SELECT SYS. XMLTYPE. GETCLOBVAL (SYS. XMLTYPE. Extract ("CUSTDOC",'/ Report/Queries/Query/Selection/DataItem/expression / Text () ')) 'CUSTDOC' OF 'PRODUCT '.

    and I await the result below

    CUSTDOC
    ---
    [ABC]. [XYZ]. [Incident]

    but I am NOTHING.

    Please help me. Don't know what I'm doing wrong here.

    Kind regards
    Ravi

    Hi Ravi,

    Don't know what I'm doing wrong here.

    Two things:

    -EXTRACT function is deprecated--> use XMLQuery or XMLTable rather
    -You are missing the namespace declaration

    See if it works for you:

    SQL> SELECT XMLCast(
      2           XMLQuery(
      3             'declare default element namespace "http://developer.cognos.com/schemas/report/6.0/"; (: :)
      4              /report/queries/query/selection/dataItem/expression'
      5             passing t.custdoc
      6             returning content
      7           )
      8           as varchar2(30)
      9         ) as expr
     10  FROM product t ;
    
    EXPR
    ------------------------------
    [ABC].[XYZ].[Incident]
     
    
  • SQL QUERY PLEASE HELP

    My requirement is to calculate a few things like total_cash_amount, TOTAL_CO_PATIENT_AMOUNT etc... For this we will use BILLING. PATIENTBILL, BILLING. PATIENTBILLDETAILS, BILLING. Operating tables. Here it is already developed procedure.
    I'm askd to change the same for best performance. This is the part of it that is calculation of total_cash_amount. There will be two categories such as INPATIENT (IP) and OUTPATIENT (OP). Can anyone of you please help me to change the code in a manner better. And can you please suggest that global temporary tables are better or can we use the tables directly.
    INSERT INTO GTT_PATIENTBILL
    SELECT *
      FROM BILLING.PATIENTBILL P
     WHERE P.LOCATIONID = IN_LOCATIONID
       AND TRUNC(P.BILLDATE) BETWEEN TRUNC(IN_YEARDATE) AND TRUNC(IN_FROMDATE);
    INSERT INTO GTT_PATIENTBILLDETAILS   
    SELECT PB.*
      FROM BILLING.PATIENTBILL P, BILLING.PATIENTBILLDETAILS PB
     WHERE P.LOCATIONID = IN_LOCATIONID
       AND P.BILLNO = PB.BILLNO
       AND TRUNC(P.BILLDATE) BETWEEN TRUNC(IN_YEARDATE) AND TRUNC(IN_FROMDATE);
    INSERT INTO GTT_TRANSACTION
    SELECT *
      FROM BILLING.TRANSACTION T
     WHERE T.LOCATIONID = IN_LOCATIONID
       AND TRUNC(T.CREATEDDATE) BETWEEN TRUNC(IN_YEARDATE) AND
           TRUNC(IN_FROMDATE);
    
    DELETE FROM TEMP_1;
    INSERT INTO TEMP_1
    
      SELECT (CASE
               WHEN PARTICULARS IS NULL AND FLG = 1 THEN
                'TOTAL COLLECTIONS'
               WHEN PARTICULARS IS NULL AND FLG = 2 THEN
                'TOTAL DEDUCTIONS'
               ELSE
                PARTICULARS
             END) PARTICULARS,
             SUM(A) "FOR THE DAY",
             SUM(B) "MONTH TO DATE",
             SUM(C) "YEAR TO DATE"
        FROM (SELECT PARTICULARS,
                     SUM(TOTAL_CASH_AMOUNT_DAY) A,
                     SUM(TOTAL_CASH_AMOUNT_MONTH) B,
                     SUM(TOTAL_CASH_AMOUNT_YEAR) C,
                     1 FLG
                FROM (SELECT 'OP CASH' PARTICULARS,
                             SUM(P.TOTALSERVICEAMOUNT) TOTAL_CASH_AMOUNT_DAY,
                             0 TOTAL_CASH_AMOUNT_MONTH,
                             0 TOTAL_CASH_AMOUNT_YEAR
                        FROM GTT_PATIENTBILL P--BILLING.PATIENTBILL P, BILLING.PATIENTBILLDETAILS PB
                       WHERE P.LOCATIONID = IN_LOCATIONID
                         --AND P.BILLNO = PB.BILLNO
                         AND P.BILLINGTYPEID = 1
                         AND P.PATIENTSERVICE = 2
                         AND TRUNC(P.BILLDATE) =TRUNC(IN_FROMDATE)
                      UNION ALL
                      SELECT 'OP CASH' PARTICULARS,
                             0,
                             SUM(P.TOTALSERVICEAMOUNT) TOTAL_CASH_AMOUNT_MONTH,
                             0
                        FROM GTT_PATIENTBILL P--BILLING.PATIENTBILL P, BILLING.PATIENTBILLDETAILS PB
                       WHERE P.LOCATIONID = IN_LOCATIONID
                         --AND P.BILLNO = PB.BILLNO
                         AND P.BILLINGTYPEID = 1
                         AND P.PATIENTSERVICE = 2
                         AND TRUNC(P.BILLDATE) BETWEEN TRUNC(IN_MONTHDATE) AND
                             TRUNC(IN_FROMDATE)
                      UNION ALL
                      SELECT 'OP CASH' PARTICULARS,
                             0,
                             0,
                             SUM(P.TOTALSERVICEAMOUNT) TOTAL_CASH_AMOUNT_YEAR
                        FROM GTT_PATIENTBILL P--BILLING.PATIENTBILL P, BILLING.PATIENTBILLDETAILS PB
                       WHERE P.LOCATIONID = IN_LOCATIONID
                         --AND P.BILLNO = PB.BILLNO
                         AND P.BILLINGTYPEID = 1
                         AND P.PATIENTSERVICE = 2
                         AND TRUNC(P.BILLDATE) BETWEEN TRUNC(IN_YEARDATE) AND
                            TRUNC(IN_FROMDATE)
                      UNION ALL
                      SELECT 'IP CASH' PARTICULARS,
                             SUM(P.TOTALSERVICEAMOUNT) TOTAL_CASH_AMOUNT_DAY,
                             0 TOTAL_CASH_AMOUNT_MONTH,
                             0 TOTAL_CASH_AMOUNT_YEAR
                        FROM GTT_PATIENTBILL P--BILLING.PATIENTBILL P, BILLING.PATIENTBILLDETAILS PB
                       WHERE P.LOCATIONID = IN_LOCATIONID
                         --AND P.BILLNO = PB.BILLNO
                         AND P.BILLINGTYPEID = 1
                         AND P.PATIENTSERVICE = 3
                         AND TRUNC(P.BILLDATE) = TRUNC(IN_FROMDATE)
                      UNION ALL
                      SELECT 'IP CASH' PARTICULARS,
                             0,
                             SUM(P.TOTALSERVICEAMOUNT) TOTAL_CASH_AMOUNT_MONTH,
                             0
                        FROM GTT_PATIENTBILL P--BILLING.PATIENTBILL P, BILLING.PATIENTBILLDETAILS PB
                       WHERE P.LOCATIONID = IN_LOCATIONID
                         --AND P.BILLNO = PB.BILLNO
                         AND P.BILLINGTYPEID = 1
                         AND P.PATIENTSERVICE = 3
                         AND TRUNC(P.BILLDATE)  BETWEEN TRUNC(IN_MONTHDATE) AND
                             TRUNC(IN_FROMDATE)
                      UNION ALL
                      SELECT 'IP CASH' PARTICULARS,
                             0,
                             0,
                             SUM(P.TOTALSERVICEAMOUNT) TOTAL_CASH_AMOUNT_YEAR
                        FROM GTT_PATIENTBILL P--BILLING.PATIENTBILL P, BILLING.PATIENTBILLDETAILS PB
                       WHERE P.LOCATIONID = IN_LOCATIONID
                         --AND P.BILLNO = PB.BILLNO
                         AND P.BILLINGTYPEID = 1
                         AND P.PATIENTSERVICE = 3
                         AND TRUNC(P.BILLDATE) BETWEEN TRUNC(IN_YEARDATE) AND
                             TRUNC(IN_FROMDATE)) T1
               GROUP BY PARTICULARS
               
    Thanks in advance...

    You can do this in the same query. As long as the underlying table is the same thing that you don't have to have multiple queries to the same table. You just improvise on your selection fields. Use a CASE statement instead of DECIDING that I used and include the other INDICATIONS which I didn't understand.

  • need help with query-please help

    Hi all
    I want to convert the query to a view online below. I write examples of data that I used
    create table pde(empno number(10),act_hour varchar(10 byte));
     
    Table created
     
    insert into pde values(211,'12:20');
    insert into pde values(211,'06:00');
    insert into pde values(213,'07:00');
    insert into pde values(213,'07:20');
     
    WITH  got_total_hours     AS
    (
         SELECT SUM (   TO_NUMBER (SUBSTR (act_hour, 1, 2))
                    + ( TO_NUMBER (SUBSTR (act_hour, 4, 2))
                      / 60
                      )
                    ) AS total_hours  
         from      pde
     
    )
    SELECT        TO_CHAR (FLOOR (total_hours))
           || ':'
           || TO_CHAR ( MOD (total_hours, 1) * 60
                  , 'fm00'
                   )     AS hh_mm,empno,act_hour
    FROM     got_total_hours ,pde
     
    this is the output i am getting  
     
    HH_MM  EMPNO  ACT_HOUR
    32:40     211     12:20
    32:40     211     06:00
    32:40     213     07:00
    32:40     213     07:20
    
    i tried changing the above query to an inline view given below
    SELECT        TO_CHAR (FLOOR (total_hours))
           || ':'
           || TO_CHAR ( MOD (total_hours, 1) * 60
                  , 'fm00'
                   )     AS hh_mm,act_hour from(SELECT SUM (   TO_NUMBER (SUBSTR (act_hour, 1, 2))
                    + ( TO_NUMBER (SUBSTR (act_hour, 4, 2))
                      / 60
                      )
                    ) AS total_hours  
         from      pde)
    
    ORA-00904:''ACT_HOUR":invalid identifier
    Please help me
    thanking in advance
    Kind regards
    oracleuser

    Published by: makdutakdu on January 5, 2010 11:41

    Just push "with the clause" within the main request and give an alias of the table for him... something like

     SELECT TO_CHAR (FLOOR (total_hours))
      || ':'
      || TO_CHAR ( MOD (total_hours, 1) * 60 , 'fm00' ) AS hh_mm,
      empno                                                     ,
      act_hour
       FROM
      (SELECT SUM ( TO_NUMBER (SUBSTR (act_hour, 1, 2)) + ( TO_NUMBER (SUBSTR (act_hour, 4, 2)) / 60 ) ) AS total_hours
         FROM pde
      ) got_total_hours ,
      pde
    

    Ravi Kumar

  • Please help me write this SQL query...

    Hi everyone,
    
    Please help me in this query.
    A patient can multiple types of Adresses (types P,M,D).If they have all the 3 types i need to select type: p and
    if they have (M and D) i need to select type M,and if they have only type D i have to select that.
    For each address i need to validate whether that particular address is valid or not (by start date and end date and valid flag)
    
    Patient table
    =============
    
    Patient_id          First_name    last_name
    
    1                   sanjay        kumar
    2                   ajay          singh
    3                   Mike          John
    
    
    Adress table
    ============
    
    address_id       patient_id       adresss       city       type       startdate        enddate      valid_flg
    
    1                   1             6222         dsadsa           P          01/01/2007       01/01/2010
    2                   1             63333        dsad             M          01/02/2006       01/01/2007      N
    3                   1             64564         fdf              M          01/01/2008       07/01/2009      
    4                   1             654757       fsdfsa          D          01/02/2008       09/10/2009  
    5                   2             fsdfsd       fsdfsd            M          01/03/2007       09/10/2009   
    6                   2             jhkjk        dsad              D          01/01/2007       10/10/2010   
    7                   3             asfd         sfds               D          01/02/2008       10/10/2009      
    
    
    output
    =====
    
    1        sanjay       kumar            6222       dsadsa      P          01/01/2007        01/01/2010     
    2        ajay         singh            fsdfsd     fsdfsd       M          01/03/2007        09/10/2009 
    3        mike         john              asfd       sfds        D          01/02/2008        10/10/2009
    Thanks in advance
    Phani

    Hello, Fabienne,.

    This race for you (twisted code of Sarma):

    SELECT patient_id, first_name, last_name, address, city, type, startdate, enddate
     FROM (
      SELECT a.patient_id patient_id, first_name, last_name, address, city, type, startdate, enddate,
                 ROW_NUMBER() OVER (PARTITION BY p.patient_id ORDER BY CASE type WHEN 'P' THEN 1
                                                                          WHEN  'M' THEN 2
                                                                          WHEN  'D' THEN 3
                                                                        END) rn
        FROM  patient p
        JOIN  address a ON (p.patient_id = a.patient_id )
       WHERE NVL(valid_flg, 'X') != 'N'
         AND SYSDATE BETWEEN startdate AND  NVL(enddate, SYSDATE)
         )
    WHERE rn = 1; 
    

    Edit, currently in the trial:

    With Patient AS (
    SELECT 1  Patient_id , 'sanjay' First_name, 'kumar'  last_name FROM DUAL UNION ALL
    SELECT 2, 'ajay', 'singh' FROM DUAL UNION ALL
    SELECT 3, 'Mike', 'John' FROM DUAL),
    Address AS (
    SELECT 1   address_id, 1  patient_id, '6222'    address, 'dsadsa'   city, 'P'  type, to_date('01/01/2007', 'DD/MM/YYYY')  startdate, to_date('01/01/2010', 'DD/MM/YYYY')  enddate, NULL  valid_flg FROM DUAL UNION ALL
    SELECT 2,1,'63333','dsad','M', to_date('01/02/2006', 'DD/MM/YYYY'), to_date('01/01/2007', 'DD/MM/YYYY'),  ' N'  FROM DUAL UNION ALL
    SELECT 3,1,'64564','fdf','M', to_date('01/01/2008', 'DD/MM/YYYY'), to_date('07/01/2009', 'DD/MM/YYYY'), NULL  FROM DUAL UNION ALL
    SELECT 4,1,'654757','fsdfsa','D', to_date('01/02/2008', 'DD/MM/YYYY'), to_date('09/10/2009', 'DD/MM/YYYY'),  NULL  FROM DUAL UNION ALL
    SELECT 5,2,'fsdfsd ','fsdfsd','M', to_date('01/03/2007', 'DD/MM/YYYY'), to_date('09/10/2009', 'DD/MM/YYYY'), NULL  FROM DUAL UNION ALL
    SELECT 6,2,' jhkjk','dsad','D', to_date('01/01/2007', 'DD/MM/YYYY'), to_date('10/10/2010', 'DD/MM/YYYY'),  NULL  FROM DUAL UNION ALL
    SELECT 7,3,'asfd',' sfds',' D', to_date('01/02/2008', 'DD/MM/YYYY'), to_date('10/10/2009', 'DD/MM/YYYY'),  NULL  FROM DUAL)
    -- end test data
     SELECT patient_id, first_name, last_name, address, city, type, startdate, enddate
     FROM (
      SELECT a.patient_id patient_id, first_name, last_name, address, city, type, startdate, enddate,
                 ROW_NUMBER() OVER (PARTITION BY p.patient_id ORDER BY CASE type WHEN 'P' THEN 1
                                                                          WHEN  'M' THEN 2
                                                                          WHEN  'D' THEN 3
                                                                        END) rn
        FROM  patient p
        JOIN  address a ON (p.patient_id = a.patient_id )
       WHERE NVL(valid_flg, 'X') != 'N'
         AND SYSDATE BETWEEN startdate AND  NVL(enddate, SYSDATE)
         )
    WHERE rn = 1; 
    
    PATIENT_ID FIRST_ LAST_ ADDRESS CITY   TY STARTDATE ENDDATE
    ---------- ------ ----- ------- ------ -- --------- ---------
             1 sanjay kumar 6222    dsadsa P  01-JAN-07 01-JAN-10
             2 ajay   singh fsdfsd  fsdfsd M  01-MAR-07 09-OCT-09
             3 Mike   John  asfd     sfds  D 01-FEB-08 10-OCT-09
    
  • Reg: Query writing - please help

    Hello

    can you please help me get the results below?

    select * from emp_time
    
    EMP_ID  SHIFT_START_TIME                      SHIFT_END_TIME                        YARD_DEPARTURE                                                EFFECTIVE_DATE
    220541  03-12-08                              03-12-08                              y                                                              03-12-08
    220541  04-12-08                              04-12-08                              y                                                              04-12-08
    220541  18-02-09                              18-02-09                              y                                                              18-02-09
    220541  20-02-09                              20-02-09                              y                                                              20-02-09
    220541  24-02-09                              24-02-09                              y                                                              24-02-09
    220541  25-02-09                              25-02-09                              y                                                              25-02-09
    220541  26-02-09                              26-02-09                              y                                                              26-02-09
    220541  27-02-09                              27-02-09                              y                                                              27-02-09
    220541  28-02-09                              28-02-09                              y                                                              28-02-09
    220541  28-03-09                              28-03-09                              y                                                              28-03-09
    
    select * from emp_time
    where effective_date between to_date('02-20-09','mm-dd-yy') and to_date('02-24-09','mm-dd-yy');
    
    EMP_ID  SHIFT_START_TIME                      SHIFT_END_TIME                        YARD_DEPARTURE                                                EFFECTIVE_DATE
    220541  20-02-09                              20-02-09                              y                                                              20-02-09
    220541  24-02-09                              24-02-09                              y                                                              24-02-09
    
    Expected output:
    
    EMP_ID  SHIFT_START_TIME                      SHIFT_END_TIME                        YARD_DEPARTURE                                                EFFECTIVE_DATE
    220541  20-02-09                              20-02-09                              y                                                              20-02-09
    220541  20-02-09                              20-02-09                              y                                                              21-02-09
    220541  20-02-09                              20-02-09                              y                                                              22-02-09
    220541  20-02-09                              20-02-09                              y                                                              23-02-09
    220541  24-02-09                              24-02-09                              y                                                              24-02-09
    
    
    

    Oracle Version: 11.2.0

    Kind regards

    Jame

    Let me make a try also, because your statement only a min and max on XX, efective_date whatever the emp_id ;-)

    I pretend not, this one is better (too many results that must be filtered with a separate), but it should be emp id independent

    with emp_exist as (select emp_id

    shift_start_time

    shift_end_time

    yard_departure

    effective_date

    of emp_time)

    emp_fake as (select emp_id separate

    to_date('02-20-09','mm-dd-yy') shift_start_time

    to_date('02-20-09','mm-dd-yy') shift_end_time

    , 'y' yard_departure

    , to_date (20 February 09 ',' dd-mm-aa "") + effective_date level 1

    of emp_exist

    connect by level<=>

    Select emp_id nvl (t1.emp_id, t2.emp_id)

    nvl (t1.shift_start_time, t2.shift_start_time) shift_start_time

    nvl (t1.shift_end_time, t2.shift_end_time) shift_end_time

    nvl (t1.yard_departure, t2.yard_departure) yard_departure

    nvl (t1.effective_date, t2.effective_date) effective_date

    of emp_exist t1

    join right emp_fake t2

    on (t1.emp_id = t2.emp_id

    and t1.effective_date = t2.effective_date)

    order effective_date;

Maybe you are looking for