the non-null values insert select

Please could you help me on this:

INSERT INTO new_table (ID, hobby, country, internal_id, cat)

SELECT ID, hobby, country internal_id,

Max (CASE cat WHEN = 'ben' THEN 1

WHAT cat = 'ale' THEN 2

WHAT cat = 'inf' THEN 3

WHAT cat = "cad" THEN 4

WHAT cat = 'juv' THEN 5

WHAT cat = 'sen' THEN 6

WHAT cat = 'teacher' THEN 7

END)

Ancienne_table GROUP BY ID, hobby, country, internal_id;

the lines on ancienne_table have duplicates regs as follows:

IDCountryHobbyInternal_IDCAT
1OmanRugby Union63180Juv
1OmanRugby Union63180Teacher
1OmanRugby Union63180Juv

the request requires the output as follows:

IDCountryHobbyInternal_IDCAT
1OmanRugby Union631807

because cat = 'juv' (5) is less cat =' teacher (7).

all columns are not null and the primary key of the new_table is ID + hobby

The script displays the following output:

01400 00000 - 'impossible to insert NULL into (%s)' because does not recognize the function max.

Thanks in advance.

Kind regards.

Actually there are values not in the range of cat. There is an error in the old data table.

I proceeded to remove the lines out of reach in the cat because column that are not relevant in this case.

The parameters I use Oracle are:

Oracle Database 11 g Enterprise Edition Release 11.2.0.1.0 - 64 bit Production

PL/SQL Release 11.2.0.1.0 - Production

"CORE 11.2.0.1.0 Production."

AMT for 64-bit Windows: Version 11.2.0.1.0 - Production

NLSRTL Version 11.2.0.1.0 - Production

Thank you very much. You are so helpful.

Kind regards.

Tags: Database

Similar Questions

  • Need to get the latest news for a group-max (value) of the non-null values

    Hello

    Here is my table and data

    SQL > desc method

    NUMBER OF SEQ_ID
    EMP_ID NUMBER
    ISSUE GUID
    FIRST NAME VARCHAR2 (30)
    LAST_NAME VARCHAR2 (30)
    E-MAIL VARCHAR2 (45)
    PHONENO VARCHAR2 (30)

    SQL > Select * method;

    SEQ_ID EMP_ID GUID FIRST_NAME LAST_NAME EMAIL PHONENO
    ------ - ----- ---- --------- --------------------------------------------------- ----------------------------------------- ------------------------------
    1 100 20 RAJA HHH 686678
    2 100 20 [email protected]
    3 100 20 RAJA 134555
    4 100 20 HAPPY [email protected]
    5 200 20 RAM
    6 200 20 JJJ 2345667
    7 200 20 RM GGG [email protected] 1234557
    8 200 20 [email protected] RRR

    8 selected lines


    I want the latest news on the employee, the group id comibnation for the rest of the columns.

    So I want to get the following result.

    100 20 RAVI HHH [email protected] 134555

    200 20 RRR GGG [email protected] 1234557


    If you note here for the family name, we take the previous value non-zero value Eg HHH column and is the same for all columns.

    It's the value of line max (seqid) for empid, combination of guid and if the column values are null, we must get the previous maximum value non-zero.

    The seqid is also there that the sequence.

    Please help me to get the result set.

    Thanks in advance.

    mjhraja.
    SQL> select emp_id, guid, max(first_name) first_name, max(last_name) last_name
      2    , max(email) email, max(phoneno) phoneno
      3  from (
      4    select emp_id, guid
      5      , first_value (first_name ignore nulls)
      6          over (partition by emp_id, guid order by seq_id desc) first_name
      7      , first_value (last_name ignore nulls)
      8          over (partition by emp_id, guid order by seq_id desc) last_name
      9      , first_value (email ignore nulls)
     10          over (partition by emp_id, guid order by seq_id desc) email
     11      , first_value (phoneno ignore nulls)
     12          over (partition by emp_id, guid order by seq_id desc) phoneno
     13    from emp_info
     14  )
     15  group by emp_id, guid
     16  /
    
        EMP_ID GUID FIRST_NAME LAST_NAME  EMAIL                PHONENO
    ---------- ---- ---------- ---------- -------------------- ----------
           100   20 RAVI       HHH        [email protected]         134555
           200   20 RRR        GGG        [email protected]       1234557
    
    SQL> 
    
  • Count of non-null values

    I have a column of data, and there are values and values NULL how would count just the values on a resume?
    Everything I tried to give me the total number of lines not the non-null values...

    TIA

    Rose

    should count the element_name whose values are not null.

  • History of backfilling with non-null values

    Hi gurus,

    in Oracle 11 g, which would be the best way to update a null with the next available non-null value?

    For example: in the code below, I need to update the null in 20130201 record (key = 1) (key date) with a value of 100 available at the next available date IE 20130203.
    Same key = 3 value will be updated with key = 4 value i.e. 200.
    1,ABCD, 20130201, NULL
    2,ABCD,20130203, 100
    3,ABCD, 20130205, NULL
    4, ABCD,20130207, 200
    Thank you
    S
    with t as (
               select  tbl.*,
                       last_value(nvl2(c4,c1,null) ignore nulls) over(order by c1) prev_c1,
                       last_value(c4 ignore nulls) over(order by c1) prev_c4,
                       first_value(nvl2(c4,c1,null) ignore nulls) over(order by c1 rows between current row and unbounded following) next_c1,
                       first_value(c4 ignore nulls) over(order by c1 rows between current row and unbounded following) next_c4
                 from  tbl
              )
    select  c1,
            c2,
            c3,
            c4,
            case
              when c4 is not null then c4
              when c1 - prev_c1 < next_c1 - c1 then prev_c4
              else next_c4
            end new_c4
      from  t
    /
    
            C1 C2           C3         C4     NEW_C4
    ---------- ---- ---------- ---------- ----------
             1 ABCD   20130201                   100
             2 ABCD   20130203        100        100
             3 ABCD   20130205                   200
             4 ABCD   20130207        200        200
    
    SQL> 
    

    SY.

  • validation working is not to count the non-null fields

    I have six fields on the screen, I want to check that at least 3 of them are filled (3)...
    I created a validation as follows to count the non-null fields... but it's not working...
    Please help solve this problem or recommend a better way to do this...


    declare
    number of question_count;
    Start
    question_count: = 0;
    If: p1_t_question_1 is null then
    null;
    on the other
    question_count: =: question_count + 1;
    end if;
    If: p1_t_question_2 is null then
    null;
    on the other
    question_count: =: question_count + 1;
    end if;
    If: p1_t_question_3 is null then
    null;
    on the other
    question_count: =: question_count + 1;
    end if;
    If: p1_t_question_4 is null then
    null;
    on the other
    question_count: =: question_count + 1;
    end if;
    If: p1_t_question_6 is null then
    null;
    on the other
    question_count: =: question_count + 1;
    end if;
    If: p1_t_question_6 is null then
    null;
    on the other
    question_count: =: question_count + 1;
    end if;

    If: question_count > 2 then return false;
    otherwise returns true;
    end if;
    end;

    I'll take a look a bit.
    In the meantime, check if the code as

    If the trim (both '%' from: p1_question_with_nul') = 'null '.

    help

    Ok. Take a look now. I used the function TRIM as shown above and also changed the comparison for questions 3 text based on ' if: p1_question_with_text is null'

    Seems to work now

    CITY

    Published by: city has 23 October 2009 05:26

  • Do we not have an oracle function to select the non-null columns at the beginning

    Hello

    I have 8 columns. Some of them may be null.

    I want to display all 8 columns in my result. Columns not null will be first and no one at the end.

    Here is an example of data:

    Employee table:
     Employee_id   Emp_fname  emp_lname  emp_mname  dept salary emp_height  emp_weight
       1               aaa        ddd                d1   100      6           180
       2               bbb                ccc             120                 169
       3               dfe                           d2            5.9         223
    The expected result is:
    result1 result2   result3 result4  result5  result6 result7 result8
    1        aaa        ddd     d1       100     6        180
    2        bbb        ccc     120      169
    3        dfe        d2      5.9      223
    Thank you.

    Published by: BluShadow on July 12, 2012 16:12
    addition of {noformat}
    {noformat} tags for readability.  Please read {message:id=9360002} and do this yourself in future.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

    945559 wrote:
    I'm using Oracle Database 11 g Release 11.2.0.3.0.

    Here is my sample data.

    CREATE TABLE saleshist (product VARCHAR2 (30), q1 NUMBER NUMBER of q2, q3 NUMBER, NUMBER of T4);
    INSERT INTO saleshist VALUES ('EE Oracle', 100, 123, null, 128);
    INSERT INTO saleshist VALUES (null, 'Partitioning', 100, 130, 128);

    desired output:
    col1, col2, col3 col4
    'Oracle EE', 100, 123, 128, null
    "Partitioning", 100,130,128, null

    It seems that you have 4 column headers, but 5 columns.

    Assuming that the character ' ~' never occurs in one of the columns:

    WITH   got_list           AS
    (
         SELECT     '~' || product
              || '~' || TO_CHAR (q1)
              || '~' || TO_CHAR (q2)
              || '~' || TO_CHAR (q3)
              || '~' || TO_CHAR (q4)     AS list
         FROM    saleshist
    )
    SELECT  REGEXP_SUBSTR (list,  '[^~]+', 1, 1)     AS col1
    ,       REGEXP_SUBSTR (list,  '[^~]+', 1, 2)     AS col2
    ,       REGEXP_SUBSTR (list,  '[^~]+', 1, 3)     AS col3
    ,       REGEXP_SUBSTR (list,  '[^~]+', 1, 4)     AS col4
    ,       REGEXP_SUBSTR (list,  '[^~]+', 1, 5)     AS col5
    FROM    got_list
    ;
    

    Output:

    COL1            COL2       COL3       COL4       COL5
    --------------- ---------- ---------- ---------- ----------
    Oracle EE       100        123        128
    Partitioning    100        130        128
    

    If you need a multiple character string, such as '? ~', to delimit the columns, then the same basic approach works, but the regular expression is messier.

    Product is just another column in this solution. Can be NULL, and it doesn't have to be unique.

  • NULL value inserted into the Table

    I'm trying to solve a problem with the OAF page...
    where to commit a record... 2 columns are saved with NULL values.
    I tried to compare properties for items in the Page for those who save the correct value and the one with the white but has not seen any change.

    I'm posting to view Instance and view attribute for the elements properly. The elements are themselves filled with an LOV and are read-only for objects.

    Can you suggest how to debug this problem?

    ReadOnly elements will not be updated in the database...

    Workaround solution:
    1. create a formValue with the same view attribute
    2. create another card for lov to return the value to the value of the shape as well

    This should solve your problem...

    Prasanna-

  • indicates the column NULL values if I need display - (hifen or less symbol)

    In the Obiee displays NULL values if I need display - (hifen or less symbol),
    Please someone help me...


    Thank you
    René-coral

    If your column is around, then she can raise error. Try:
    IFNULL (cast (column as a varchar (10)), '-')

  • Use Insert all but want only to insert records for non-null values

    Hi, New here and could use some help with my sql statement.  I have a portfolio with denormalized data table that has been inserted via .csv file.  It is a contract with a base, options and extensions.  I need to create a contract record, then a separate record in the table for each base/option/extension contract Details.  I use INSERT ALL so I maintain the CONTRACT_ID for the secondary table.  My problem is that my statement puts a line in the table of details for the null records, that I don't want.  (i.e. 123 contract has a Base, Option 1, Option 2 and Option 3, but I'm getting a line for these lines with dates more null for Option 4, Ext 1, Ext 2, Ext 3 and Ext 4).  I want to insert only when the % CTRT_STRT_DT is NOT NULL.  It will be different for each contract.  Where should I put the IS NOT NULL statement?  Here is my complete insert statement:

    -Contract data

    INSERT ALL

    IN LRTC)

    CTRT_ID,

    PRGRM_OFC_TYP_CD,

    CTRT_NME,

    CREN_DT)

    VALUES)

    CTRT_ID_SEQ. NEXTVAL,

    PRGRM_OFC_TYP_CD,

    SPLS_SRVS_DESC,

    SYSDATE)

    -Contract detail for another period of performance data

    IN CTRT_DTLS)

    CTRT_ID,

    CTRT_STAT_CD,

    CTRT_PER_NME,

    CTRT_STRT_DT,

    CTRT_END_DT,

    CTRT_CURR_AMT,

    CTRT_NO,

    CTRT_VNDR_NME,

    CREN_DT)

    VALUES)

    CTRT_ID_SEQ. NEXTVAL,

    1,

    "Other."

    OTHER_CTRT_STRT_DT,

    OTHER_CTRT_END_DT,

    OTHER_CTRT_CURR_AMT,

    CTRT_NO,

    CTRTR_NME,

    SYSDATE)

    -Contract detail data for the reference period of performance

    IN CTRT_DTLS)

    CTRT_ID,

    CTRT_STAT_CD,

    CTRT_PER_NME,

    CTRT_STRT_DT,

    CTRT_END_DT,

    CTRT_CURR_AMT,

    CTRT_NO,

    CTRT_VNDR_NME,

    CREN_DT)

    VALUES)

    CTRT_ID_SEQ. NEXTVAL,

    1,

    'Base ',.

    BASE_CTRT_STRT_DT,

    BASE_CTRT_END_DT,

    BASE_CTRT_CURR_AMT,

    CTRT_NO,

    CTRTR_NME,

    SYSDATE)

    -Data contract detail for Option 1 execution time-out

    IN CTRT_DTLS)

    CTRT_ID,

    CTRT_STAT_CD,

    CTRT_PER_NME,

    CTRT_STRT_DT,

    CTRT_END_DT,

    CTRT_CURR_AMT,

    CTRT_NO,

    CTRT_VNDR_NME,

    CREN_DT)

    VALUES)

    CTRT_ID_SEQ. NEXTVAL,

    1,

    "Option 1",

    OPT1_CTRT_STRT_DT,

    OPT1_CTRT_END_DT,

    OPT1_CTRT_CURR_AMT,

    CTRT_NO,

    CTRTR_NME,

    SYSDATE)

    -Data contract detail for Option 2 execution time-out

    IN CTRT_DTLS)

    CTRT_ID,

    CTRT_STAT_CD,

    CTRT_PER_NME,

    CTRT_STRT_DT,

    CTRT_END_DT,

    CTRT_CURR_AMT,

    CTRT_NO,

    CTRT_VNDR_NME,

    CREN_DT)

    VALUES)

    CTRT_ID_SEQ. NEXTVAL,

    1,

    'Option 2',

    OPT2_CTRT_STRT_DT,

    OPT2_CTRT_END_DT,

    OPT2_CTRT_CURR_AMT,

    CTRT_NO,

    CTRTR_NME,

    SYSDATE)

    -Data contract detail for Option 3 lead time

    IN CTRT_DTLS)

    CTRT_ID,

    CTRT_STAT_CD,

    CTRT_PER_NME,

    CTRT_STRT_DT,

    CTRT_END_DT,

    CTRT_CURR_AMT,

    CTRT_NO,

    CTRT_VNDR_NME,

    CREN_DT)

    VALUES)

    CTRT_ID_SEQ. NEXTVAL,

    1,

    'Option 3',

    OPT3_CTRT_STRT_DT,

    OPT3_CTRT_END_DT,

    OPT3_CTRT_CURR_AMT,

    CTRT_NO,

    CTRTR_NME,

    SYSDATE)

    -Data contract detail for Option 4 execution time-out

    IN CTRT_DTLS)

    CTRT_ID,

    CTRT_STAT_CD,

    CTRT_PER_NME,

    CTRT_STRT_DT,

    CTRT_END_DT,

    CTRT_CURR_AMT,

    CTRT_NO,

    CTRT_VNDR_NME,

    CREN_DT)

    VALUES)

    CTRT_ID_SEQ. NEXTVAL,

    1,

    'Option 4',

    OPT4_CTRT_STRT_DT,

    OPT4_CTRT_END_DT,

    OPT4_CTRT_CURR_AMT,

    CTRT_NO,

    CTRTR_NME,

    SYSDATE)

    -Data contract detail for Extension 1 execution time-out

    IN CTRT_DTLS)

    CTRT_ID,

    CTRT_STAT_CD,

    CTRT_PER_NME,

    CTRT_STRT_DT,

    CTRT_END_DT,

    CTRT_CURR_AMT,

    CTRT_NO,

    CTRT_VNDR_NME,

    CREN_DT)

    VALUES)

    CTRT_ID_SEQ. NEXTVAL,

    1,

    'Extension 1',

    EXT1_CTRT_STRT_DT,

    EXT1_CTRT_END_DT,

    EXT1_CTRT_CURR_AMT,

    CTRT_NO,

    CTRTR_NME,

    SYSDATE)

    -Data contract detail for Extension 2 execution time-out

    IN CTRT_DTLS)

    CTRT_ID,

    CTRT_STAT_CD,

    CTRT_PER_NME,

    CTRT_STRT_DT,

    CTRT_END_DT,

    CTRT_CURR_AMT,

    CTRT_NO,

    CTRT_VNDR_NME,

    CREN_DT)

    VALUES)

    CTRT_ID_SEQ. NEXTVAL,

    1,

    '2 Extension',

    EXT2_CTRT_STRT_DT,

    EXT2_CTRT_END_DT,

    EXT2_CTRT_CURR_AMT,

    CTRT_NO,

    CTRTR_NME,

    SYSDATE)

    -Data contract detail for Extension 3 lead time

    IN CTRT_DTLS)

    CTRT_ID,

    CTRT_STAT_CD,

    CTRT_PER_NME,

    CTRT_STRT_DT,

    CTRT_END_DT,

    CTRT_CURR_AMT,

    CTRT_NO,

    CTRT_VNDR_NME,

    CREN_DT)

    VALUES)

    CTRT_ID_SEQ. NEXTVAL,

    1,

    '3 Extension',

    EXT3_CTRT_STRT_DT,

    EXT3_CTRT_END_DT,

    EXT3_CTRT_CURR_AMT,

    CTRT_NO,

    CTRTR_NME,

    SYSDATE)

    -Data contract detail for Extension 4 execution time-out

    IN CTRT_DTLS)

    CTRT_ID,

    CTRT_STAT_CD,

    CTRT_PER_NME,

    CTRT_STRT_DT,

    CTRT_END_DT,

    CTRT_CURR_AMT,

    CTRT_NO,

    CTRT_VNDR_NME,

    CREN_DT)

    VALUES)

    CTRT_ID_SEQ. NEXTVAL,

    1,

    '4 Extension',

    EXT4_CTRT_STRT_DT,

    EXT4_CTRT_END_DT,

    EXT4_CTRT_CURR_AMT,

    CTRT_NO,

    CTRTR_NME,

    SYSDATE)

    SELECT

    PRGRM_OFC_TYP_CD,

    SPLS_SRVS_DESC,

    CTRT_NO,

    CTRTR_NME,

    To_date(PER_OF_PFRM_OTHER_ST_DT,'MM/DD/YYYY') as OTHER_CTRT_STRT_DT,

    To_date(PER_OF_PFRM_OTHER_END_DT,'MM/DD/YYYY') as OTHER_CTRT_END_DT,

    CUR_VAL_BY_POP_OTHER_AMT as OTHER_CTRT_CURR_AMT,

    To_date(PER_OF_PFRM_BASE_ST_DT,'MM/DD/YYYY') as BASE_CTRT_STRT_DT,

    To_date(PER_OF_PFRM_BASE_END_DT,'MM/DD/YYYY') as BASE_CTRT_END_DT,

    CUR_VAL_BY_POP_BASE_AMT as BASE_CTRT_CURR_AMT,

    To_date(PER_OF_PFRM_OPT1_ST_DT,'MM/DD/YYYY') as OPT1_CTRT_STRT_DT,

    To_date(PER_OF_PFRM_OPT1_END_DT,'MM/DD/YYYY') as OPT1_CTRT_END_DT,

    CUR_VAL_BY_POP_OPT1_AMT as OPT1_CTRT_CURR_AMT,

    To_date(PER_OF_PFRM_OPT2_ST_DT,'MM/DD/YYYY') as OPT2_CTRT_STRT_DT,

    To_date(PER_OF_PFRM_OPT2_END_DT,'MM/DD/YYYY') as OPT2_CTRT_END_DT,

    CUR_VAL_BY_POP_OPT2_AMT as OPT2_CTRT_CURR_AMT,

    To_date(PER_OF_PFRM_OPT3_ST_DT,'MM/DD/YYYY') as OPT3_CTRT_STRT_DT,

    To_date(PER_OF_PFRM_OPT3_END_DT,'MM/DD/YYYY') as OPT3_CTRT_END_DT,

    CUR_VAL_BY_POP_OPT3_AMT as OPT3_CTRT_CURR_AMT,

    To_date(PER_OF_PFRM_OPT4_ST_DT,'MM/DD/YYYY') as OPT4_CTRT_STRT_DT,

    To_date(PER_OF_PFRM_OPT4_END_DT,'MM/DD/YYYY') as OPT4_CTRT_END_DT,

    CUR_VAL_BY_POP_OPT4_AMT as OPT4_CTRT_CURR_AMT,

    To_date(PER_OF_PFRM_EXT1_ST_DT,'MM/DD/YYYY') as EXT1_CTRT_STRT_DT,

    To_date(PER_OF_PFRM_EXT1_END_DT,'MM/DD/YYYY') as EXT1_CTRT_END_DT,

    CUR_VAL_BY_POP_EXT1_AMT as EXT1_CTRT_CURR_AMT,

    To_date(PER_OF_PFRM_EXT2_ST_DT,'MM/DD/YYYY') as EXT2_CTRT_STRT_DT,

    To_date(PER_OF_PFRM_EXT2_END_DT,'MM/DD/YYYY') as EXT2_CTRT_END_DT,

    CUR_VAL_POP_EXT2_AMT as EXT2_CTRT_CURR_AMT,

    To_date(PER_OF_PFRM_EXT3_ST_DT,'MM/DD/YYYY') as EXT3_CTRT_STRT_DT,

    To_date(PER_OF_PFRM_EXT3_END_DT,'MM/DD/YYYY') as EXT3_CTRT_END_DT,

    CUR_VAL_BY_POP_EXT3_AMT as EXT3_CTRT_CURR_AMT,

    To_date(PER_OF_PFRM_EXT4_ST_DT,'MM/DD/YYYY') as EXT4_CTRT_STRT_DT,

    To_date(PER_OF_PFRM_EXT4_END_DT,'MM/DD/YYYY') as EXT4_CTRT_END_DT,

    CUR_VAL_BY_POP_EXT4_AMT as EXT4_CTRT_CURR_AMT

    OF CTRT_HLDG A, PRGRM_OFC_TYP_LKUP B

    WHERE UPPER (A.OFC_NME) = UPPER (B.PRGRM_OFC_TYP_NME);

    Thanks for any help.

    Chris

    You're close.  Basically, you add one when befire each Inserts lines.  Something like:

    INSERT ALL

    When the ctrt_id is not null then

    IN LRTC (PRGRM_OFC_TYP_CD, CTRT_NME, CTRT_ID, CREN_DT)

    VALUES (CTRT_ID_SEQ. NEXTVAL, PRGRM_OFC_TYP_CD,

    SPLS_SRVS_DESC, SYSDATE)

    -Contract detail for another period of performance data

    When the OTHER_CTRT_STRT_DT is not null then

    IN (CTRT_ID, CTRT_STAT_CD, CTRT_PER_NME, CTRT_DTLS

    CTRT_STRT_DT, CTRT_END_DT, CTRT_CURR_AMT,

    CTRT_NO, CTRT_VNDR_NME, CREN_DT)

    VALUES (CTRT_ID_SEQ. NEXTVAL, 1, 'Other', OTHER_CTRT_STRT_DT,

    OTHER_CTRT_END_DT, OTHER_CTRT_CURR_AMT,

    CTRT_NO, CTRTR_NME, SYSDATE)

    -Contract detail data for the reference period of performance

    When the BASE_CTRT_STRT_DT is not null then

    IN (CTRT_ID, CTRT_STAT_CD, CTRT_PER_NME, CTRT_DTLS

    CTRT_STRT_DT, CTRT_END_DT, CTRT_CURR_AMT,

    CTRT_NO, CTRT_VNDR_NME, CREN_DT)

    VALUES (CTRT_ID_SEQ. NEXTVAL, 1, 'Base', BASE_CTRT_STRT_DT,

    BASE_CTRT_END_DT, BASE_CTRT_CURR_AMT, CTRT_NO,

    CTRTR_NME, SYSDATE)

    You may need to adjust my predicates, but it should give you the idea.

    John

  • Return the first line with a non-null value in a column

    Hey there, gurus. Can someone give me a hand with this?

    I have a table with six columns: Class_ID, Course_ID, start_date, Last_Name, First_Name, company. When our students take a class and will fill in an evaluation form, they fill in the Class_ID Last_Name First_Name, and Company. The Course_ID and Start_Date are filled automatically based on the Class_ID.

    I am trying to generate a report that contains Class_ID, Course_ID and Start_Date, PLUS the value of the first not null in the column of company for a combination given to the three first columns. Here is my SQL that does not quite do the trick:

    Select distinct c.class_id, c.course_id, c.start_date, p.company
    class c, person p, student s
    where substr (c.course_id, 1, 3) = "OBI".
    and c.start_date > = SYSDATE-30
    and c.class_id = s.class_id
    and s.pid = p.pid
    order start_date, company, class_id, course_id

    At present, if in a given class, a student entered "CCCC" under the name of the company and another entry "Collin Co Comm Coll" and another between "Collin College", I would get three rows of this class when you use the SQL above. I want just one line. I don't really like which company names gets selected - one of them will be great for our needs. I just need to see a line for each class_id and single value that some students entered the area of the company. And I guess I have to say that if no student fills in the name of the company I need to see the value null, because I need to see the line no matter what.

    Looks like some sort of funky outer join or a coalesce, but it is beyond my ability to understand.

    Now I get this:
    During class 1234 SQL271 Date 22/07/2013 company XYZ Inc.
    Made up of class 1234 SQL271 Date 22/07/2013 company XYZ
    1234 SQL271 Date 22/07/2013 company XYZ, class

    What I want:
    Class 1234 SQL271 22/07/2013 Date course XYZ Inc. or one of the other 2

    Thanks in advance!
    select c.class_id, c.course_id, c.start_date, max(p.company) company
    from class c, person p, student s
    where substr(c.course_id,1,3) = 'OBI'
    and c.start_date >= SYSDATE-30
    and c.class_id = s.class_id
    and s.pid = p.pid
    group by c.class_id, c.course_id, c.start_date
    order by class_id, start_date, course_id
    

    Or if you want to list all businesses, you can use LISTAGG if you're on the latest version of ORACLE

  • Newbie - sorting data at the top and bottom of the column, NULL values

    I have this example:
    drop table da;
    create table (da)
    ANAME varchar2 (10)
    , number (3,0) db_ID
    (, dc_ID number (3.0));
    insert into da(aname,db_id,dc_ID) values('Apples',1,100);


    drop table db;
    Create table (db)
    VARCHAR2 (10) aBBB
    , rec_id number (3.0)
    (, number (3.0)) db_id.
    insert into db (aBBB, rec_id, db_id) values('B1test',1,1);
    insert into db (aBBB, rec_id, db_id) values('B2test',2,1);


    drop table dc;
    Create table (dc)
    VARCHAR2 (10) of the aCCC
    , rec_id number (3.0)
    (, dc_id number (3.0));


    insert into dc (aCCC, rec_id, dc_id) values('C1test',10,100);
    insert into dc (aCCC, rec_id, dc_id) values('C2test',20,100);
    insert into dc (aCCC, rec_id, dc_id) values('C3test',30,100);

    Select aname, db_id, dc_Id
    abbb, accc null null
    of da
    where db_id = 1
    Union of all the
    Select A.aname, A.db_id, null dc_id
    B.aBBB, accc null
    DB B right outer join da A
    ON B.db_id = A.db_id
    Union of all the
    Select A.aname, null db_id, A.dc_id
    null aBBB, B.accc
    Since DC B right outer join da A
    ON B.dc_id = A.dc_id

    I get results like:
    ANAME DB_ID ABBB ACCC DC_ID
    ---------- ---------- ---------- ---------- --------
    Apple 1 100
    Apples 1 B1test
    Apples 1 B2test
    Apples 100 C1test
    Apples 100 C2test
    Apples 100 C3test

    However, I want to be:
    ANAME DB_ID ABBB ACCC DC_ID
    ---------- ---------- ---------- ---------- --------
    C1test of B1test of 1 100 apples
    C2test of B2test of 1 100 apples
    Apple 1 100 C3test

    Suggestions?

    TIA.
    Steve42

    Actually looking at your results once again, that don't necessarily look as a problem of sorting.

    What is your version of Oracle?

    Can you explain why these lines are your expected results? For example if I have run the following query I see these results:

    SQL> SELECT  DA.ANAME
      2  ,       DA.DB_ID
      3  ,       DA.DC_ID
      4  ,       DB.ABBB
      5  ,       DC.ACCC
      6  FROM    DA
      7  JOIN    DB      ON DA.DB_ID = DB.DB_ID
      8  JOIN    DC      ON DC.DC_ID = DA.DC_ID
      9  ORDER BY 1, 2, 3, 4, 5
     10  /
    
    ANAME                     DB_ID                DC_ID ABBB       ACCC
    ---------- -------------------- -------------------- ---------- ----------
    Apples                        1                  100 B1test     C1test
    Apples                        1                  100 B1test     C2test
    Apples                        1                  100 B1test     C3test
    Apples                        1                  100 B2test     C1test
    Apples                        1                  100 B2test     C2test
    Apples                        1                  100 B2test     C3test
    

    Yet in your results, you have your lines collapsed as B1TEST and C1TEST are in the SAME ROW, but B2TEST is not in the same line as C1TEST or C3TEST. Why?

    Is that what you are looking for?

    SQL> SELECT  DA.ANAME
      2  ,       DA.DB_ID
      3  ,       DA.DC_ID
      4  ,       DB.ABBB
      5  ,       DC.ACCC
      6  FROM    DA
      7  JOIN    DC              ON DC.DC_ID = DA.DC_ID
      8  LEFT OUTER JOIN    DB   ON DA.DB_ID = DB.DB_ID AND SUBSTR(DB.ABBB,2,1) = SUBSTR(DC.ACCC,2,1)
      9  ORDER BY 1, 2, 3, 4, 5
     10  /
    
    ANAME                     DB_ID                DC_ID ABBB       ACCC
    ---------- -------------------- -------------------- ---------- ----------
    Apples                        1                  100 B1test     C1test
    Apples                        1                  100 B2test     C2test
    Apples                        1                  100            C3test
    

    Published by: Centinul on January 15, 2010 11:01

  • with regard to the non-null columns

    Hi all
    I use 10g db.
    I have a query as below
    SELECT SH_CODE,SH_NAME,SH_NUMBER
    FROM SHIPPING_DETAILS
    WHERE SH_COMP = :GLOBAL.COMPANY
    AND TRUNC(SH_DATE) BETWEEN '01-JAN-2009' AND '02-JAN-2009'
    AND SH_NUMBER = NVL(l_num,SH_NUMBER)
    for the date of the range I have 5 files among them 4 records has value for the SH_NUMBER column and a block is null.
    but I don't get any record for which SH_NUMBER is set to null.
    why there is not this number?
    AND SH_NUMBER = NVL(l_num,SH_NUMBER)
    How this code above will be the judge?

    Please suggest me.

    Thank you...

    Try this:

    AND NVL(SH_NUMBER, -999) = NVL(l_num, NVL(SH_NUMBER, -999))
    

    or:

    AND (l_num IS NULL
    OR  (l_num IS NOT NULL
    AND SH_NUMBER = l_num))
    
  • A query to extract only the non-null columns.

    Hello

    I have a table with:

    COLA, COLB TEACHERS COLD COLE

    AA BB < null > < null > JJ

    < Null > < null > CC < null > EE


    I need a query that retrieves only 1 rows like this:

    COLA, COLB TEACHERS COLD COLE

    AA BB CC DD EE


    My version of the database is 11.2


    Thnak you

    odd design but in this case, you could go with MAX (or MIN)

    HTH

  • How to find null values in the table

    Hi experts,

    my version is oracledb10g:

    I am doubted simple.

    for example:

    create table ex1 (e varchar2 (20));

    Insert ex1 (null).

    insert into ex1 (e)

    number of will be: 2

    I can easily find the value "e".

    so my question is:

    How can I find null values in the table and I want to remove the null values from the table.

    ADF 7 wrote:
    Hi experts,

    my version is oracledb10g:

    I am doubted simple.

    for example:

    create table ex1 (e varchar2 (20));

    Insert ex1 (null).

    insert into ex1 (e)

    number of will be: 2

    It depends on what you expect. If you issue count (*) you ask Oracle "how many lines there. If you issue a County () you ask Oracle 'how non null values for are there. "

    ADF 7 wrote:

    I can easily find the value "e".

    so my question is:

    How can I find null values in the table and I want to remove the null values from the table.

    delete from 
    where  is null;
    

    Ideally you would simply place a NOT NULL constraint on the column in question, thus prohibiting NULL values into the system (assuming that your business needs, it is that the column MUST always have a value).

  • Fill with the previous 'not null' value ' Null' known values

    Hi all

    I have the following requirement to fill in missing values (null values) with the "Not null" values known previously available.

    Source of the example:

    Emp_Id Start_Dt LOC Comm Grade

    A101

    01/01/2013

    NJ4000B

    A101

    15/03/2013

    CA4800

    A101

    15/05/2013

    3500C

    A101

    25/07/2013

    2500

    A101

    20/12/2013

    NY5800A

    A101

    14/02/2013

    5000

    A101

    20/05/2014

    DC6000A

    A101

    03/06/2014

    3600C

    A102

    24/05/2013

    THE5000A

    A102

    15/12/20134300

    Expected results values in columns LOC and grades:

    Emp_Id Start_Dt LOC Comm Grade
    A101

    01/01/2013

    NJ4000BA101

    15/03/2013

    CA4800BA101

    15/05/2013

    CA3500CA101

    25/07/2013

    CA2500CA101

    20/12/2013

    NY5800AA101

    14/02/2013

    NY5000AA101

    20/05/2014

    DC6000AA101

    03/06/2014

    DC3600CA102

    24/05/2013

    THE5000AA102

    15/12/2013

    THE4300A

    Any suggestions would be helpful.

    Kind regards

    Arun

    Also, I think that this is a case of analytics. Last_value is perhaps the most appropriate function for the given task:

    Select emp_id

    start_dt

    last_value(loc ignore nulls) over (partition by emp_id arrested by start_dt) loc

    comm

    last_value(grade ignore nulls) about category (partition by emp_id arrested by start_dt)

    t

Maybe you are looking for