generate several lines of each row in a table

Hi all, consider the following data
with data1 as
(
  select 1 id, 0 cust_sh, null mx_sh, 123 cust_coll from dual union all
  select 2 id,0 cust_sh, 0 mx_sh, 0 cust_coll from dual union all
  select 3 id,456 cust_sh, 890 mx_sh, 123 cust_coll from dual 
),
data2 as
(
  select 'cust_sh' col_nm, 'ABC' nm from dual union all
  select 'mx_sh' col_nm, 'BDC' nm from dual union all
  select 'cust_coll' col_nm, 'HGR' nm from dual 
)
what I'm trying to do here is to join the two tables sort and produce this output
id  nm         amt
=====================
1   HGR        123
3   ABC        456
3   BDC        890
3   HGR        123
That's how I get this result. Data1 has lines with the ID and the columns with quantities such as cust_sh, etc.
data2 bearing the name of the column in data1 under col_nm and an additional column as nm.

for each line of Data1, I should take the amount column, look in database2 and the data1 amount is greater than 0.
then display the output.

for example, to get data1 row1. cust_sh is set to 0, so we don't look this column in Database2. mx_sh is set to null, which is not greater than 0, then us none.
cust_coll has the value 123 so we wait for this column in Database2. This is the third row in Database2. Therefore, view us the id and the amt of Data1 and data2 nm.

for line two of Data1, all after id columns are 0. don't have no need to display the output. only when the amount is greater than zero, we watch in Database2.

third row, cust sh has value 456 so we expect in database2 and we found in the first row. mx_sh has a value of 890 (greater than 0) so again once we look at Database2 and display the output.

can someone help me with a query that produces the output above?

Thanks in advance

OK, two more options:

WITH pivot_data1 AS (
  SELECT id, 'cust_sh' as col_nm, cust_sh as amt
  FROM data1
  WHERE cust_sh > 0
  UNION ALL
  SELECT id, 'mx_sh', mx_sh
  FROM data1
  WHERE mx_sh > 0
  UNION ALL
  SELECT id, 'cust_coll', cust_coll
  FROM data1
  WHERE cust_coll > 0
)
SELECT d1.id
     , d2.nm
     , d1.amt
FROM pivot_data1 d1
     JOIN data2 d2 ON d2.col_nm = d1.col_nm
;
WITH pivot_data1 AS (
  SELECT id
       , case i when 1 then 'cust_sh'
                when 2 then 'mx_sh'
                when 3 then 'cust_coll'
         end as col_nm
       , case i when 1 then cust_sh
                when 2 then mx_sh
                when 3 then cust_coll
         end as amt
  FROM data1
       CROSS JOIN ( SELECT level i FROM dual CONNECT BY level <= 3 )
)
SELECT d1.id
     , d2.nm
     , d1.amt
FROM pivot_data1 d1
     JOIN data2 d2 ON d2.col_nm = d1.col_nm
WHERE d1.amt > 0
;

Tags: Database

Similar Questions

  • Remove the checksum of each row in the table

    Hallo

    I got out as table and since I have just read the data with the checksum

    For example, I have to neglect the last 2 bytes in each row of the table. I tried several times but I can't succeed. It would be nice if someone help me to go around this problem.

    thanking you

    Best regards

    Karine

    Hi Karine,.

    have you tried really 'several times' to remove the last two characters of each string? Using an autoindexing FOR loop?

  • Determine the space used for each row in the table

    I am trying to determine how much disk space each row in the table will take place (data and indexes). I use sys_guids for my (PK_ID) primary key and foreign key columns (FK_xx), so who are on varchar2 (32).

    What is the best way to determine this?


    CREATE TABLE STU_ENROLLMENT
    (PK_ID VARCHAR2 (32), sys_guid() by default)
    CONSTRAINT pk_stu_enrollment
    KEY ELEMENTARY SCHOOL
    With the HELP of INDEX TABLESPACE sis_express_index
    STORAGE (4096 INITIAL
    NEXT 4096
    PCTINCREASE 0),.
    FK_STU_SCHOOL VARCHAR2 (32)
    FK_SCHOOL_CALENDAR VARCHAR2 (32)
    FK_PCC_CODE VARCHAR2 (32),
    FK_GRADE_LEVEL VARCHAR2 (32),
    FK_VOTECH VARCHAR2 (32),
    FK_SPECIAL_ED VARCHAR2 (32),
    FK_TUITION_CODE VARCHAR2 (32),
    FK_HOME_INSTRUCTION VARCHAR2 (32),
    FK_DISTRICT VARCHAR2 (32),
    FK_PROGRAM_CODE VARCHAR2 (32),
    FK_SERVING_SCHL VARCHAR2 (32),
    FK_RESPONSIBLE_SCHL VARCHAR2 (32),
    FK_ADA_CODE VARCHAR2 (32),
    FK_ENROLL_TYPE VARCHAR2 (32),
    FK_PCC_REASON VARCHAR2 (32),
    STANDARD_DAY NUMBER (3).
    PCC_NOTES VARCHAR2 (1000).
    USER_FLD1 VARCHAR2 (100),
    USER_FLD2 VARCHAR2 (100),
    USER_FLD3 VARCHAR2 (100),
    USER_FLD4 VARCHAR2 (100),
    USER_FLD5 VARCHAR2 (100),
    PCC_TRANSACTION_ID VARCHAR2 (32),
    TRANSACTION_NUMBER DEFAULT NUMBER '1',
    TRANSACTION_TYPE VARCHAR2 (1) DEFAULT 'A,'
    DATE OF LAST_UPDATE_DATE,
    LAST_UPDATE_USER VARCHAR2 (100))
    TABLESPACE sis_express_base
    STORAGE (5120 INITIAL
    NEXT 5120
    PCTINCREASE 0);

    CREATE INDEX STU_ENROLLMENT_I ON STU_ENROLLMENT (FK_STU_SCHOOL, FK_SCHOOL_CALENDAR, FK_PCC_CODE) SINGLE
    TABLESPACE sis_express_index
    STORAGE (4096 INITIAL
    NEXT 4096
    PCTINCREASE 0);

    CREATE INDEX STU_ENROLLMENT_I2 ON STU_ENROLLMENT (FK_SCHOOL_CALENDAR)
    TABLESPACE sis_express_index
    STORAGE (4096 INITIAL
    NEXT 4096
    PCTINCREASE 0);

    CREATE INDEX STU_ENROLLMENT_I3 ON STU_ENROLLMENT (FK_PCC_CODE)
    TABLESPACE sis_express_index
    STORAGE (4096 INITIAL
    NEXT 4096
    PCTINCREASE 0);

    CREATE INDEX STU_ENROLLMENT_I4 ON STU_ENROLLMENT (FK_GRADE_LEVEL)
    TABLESPACE sis_express_index
    STORAGE (4096 INITIAL
    NEXT 4096
    PCTINCREASE 0);

    CREATE INDEX STU_ENROLLMENT_I5 ON STU_ENROLLMENT (FK_VOTECH)
    TABLESPACE sis_express_index
    STORAGE (4096 INITIAL
    NEXT 4096
    PCTINCREASE 0);

    CREATE INDEX STU_ENROLLMENT_I6 ON STU_ENROLLMENT (FK_SPECIAL_ED)
    TABLESPACE sis_express_index
    STORAGE (4096 INITIAL
    NEXT 4096
    PCTINCREASE 0);

    CREATE INDEX STU_ENROLLMENT_I7 ON STU_ENROLLMENT (FK_TUITION_CODE)
    TABLESPACE sis_express_index
    STORAGE (4096 INITIAL
    NEXT 4096
    PCTINCREASE 0);

    CREATE INDEX STU_ENROLLMENT_I8 ON STU_ENROLLMENT (FK_HOME_INSTRUCTION)
    TABLESPACE sis_express_index
    STORAGE (4096 INITIAL
    NEXT 4096
    PCTINCREASE 0);

    CREATE INDEX STU_ENROLLMENT_I9 ON STU_ENROLLMENT (FK_DISTRICT)
    TABLESPACE sis_express_index
    STORAGE (4096 INITIAL
    NEXT 4096
    PCTINCREASE 0);

    CREATE INDEX STU_ENROLLMENT_I10 ON STU_ENROLLMENT (FK_PROGRAM_CODE)
    TABLESPACE sis_express_index
    STORAGE (4096 INITIAL
    NEXT 4096
    PCTINCREASE 0);

    CREATE INDEX STU_ENROLLMENT_I11 ON STU_ENROLLMENT (FK_SERVING_SCHL)
    TABLESPACE sis_express_index
    STORAGE (4096 INITIAL
    NEXT 4096
    PCTINCREASE 0);

    CREATE INDEX STU_ENROLLMENT_I12 ON STU_ENROLLMENT (FK_RESPONSIBLE_SCHL)
    TABLESPACE sis_express_index
    STORAGE (4096 INITIAL
    NEXT 4096
    PCTINCREASE 0);

    CREATE INDEX STU_ENROLLMENT_I13 ON STU_ENROLLMENT (FK_ADA_CODE)
    TABLESPACE sis_express_index
    STORAGE (4096 INITIAL
    NEXT 4096
    PCTINCREASE 0);

    CREATE INDEX STU_ENROLLMENT_I14 ON STU_ENROLLMENT (FK_ENROLL_TYPE)
    TABLESPACE sis_express_index
    STORAGE (4096 INITIAL
    NEXT 4096
    PCTINCREASE 0);

    CREATE INDEX STU_ENROLLMENT_I15 ON STU_ENROLLMENT (FK_PCC_REASON)
    TABLESPACE sis_express_index
    STORAGE (4096 INITIAL
    NEXT 4096
    PCTINCREASE 0);

    CREATE INDEX STU_ENROLLMENT_I16 ON STU_ENROLLMENT (PCC_TRANSACTION_ID)
    TABLESPACE sis_express_index
    STORAGE (4096 INITIAL
    NEXT 4096
    PCTINCREASE 0);

    CREATE INDEX STU_ENROLLMENT_I17 ON STU_ENROLLMENT (FK_STU_SCHOOL, PCC_TRANSACTION_ID, PK_ID)
    TABLESPACE sis_express_index
    STORAGE (4096 INITIAL
    NEXT 4096
    PCTINCREASE 0);

    Hello

    The maximum size of the row in the table

    Select table_name, ROUND (sum (data_length) / 1024,2) SIZE_IN_KB
    of user_tab_cols
    where table_name = 'STU_ENROLLMENT '.
    TABLE_NAME GROUP;

    +

    Select index_name table_name, ROUND (sum (CHAR_LENGTH) / 1024,2) SIZE_IN_KB
    of USER_IND_COLUMNS
    where table_name = 'STU_ENROLLMENT '.
    GROUP BY index_name table_name;

    See the size of the table - method with the average row size

    Concerning
    Hitgon

    Published by: hitgon on May 2, 2012 18:19

    Published by: hitgon on May 2, 2012 18:39

  • Creation of 5 rows in table B for each row in the table has

    create table A
    (col1 number,
    col2 varchar2(20)
    )
    /
     
    create table B
    (col1 number,
    col2 varchar2(20)
    )
    /
    
    insert into A values (1,'Jane');
    insert into A values (2,'Kate');
    insert into A values (3,'Stomp');
    
    
    SQL> select * from a;
    
          COL1 COL2
    ---------- -----------------
             1 Jane
             2 Kate
             3 Stomp
    For each row in the table, I want 5 rows to be created in the table b. table B should look like
        COL1   COL2
    ---------- -----------------
             1 Jane
             2 Jane
             3 Jane
             4 Jane
             5 Jane
             6 Kate
             7 Kate
             8 Kate
             9 Kate
             10 Kate
             11 Stomp
             12 Stomp
             13 Stomp
             14 Stomp
             15 Stomp
    How can I do this?

    Hello

    A way

    insert into b
    select rownum*5  , col2 from a
    union all
    select rownum*5+1, col2 from a
    union all
    select rownum*5+2, col2 from a
    union all
    select rownum*5+3, col2 from a
    union all
    select rownum*5+4, col2 from a
    

    Concerning
    Anurag

  • Swivel... Several lines in simple row, multiple column

    Dear Oracle Guru

    I have a table
    emp_req_order
    the fields are
    EmpNo, Deptno, req_order

    I need to find the max of Req_order for all dept

    so my request was

    SELECT max (req_order) emp_req_order Maxorder
    Deptno group

    put it was

    MaxOrder
    -----------
    45
    34
    23
    17

    I want the output of a line like

    45 34 23 17

    Instead of four rows data must be in a line, but four columns

    I searched the forum I even had a proposal as follows:
    Max (SELECT Decode(deptno,101,req_order)) const1,
    Const2 Max (decode(DEPTNO,102,req_order)),
    Const3 Max (decode(DEPTNO,103,req_order)),
    Const4 Max (decode(DEPTNO,104,req_order)),
    Of emp_req_order
    Group By deptno


    The output was still in several lines

    Kindly guide me in this regard

    with the best regards

    RSS

    SSR,

    Just drag the group clause for the purpose.

    Kind regards
    Rob.

  • How to generate several lines from a single line

    Hi, using Oracle 11 g R2.

    Looking for a way to display the result of a query where a column value is a text similar to the following field. There is only 1 row.

    create table testlist (col1 varchar2 (50))

    insert into testlist values (' ' 845, 999, ABC ")

    The values are always separated by commas. Looking for display the result as follows

    845

    999

    ABC

    Hello

    Since you have only 1 row in the table, you can do this:

    SELECT REGEXP_SUBSTR (col1

    , '[^,]+'

    1

    LEVEL

    ), Element

    OF testlist

    CONNECT BY LEVEL<= regexp_count="" (="">

    , '[^,]+'

    )

    ;

    Output:

    AGENDA

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

    845

    999

    ABC

  • Exaprom PDF: how cells that can cover several lines in the body of the table?

    I use Exaprom PDF but I have a case that I can't work.

    I would like to generate a table where a cell spans an entire line in the body of the table, the table should be translated as:

    ____________

    |__|__|__|__|

    |__|__|__|__|

    |___________|

    |__|__|__|__|

    Dear Marc,

    could you post an example how span a cell of an entire row in the table with the function body "Append custom Table.vi."

    For example, please use the 2009 version.

    Thank you

    Asper

    This message refers to the report Exaprom PDF generation tool:

    https://decibel.NI.com/content/docs/doc-10952

    Perhaps this.

  • Block a particular cell on each row in a table

    I have a single Table ("TableA") with 4 rows - all named 'Row '. So... Row [0], row [1], row [2] and row [3].


    Each row has 3 cells.

    What I'm trying to make is the cell ("Cell2") to lock the 2nd row - locking, essentially an entire column.

    I tried to use the script on a button, but it does not work.

    TableA.Row [*]. Cell2.access = 'protected ';

    Any ideas?

    Thanks in advance!

    Tony

    I figured it using a loop statement...

    Let this posted just in case it helps someone else in the future!

    lines of the var = xfa.resolveNodes ("TableA.Row [*]. Cell2");

    for (var i = 0; i)< rows.length;="">

    {

    var lockField = rows.item (i);

    lockField.access = 'protected ';

    }

  • Question: insertion of several lines in the MS Sql Server table using the DB adapter

    Hi all

    I managed to insert a single row in a table of MS SQL Server via the adapter DB to my process BPEL, but when I tried to insert in mutiple lines in the same table of MS SQL server, I encounter the error below.

    I use a DB SQL XA connection to connect to the server.

    Kindly help me to solve the problem. Thanks in advance.

    Error:

    " < bpelFault > < faultType > 0 < / faultType > < remoteFault xmlns =" http://schemas.Oracle.com/BPEL/extension "> < a name ="summary"part > < summary > exemption is is produced when the binding was used." Exception occurred during invocation of the JCA binding: "JCA binding run 'merge' reference operations have to: DBWriteInteractionSpec Execute Failed Exception." the merger failed. The descriptor name: [LoadCmpAggSpendStage.SapTable]. Caused by com.microsoft.sqlserver.jdbc.SQLServerException: incorrect syntax near ')'... Check the logs for the record output full DBAdapter before this exception. This exception is considered as reproducible, probably due to a communication failure. To be classified as not reproducible rather add property nonRetriableErrorCodes with the '102' value in the deployment descriptor (i.e. weblogic - RA.Xml). Auto retry a reproducible fault set composite.xml for this invoke these properties: jca.retry.interval, jca.retry.count and jca.retry.backoff. All properties are integers. ". The called JCA adapter threw an exception of resource. Please examine the error message above carefully to determine a resolution. < /Summary. (> < / piece > < part name = "detail" > < detail syntax > incorrect near ')'. < / details > < / piece > < part name = "code" > < code > 102 < / code > < / piece > < / remoteFault > < / bpelFault >

    Kind regards

    Balaji Rahmani

    It seems that in this case is called merge operation. If existing records (check primary key) are not there then it will be inserted on the other update. Check the syntax of the query that is created. It looks like she may have a supplement "). If you want to only insert then call insert operation and not merge.

  • Update a variable based on the expiration date checking against now() in each row of a table

    I use Coldfusion 9,0,0,251028 on Windows 7 64 bit with a Microsoft Access 97 database.

    I am creating a query that loops through all the rows in a table and checks if the current date is earlier than the date of expiration or later. 

    If the expiration date column is earlier to now(), it sets the column "is_current" to 0, which is a variable that determines if a message appears on a page (breakingnews.cfm) different. 

    The column that has the expiry date is "exp_dat" in the "news" table. The query I have at the moment is:

    <cfquery name="expire" datasource="#db#">
    

    Two things:

    1 - a query will never affect rows that you did not. So if new lines have the is_current set to 0, then it's because you want to set or out in the instructions of your return or you set a default value to the column. Just make sure that you set the 1 column when you insert the new line.

    2. Why are you doing this at all? Say you run your update query (which is relatively intensive) a millisecond before the expiration of one, it will still appear on your page. Why you not just do "SELECT * FROM MaTable WHERE correspondents > now() '?

    Obviously using cfqueryparams, but this is just an example. It seems for now what you do is store obsolete data in a database and cause you more work and overhead.

  • SQL query, generate several lines in a value in a field

    I use the Oracle database. Imaging I have a table with a row as follows

    col1 |  col2. COL3

    "Str1" | "Str2". 4

    The value of col3 = 4 is expected to generate 4 rows as follows:

    col1 |  col2. COL3

    "Str1" | "Str2". 1

    "Str1" | "Str2". 2

    "Str1" | "Str2". 3

    "Str1" | "Str2". 4

    After several hours in front of the screen and stil no chance - how do I create such a select query?

    It will work

    Select * from test_table:

    COL1 COL2 COL3

    str1 str2 2

    strA, strb 4

    STRX deltas 8

    WITH t(col1,col2,col3) AS
            (
            select col1,col2,col3 from test_table
             union all
             select col1,col2,col3-1 from t where col3>1)
    SELECT *
      FROM t
      order by col1,col3;
    

    OUTPUT:

    COL1 COL2 COL3

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

    str1 str2 1

    str1 str2 2

    strB Stra 1

    strA, strb 2

    strA, strb 3

    strA, strb 4

    deltas STRX 1

    STRX deltas 2

    deltas STRX 3

    STRX deltas 4

    STRX deltas 5

    STRX deltas 6

    STRX deltas 7

    STRX deltas 8

    See you soon,.

    Manik.

  • Several lines in a row with different column name

    Hello
    I have the table "v_Profile_ID" and "v_Trasaction".

    V_profile_ID columns
    1 profile_nbr
    2 idendifaction_number

    V_Trasaction columns (a historical data)
    1 profile_nbr
    2 Transaction_ID
    3.TransactionDate_Time
    4.Transaction_amount


    common profile_nbr in the tables 'v_Profile_ID' and 'v_Trasaction '.

    Are values in the v_profile_ID table

    profile_nbr Identification_nbr

    1001 Au1002
    1001 BD9089
    1001 FC3900


    To view the identification_3 of v_profile_ID, identification_2, identification_1, Profile_nbr
    and TransactionDate_Time Transaction_amount of v_Trasaction

    Profile_nbr identification_1 identification_2 identification_3 TransactionDate_Time Transaction_amount

    1001 Au1002 BD9089 FC3900 1000 2 April 2011


    Please can someone help me

    Thank you
    Petrilla

    Hello

    Try this... !!

    Select profile_nbr,
    max(decode(seq,1,identification_nbr,null)) identification_1,
    max(decode(seq,2,identification_nbr,null)) identification_2,
    max(decode(seq,3,identification_nbrl,null)) identification_3
    
    from (select profile_nbr,identification_nbr,row_number()
        over(partition by profile_nbr
           order by identification_nbr) seq
            from table_name) where seq<=3;
             group by profile_nbr;
    

    Concerning
    KPR

    * If it's OK... then do as correct
    * If it's useful... then make it as useful

    Published by: KPR on April 29, 2011 04:53

  • Generate several plots according to the size of table

    Hi all

    I'm relatively new to LabView and I read several threads that are similar to my problem, but I don't know how to implement it yet.  Basically I have a large table that stores the raw data read from multiple text files.  This large painting size would vary based on the number of text files generated by a data acquisition system.  I would like to draw each line of the table a new field.  Instead of manually wiring, is there an easier way around?

    Thank you

    Lynniz

    Lynniz,

    If you need a separate chart for each parcel then can determine you at least in advance what the maximum number of locations could be and show/hide your graphics programmatically based on data. All the graphs could be in a Subvi that appears separate from your main application window, or you could use a secondary.

    I'm just brainstorming here. There could be a more elegant way to do it, but the graphic masking that already exist is the only thing I can think of right now.

    Eric

  • Several feature selection to row in the table

    Hello

    I have a group with the style of layout table and I want to set the the "multiple" generated table rowSelection property but it seems there is no way to do unless you are using the group as LOV, which is not desirable for me to JHs. The following code shows how to set this property in tableGroup.vm:

    #if (! ($JHS.current.group.useAsLov & & $JHS.current.group.multiSelect))
    selectionListener = "#{#BINDINGS_TABLE (). collectionModel.makeCurrent}.
    rowSelection = 'single '.
    #if (! $JHS.current.group.useAsLov)
    selectedRowKeys = ' #{#TABLE_BEAN () .selectedRow} ".
    #end
    #else
    rowSelection = "multiple".
    selectedRowKeys = ' #{#LOV_PAGE_BEAN () .selectedRowKeySet} ".
    selectionListener = "#{#LOV_PAGE_BEAN () .selectionListener}".
    #end

    Is there a reason for not allowing to have several feature selection of line in the table when it is not in mode LOV?

    Thank you
    Will do

    Will do,

    No, but if you want to have multiple selection, there is typically a custom action that you want to apply to selected lines, which cannot be defined in the Jheadstart Application definition editor.
    However, it is perfectly well use a custom template tableGroup.vm and allow multiple selection.

    Steven Davelaar,
    JHeadstart team.

  • Independent LOV in each row of the table

    Hello

    I use Jdev 12 c, my requirement is, I want to ask a LOV a table in every row and want to get its value in my grain of java against the selected line. Snapshot is attached for clarity.

    acceptReject.PNG

    Help, please

    Kind regards

    Crusher

    Add a transitional attribute to the object of the entity that holds the value selected from the lov.

    Create a static view object to contain possible values to select for the lov. Then, build a model piloted by lov on the transitional attribute using the static vo.

    An example of this approach is presented here https://tompeez.wordpress.com/2013/04/05/using-one-viewobject-for-global-lookup-data/

    It's using a global table to get the data to search instead of a static vo.

    Timo

Maybe you are looking for

  • Official 6 Android application

    Hi everyone please comment for Motorola Moto Know X 1 st Gen need to Upgrade for 6 Android, Samsung get upgrade 6 Android Galaxy S4 Moto X 1 St gen even have 2 GB of ram and so much better, why don't have 6 Android official, please hit, each a blow f

  • FLEXlm License Finder error for VideoMaster and AudioMaster works on remote server

    I get the error of FLEXlm license Finder for my VM and I am running on a remote server; This is as described in the following link: http://digital.ni.com/public.nsf/allkb/15BEB6A00E1E0C418625757F00548827 I can then search for license files and select

  • Windows (7, SP1) update stuck looking for updates! Help, please!

    My laptop Windows 7 SP1 is stuck updates. I tried many methods, including: Reset Windows Update. Empty the cache. Troubleshooting of Windows Update. Microsoft Fixit. With the help of CCleaner. Using method of command line to rename files (I think it

  • ASA CX to the firepower Upgrade Kit - required SSD?

    We have a customer who uses the CX software on a pair of ASA HA 5512-x they want to move to the IPS of firepower. Is there an upgrade SKU (ASA5512-FP-UPG) which is an upgrade kit. Through CCW, when you customize the options on this Reference, you're

  • Cannot use my legitimate activation key.

    I bought a windows upgrade 7 years through reduction of the student. Since then, I have bought new pieces for my computer and had to format due to windows not being able to start new post part installation (I called support, no way around it). Howeve