SQL 1-n, n - n, due to other values in the column

Hello

I have the following output:

STATUS, ID_RESSOURCE, PERIOD_ID, DATE_FROM, DATE_UNTIL

ERROR 45 2576 27.02.2014 27.02.2014 15:00 19:00

ERROR 45 2575 27.02.2014 27.02.2014 15:00 19:00

45-2552 27.02.2014 OK 27.02.2014 08:50, 14:30

ERROR 45 2544 27.02.2014 22:40 28.02.2014 04:10

ERROR 45 2536 27.02.2014 22:40 28.02.2014 04:10

What I need is:

STATUS, ID_RESSOURCE, PERIOD_ID, DATE_FROM, DATE_UNTIL

ERROR 45_2 2576 27.02.2014 27.02.2014 15:00 19:00

ERROR 45_1 2575 27.02.2014 27.02.2014 15:00 19:00

OK 45_1 2552 27.02.2014 27.02.2014 08:50, 14:30

ERROR 45_2 2544 27.02.2014 22:40 28.02.2014 04:10

ERROR 45_1 2536 27.02.2014 22:40 28.02.2014 04:10

Whenever the STATUS column has the 'ERROR' val, a new id_ressource to be generated.

I don't have a lot of new RESOURCE_IDs as I overlap of time periods.

For example must have another line like this:

ERROR 45 2578 27.02.2014 27.02.2014 12:00 18:00

It should then look like this:

ERROR 45_2 2576 27.02.2014 27.02.2014 15:00 19:00

ERROR 45_1 2575 27.02.2014 27.02.2014 15:00 19:00

ERROR 45_3 2578 27.02.2014 27.02.2014 12:00 18:00

OK 45_1 2552 27.02.2014 27.02.2014 08:50, 14:30

ERROR 45_2 2544 27.02.2014 22:40 28.02.2014 04:10

ERROR 45_1 2536 27.02.2014 22:40 28.02.2014 04:10

It's source code:

CREATE TABLE MY_TABLE 
(
  STATUS VARCHAR2(20) 
, RESOURCE_ID NUMBER 
, PERIOD_ID NUMBER 
, DATE_FROM DATE 
, DATE_UNTIL DATE 
);

Insert into MY_TABLE (STATUS,RESOURCE_ID,PERIOD_ID,DATE_FROM,DATE_UNTIL) values ('ERROR','45','2576',to_date('27.02.2014 15:00','DD.MM.YYYY HH24:MI'),to_date('27.02.2014 19:00','DD.MM.YYYY HH24:MI'));
Insert into MY_TABLE (STATUS,RESOURCE_ID,PERIOD_ID,DATE_FROM,DATE_UNTIL) values ('ERROR','45','2575',to_date('27.02.2014 15:00','DD.MM.YYYY HH24:MI'),to_date('27.02.2014 19:00','DD.MM.YYYY HH24:MI'));
Insert into MY_TABLE (STATUS,RESOURCE_ID,PERIOD_ID,DATE_FROM,DATE_UNTIL) values ('OK','45','2552',to_date('27.02.2014 08:50','DD.MM.YYYY HH24:MI'),to_date('27.02.2014 14:30','DD.MM.YYYY HH24:MI'));
Insert into MY_TABLE (STATUS,RESOURCE_ID,PERIOD_ID,DATE_FROM,DATE_UNTIL) values ('ERROR','45','2544',to_date('27.02.2014 22:40','DD.MM.YYYY HH24:MI'),to_date('28.02.2014 04:10','DD.MM.YYYY HH24:MI'));
Insert into MY_TABLE (STATUS,RESOURCE_ID,PERIOD_ID,DATE_FROM,DATE_UNTIL) values ('ERROR','45','2536',to_date('27.02.2014 22:40','DD.MM.YYYY HH24:MI'),to_date('28.02.2014 04:10','DD.MM.YYYY HH24:MI'));

I hope someone can help.

Thanks in advance.

Tobias Cheer

If I have two periods that overlap, I need to create two virtual resources / lines.

In case the third period is the first period, then they must stay next to each other.

OK, understood.

Here's a different approach, using the TYPE clause:

SQL > select status

2, id_ressource | '_' ||

3 row_number() over (partition by group_id order date_from) as id_ressource

4, period_id

5, date_from

6, date_until

7 of)

8 select

9 from my_table

model 10

11 partition by (id_ressource)

size 12 by (row_number() (order date_from) as I)

13 steps (status, date_from, date_until, period_id, 1 as group_id)

(14) rules

group_id 15 [I > 1] = case when date_from [cv ()]<>

16 then group_id [cv () - 1]

17 cv (i) else

18                        end

19)

(20);

STATUS ID_RESSOURCE PERIOD_ID DATE_FROM DATE_UNTIL

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

ERROR 45_1 2222 28.02.2014 07:33 28.02.2014 09:30

ERROR 45_2 3333 28.02.2014 28.02.2014 08:30, 14:40

ERROR 45_3 2552 28.02.2014 09:00 28.02.2014 14:40

ERROR 45_1 2553 28.02.2014 13:20 28.02.2014 19:00

ERROR 45_2 2575 28.02.2014 18:50 28.02.2014 21:00

OK 45_1 2578 28.02.2014 28.02.2014 21:30 22:00

ERROR 45_1 2576 28.02.2014 01.03.2014 22:30 01:30

ERROR 45_2 2536 01.03.2014 01.03.2014 01:00 03:00

OK 45_1 2544 01.03.2014 03:06 01.03.2014 05:50

9 selected lines.

Each line is tested for overlap with the first line of the group in order DATE_FROM.

If the test is positive, the line is assigned the same GROUP_ID, otherwise we start again with a new ID initialized from its current position (size I).

When this is done, we just need to use the ROW_NUMBER function to calculate a sequential number for each row in each group.

Tags: Database

Similar Questions

  • A combine with other values of the column, the column values


    Hi all

    I need your help on this. I have a req I need to combine values of a percentage to the values of column test column. Here are examples of data

    Select decode('test',null,'ABC') abc, b.percent, b.amount

    from tableA, tableB b

    where id = b.id

    Examples of data

    Amount of the percentage TEST

    ABC              0.3                10

    ABC              0.5                20

    ABC              0.2                30

    After him combine the values of percentage to the ABC percentage column values must be zero

    Desired output

    Amount of the percentage TEST

    ABC(30%)                              10

    ABC(50%)                              20

    ABC(20%)                              30

    Valuable suggestions are appreciated

    Alter the query from Etbin with a trifle for the exact release.

    with

    t as

    (select 'ABC' test, 0.3 percent, 10 amount of all the double union

    Select 'ABC', 0.5, 20 double Union all

    Select 'ABC', 0.2, 30 double

    )

    Select test | ' ('| to_char(100 * percent) |'%)' test, null as a percentage, the amount

    t

  • How can I replace the value of the column with a particular value in SQL

    Hi all

    Is someone can you please tell me how can I format my output with the replacement of a column value with a specific value that really depends on the current value of the column

    I am executing the following SQL statement

    Select state, count (id) from < table_name > where composite_dn = < composite_dn_name > Group by State;
    My current performance is:

    State Instance number
    1 3
    3 28

    I want to replace the value in the State column as follows

    No.OfInstances State
    3 filled
    28 faulted

    I want '1' to be reppaced of 'Done' and '3' is replaced by 'Faulted. '

    Is is possible with SQL or PL/SQL, if it is then how I can achieve this required. Help, please!

    Thanks in advance!

    Published by: Roshni Shankar on October 27, 2012 12:38 AM

    Hi Claude,.

    I guess this CASE clause can be simulated by a DECODE and also it is very easy to use.
    See - http://www.techonthenet.com/oracle/functions/decode.php

    select decode(t1.state,t2.state_id,t2.state_name), t1.count_id
    from  t2, (select state,count(id) count_id
     from 
     where composite_dn= 
    group by state) t1
    where t1.state = t2.state_id;
    

    HTH
    Vanessa B.

    Published by: Vanessa B on October 27, 2012 14:02
    -link added

    Published by: Vanessa B on October 27, 2012 14:19
    -added code sample

  • request to retrieve the value of the column and use in the other rows of the table

    Hi all

    I have a table where I maintain pairs name / value of a particular job.  Now, I would like to see some of the values of the rows passed to other rows in a table for example

    create job_name_value

    (

    VARCHAR2 (100) job_name,.

    name varchar2 (100),

    value varchar2 (100)

    );

    job_name name value
    TRGJOB% FPATH/ USR/LOCAL/TRGJOB
    TRGJOB% FDATECALDATE + 1
    TRGJOB$FNAME1%%FPATH.mywatchfile1.%FDATE
    TRGJOBFNAME2%%FPATH.mywathcfile2.%FDATE
    TRGJOB% FNAME3%FPATH.mywatchfile3.%FDATE

    can we write a sql query to produce output as below the objective is to use the value of fdate and fpath in the remaining lines. If there is no fdate and fpath then value should return such what or we need to use the stored procedure to achieve

    / usr/local/trgjob/mywatchfile1. CALDATE + 1

    / usr/local/trgjob/mywatchfile2. CALDATE + 1

    / usr/local/trgjob/mywatchfile3. CALDATE + 1

    Hello

    Since it cannot be '% FDATTE' or '% FPATH' (or both) then you need to replace each of them separately.  If the other does not, there 'replacement' will do nothing, but this will not affect the result of the other.

    Here's a way to do it:

    SELECT REPLACE (REPLACE (n.value

    '% FPATH.'

    p.value | '/'

    )

    , '% FDATE '.

    d.value

    ) AS output_value

    OF job_name_value n

    LEFT OUTER JOIN job_name_value p ON p.name = '% f '.

    LEFT OUTER JOIN job_name_value d ON p.name = '% FDATE '.

    WHERE n.name AS '\%FNAME%' ESCAPE ' \'

    ;

    I hope that answers your question.

    If this isn't the case, please post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and also publish outcomes from these data.

    Report when the above query is to give erroneous results and explain, using specific examples, how to get the correct results of data provided in these places.  If you change the query at all, your postal code.

    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#9362002

  • SQL query to retrieve all instances of a model within the column

    I had an obligation to build a query for the scenario below. Pls help to troubleshoot.

    Scenario.
    Table 1 has two columns < number > col1 and col2 < CLOB >
    The table has 2 rows

    < Number > col1 has the following values
    Row1-col1
    1

    Row2-col1
    2

    Col2 < Clob > has the following values

    Row1-col2
    blah blah blah blah blah blah blah A1235 A1234
    blah blah blah blah blah blah blah A1237 A1236

    Col2 Row2
    blah blah blah blah blah blah blah A1239 A1238


    The query should return (expected result)
    A1234
    A1235
    A1236
    A1237
    A1238
    A1239

    I tried with the substr, but it returns only the first occurrence of the pattern

    Query:
    Select
    REGEXP_SUBSTR (col2,
    "A [0-9] {4}') 'REGEXP_SUBSTR'"
    FROM table1


    Output:
    A1234
    A1238


    I want a loop must be created and check until the end of the column.
    Pls help me with the query. It will be useful.

    Thank you
    Nanan

    Published by: 950268 on August 1, 2012 07:19

    You can simulate loops with sql functions as the clause-type (since 10 g) or recursive subquery (since 11.2).
    Generally in this forum link it by the approach will be proposed.
    The depth will be the number of occurrences of your model and the level will be the real occurency.
    So just search inthi forum for an abundance of examples

    select regexp_substr(col2, 'A[0-9]{4}', 1, level)
    from tabel1
    connect by
    level <= regexp_count(col2,'A[0-9]{4}')
    

    regexp_count since 11. ?

    concerning

  • Reset the other values in the list of selection when it is clicked on a selection list.

    I'm currently developing a report based on various selection lists. I have three selection lists. When I select a value in that I want them to have the value zero. Like that when I select my button GB the report displays only the information related to the value in the selection list, I clicked. (I've already worked my report based on a function, plsql referring to different queries depending on the valuses of selection lists and he needs 2 of the three to 0).

    Thank you
    Greg

    Greg,

    I've made a few changes (no worries, you can export your application as x min there is to get him back if you like):
    1. I have modified your reports to simple sql queries and put logic in the where clause.
    2. I changed the first to the elements to make them more standard.

    Play with the page a bit and I would like to know if it's on the right track...

    Kind regards
    Dan

    http://danielmcghan.us
    http://sourceforge.NET/projects/tapigen
    http://sourceforge.NET/projects/plrecur

    You can reward this answer by marking as being useful or correct ;-)

  • minimum partition value to determine the value of the column in the other column

    Hello

    Hope that the title is logical!

    I'm having a brain freeze. I have my table "temp". I want to do is add an additional column that contains the value 'Act' which corresponds to the minimum value of 't' for each partition "emp". My sql adds a column with the value of 't' ('min_t'), but what I really want, the corresponding value of "Act." Could someone advise me please how to do this?

    Thanks in advance

    Jon
    with temp as
    (
      select 'Jim' emp, 1 t, 'NR' act from dual union all
      select 'Jim' , 2 , 'NR' from dual union all
      select 'Jim' , 3 , 'NR' from dual union all
      select 'Bob' , 3 , 'Aff' from dual union all
      select 'Bob' , 4, 'NR' from dual union all
      select 'Bob' , 5 , 'NR' from dual union all
      select 'John' , 7 , 'NR' from dual union all
      select 'John' , 8 , 'Aff' from dual union all
      select 'John' , 9 , 'Aff' from dual
    )
    select temp.*, min (t) over (partition by emp)  min_t
    from temp

    Is that what you are looking for?

    SELECT emp
         , t
         , MIN(act) KEEP (DENSE_RANK FIRST ORDER BY t) OVER (PARTITION BY emp) AS min_t
    FROM   temp
    ;
    

    That gives these results:

    EMP                     T MIN
    ---- -------------------- ---
    Bob                     4 Aff
    Bob                     5 Aff
    Bob                     3 Aff
    Jim                     3 NR
    Jim                     2 NR
    Jim                     1 NR
    John                    7 NR
    John                    9 NR
    John                    8 NR
    
  • SQL question - aggregate rows based on the value of the columns

    Hi all

    DBA_EXTENTS reports on the number of starting point of measurement (column BLOCK_ID) and the size of the scale in the Oracle (BLOCKS column) blocks.

    I'm trying to combine the lines of the segments of same as if their associated extensions are adjacent.

    For example, assume that the data in the view are as follows:

    Nom_segment Block_id BLOCKS
    EMP108
    EMP188
    DEPARTMENTS2616
    DEPARTMENTS428

    EMP

    5016EMP668

    And I want to do a set of results that looks like this:

    Nom_segment Block_id BLOCKS
    EMP1016
    DEPARTMENTS2624
    EMP5016

    I thought to use an analytical query, but I couldn't find something that meets this requirement.

    Can you please help?

    Hello

    Here's a way to do that using the analytical SUM function:

    WITH got_grp AS

    (

    SELECT nom_segment, block_id, blocks

    , block_id - SUM (blocks) over (PARTITION BY nom_segment

    ORDER BY block_id

    ) AS the grp

    FROM the data

    )

    SELECT nom_segment

    MIN (block_id) AS grp_block_id

    SUM (blocks) AS grp_blocks

    OF got_grp

    GROUP BY nom_segment, grp

    ORDER BY grp_block_id

    ;

    The data you posted in response to #3:

    Pini Dibask wrote:

    Thanks Frank,.

    If you would care to post CREATE TABLE and INSERT to your sample data and the version of your database, then I could show you exactly.

    The Oracle version is 11.2.0.4

    This is the CREATE TABLE and INSERT statements for the sample data:

    CREATE THE TABLE DATA (NOM_SEGMENT VARCHAR2 (20), BLOCK_ID NUMBER, NUMBER OF BLOCKS);

    INSERT IN DATA VALUES ("EMP", 10, 8);

    INSERT IN DATA VALUES ("EMP", 18: 8);

    INSERT IN DATA VALUES ("DEPARTMENTS", 28, 16);

    INSERT IN DATA VALUES ("DEPARTMENTS", 42, 8);

    INSERT IN DATA VALUES ("EMP", 50, 8);

    INSERT IN DATA VALUES ("EMP", 66, 8);

    is not the same as the data in your first post and the results are not the same, either.

    The fixed difference technique involves lines don't overlap.  In the sample data in response #3, 'DEPARTMENTS' lines overlap, i.e. blocks 42 and 43 belong to two segments.

  • How to render a table on the value of the column in the other table. in the adf.

    I have an A.It table have a column b column B have value is xxx then table C will go to true.any you suggest how to achieve this.
    I use jdev 10.1.3.3

    Please suggest
    Thank you
    Emine

    Published by: vinay kumar on March 27, 2009 18:51

    You can use in the detail of the line?

    In this case, put the af:table component in the af: column and assess the condition of rendering on the value of the column.

    Tocarli

  • Please help - value of the long column of SQL report required in a separate cell

    Hello.

    I need to create a SQL report with the structure as below:

    Country city vertical Business details for the State (detail desc)
    MY Quincy Healthcare US
    It is a detailed desc value that is displayed in a different cell.
    It is a detailed desc value that is displayed in a different cell.
    It is a detailed desc value that is displayed in a different cell.
    It is a detailed desc value that is displayed in a different cell.
    US MY Braintree, HE
    It is a detailed desc value that is displayed in a different cell.
    It is a detailed desc value that is displayed in a different cell.
    It is a detailed desc value that is displayed in a different cell.

    I came across a solution where you can hide/show the column value in another cell. But my requirement is that it should be displayed when the page opens, and the user will not have to click a button to see the value. This is mainly for their presentations, and printing. so its very annoying for them to click on the buttons so much to see the value of desc of detail for each record. I'm new to Java script, please help

    I'm not sure to understand your needs - you are showing us 2 data lines, lines 7 or 9? You want the long description of detail on the same line, or below?

    I'll go on the assumption that you are showing us two rows, and long descriptions in this example are found along 3 to 4 lines, and you want long descriptions under the details. In this case, I suggest using "column name" models They are much easier to use as they appear when you look at everything first, so bear with me.

    Under 'shared components', select 'Models' and click then on create yellow > button. You create a report model, from scratch. On the last page of the wizard, select "Named column (model line)" for the type of model. This will create a model rather generic (and useless), which you'll have to edit; I don't know why they chose to not send you directly in the editor. When you change it, you will see that the single field whose value is 'Model line 1', and it is set to:

    #1##2##3##4##5#
    

    To meet your needs with basic features, change it to something in the sense of:

    #COUNTRY##STATE##CITY##VERT#
    #DETAILS#
    

    Then, you probably want to put something in the position of the column model, such as:

    CountryStateCityVertical Business
    Details
    

    Applies to whatever you want, of course the CSS styles.

    Using the model is simple: If your report, select the model and make sure that your SQL returns values using the column names specified in your model. Otherwise, you will see "#COUNTRY #" literal text in your report.

    You can get fancy with these, of course, as the application of different styles for alternating rows, but I'll leave that as pretty good to help you get started (I hope).

    -David

  • SQL statement to retrieve value from the attribute using XPATH

    Hi all

    I think this is the right place to post this request since it is related to oracle and xpath.

    I'm writing a sql statement of xpath that will extract the value of the column attribute is what type of clob data. Here is the xml example (xml_content is the column name)



    < RAFSCREEN >
    < Home >
    < clientnumber reference = "123123" >
    < / servant >
    < RAFSCREEN >



    I need to output query should return 123123.
    I'm using query

    Select extract (xmltype (xml_content), 'RAFSCREEN, Domestic, @clientnumber') as clientnumber from table_name

    It will be very useful if someone answer this question.
    Thank you

    It is Possible...

    10g> with form_content
      2  as
      3  (
      4  select 212 form_content_id , '    ' xml_content from dual union all
      5  select 222 , '    '   from dual union all
      6  select 223 , '    '   from dual union all
      7  select 224 , '    '   from dual )
      8  select form_content_id, extractValue(xmltype(xml_content),'/RAF/XBorderRAF/ReviewReferences/@clientLegalName') as clientnumber
      9  from form_content ;
    
    FORM_CONTENT_ID CLIENTNUMBER
    --------------- --------------------------------------------------------------------------------------------------------------------------------------------
    212      ABC123
    222      ZY123
    223      IN123
    224      NL123
    
    4 rows selected.
    

    Your request (you need)

    select form_content_id, extractValue(xmltype(xml_content),'/RAF/XBorderRAF/ReviewReferences/@clientLegalName') as clientnumber
    from form_content ;
    
  • The SQL server system configuration checker cannot be executed due to WMI configuration on the computer

    The SQL server system configuration checker cannot be executed due to WMI configuration on the computer

     
    Toshiba-User Error: 2147749896 (0 x 80041008)
     
    I am trying to load the software for insurance. I got the above message.

    Hello

    The question you posted would be better suited in the TechNet Forums. I would recommend posting your query in the TechNet Forums.


    The SQL Server installation & set program to update Forum

    http://social.msdn.Microsoft.com/forums/en/sqlsetupandupgrade/thread/3f313abe-8826-4d76-8F04-611ebd4d0141

  • My installation process keeps failing due to "other methods" that are underway? But I closed everything...?

    My installation process keeps failing due to "other methods" that are underway? But I closed everything...?

    You can close these processes of activity monitor: -.

    • Creative cloud
    • Adobe CEF Helper
    • Service Office of Adobe
    • AdobeIPCBroker
    • Installation of Adobe
    • AdobeUpdateService
    • AdobeServiceInstaller
    • CreativeCloud (URIHandler)
    • AAM updates notifier
    • CCLibrary
    • node
    • CoreSync

    Also not restart your computer before the installation.

  • D1G64UA: Windows 10 update fails due to no space in the recovery Partition

    Try to upgrade Windows to Windows 10 8.1 upgrade fails due to insufficient space in the recovery on the D: drive Partition

    The size of the recovery Partition is configured at installation of the HP factory and it leaves only about 3 GB, which is insufficient for the upgrade of Windows 10 start the upgrade begins by creating files in the recovery Partition.

    I don't want to remove the Windows 8.1 recovery files that already exist on the D: Partition of Recovery of the construction of the plant and in fact, I don't think that the o/s will allow me to delete these files from recovery.

    Any help in how to run the upgrade of Windows 10 is appreciated.

    Glad to see that my post has solved your problem.

    Please click "Accept as Solution" to my post so that others will see that this thread has been resolved.

    Thank you

  • How to do this: Photoviewer, Windows cannot save changes to the image due to a problem with the properties of the file!

    Hi, I'm trying to post a photo, but whenever I have try rotation to the right, it says something like: can't save changes to the image due to a problem with the properties of the file. My laptop is MS, but the photos were taken on the Iphone. How to do this?

    I don't know if the following links answer your question, but
    they may be worth a visit:

    Good luck and * proceed at your own risk *.

    FWIW... There is a very long discussion (several pages) to the
    following link:

    Cannot change the imported iPhone 4S photo
    https://discussions.Apple.com/thread/3444951?start=0&TSTART=0

    More Discussion

    iPhone 4S image properties is not compartible with Windows?
    Editing can be done on the computer due to the error
    "Windows Photo Viewer cannot save changes to this picture.
    because there is a problem with the properties of the image file".
    Any help?
    https://discussions.Apple.com/message/17291884#17291884

    More Discussion:

    We cannot turn a few photos in Windows 7 (but can turn others)
    http://answers.Microsoft.com/en-us/Windows/Forum/Windows_7-pictures/cant-rotate-some-photos-in-Windows-7-but-can/3a809845-9144-4BB1-9bb3-ab4f5b856524

    A simple solution is to open the photos in paint or most other
    digital image editor and modify it.

    Some people have reported success using the following free download
    to remove the metadata.

    Property restrictions stripper iphone 4S (JPEG & PNG Stripper)
    http://www.SteelBytes.com/?mid=30&cmd=download&PID=15

    Here's a way to turn...

    Easily turn locked iPhone 4 and 4 s Photos in Windows 7 with
    the rotator Lossless JPEG free
    http://www.daleisphere.com/easily-rotate-locked-iPhone-4-and-4S-photos-in-Windows-7-with-the-free-JPEG-lossless-Rotator/

    Free download JPEG Lossless rotating
    http://annystudio.com/software/jpeglosslessrotator/

Maybe you are looking for