Build SQL query

Hello

I have the following query, which returns the active requests created and closed per year. The question is that I need to accumulate the active requests last year to next year, created, in the end, the assets must be 0. Can anyone help?

Query:

SELECT ANO, ABERTOS AS ABERTOS, ONLY, ACTIVOS
Of
(SELECT NEW. ANO AS ANO, ABERTOS, ONLY, ABERTOS-ONLY AS ACTIVOS
(SELECT CAL. AS ANO ANO, / * CRIAÇÃO. KEY_CATALOGO, * / COUNT (*) AS ABERTOS
of dm_facto_t CRIAÇÃO
LEFT JOIN DIM_CALENDARIO_T CAL
ON CRIAÇÃO. KEY_CALENDARIO = CAL. KEY_CALENDARIO
where key_tipo_facto = 17
GROUP OF CAL. -ANO, CRIAÇÃO. KEY_CATALOGO
ORDER BY CAL. NEW YEAR CSA)
LEFT JOIN)
SELECT CAL. ANO, / * FECHO. KEY_CATALOGO, * / COUNT (*) AS ONLY
of dm_facto_t FECHO
LEFT JOIN DIM_CALENDARIO_T CAL
ON FECHO. KEY_CALENDARIO = CAL. KEY_CALENDARIO
where key_tipo_facto = 18
GROUP OF CAL. -ANO, FECHO. KEY_CATALOGO
ORDER BY CAL. FECHOS ANO CSA)
ON NEW. ANO = FECHOS. ANO);

Result


Year active closed established
2008 1477 1452 25
2009 1933 1684 249
4270 4187 2010 - 83
4086 3895 2011 - 191

Thank you!
with t as ( select 2008 yr, 25 active from dual union all
                select 2009, 249 from dual union all
                select 2010, -83 from dual union all
                select 2011, -191 from dual )
  select t.*, sum(active) over (order by yr) running_total  from t
YR     ACTIVE     RUNNING_TOTAL
2008     25     25
2009     249     274
2010     -83     191
2011     -191     0

Tags: Database

Similar Questions

  • Need help to build the query/pl-sql block to get the query result and the name of column from DB table in the form of key-value pairs.

    Hi Experts,

    I have a DB table has columns of more than 50.

    I question this table, it should only return one line at any time. as sqldeveloper below image.

    here, I need to build block pl/sql-query, Discover the column in the table as a key and query result as values.

    Eg:     Key                         -  Value

    TASK_EVENT_ID - 1765

    EVENT_TYPE - ASR_UPDATE

    ... etc until all of the columns in my table.

    Experts please comment on that point, appreciate your help on this.

    Thank you

    -Vincent.

    Here is an approach using DBMS_SQL to iterate over the columns of key / value to assign... (Little code snipped for brevity)

    create or replace procedure (task_expired)

    v_store_id in full,

    v_task_action_id in full,

    v_job_id in full

    )

    as

    -[SNIP code...]

    v_sql VARCHAR2 (4000): = ' select * from my_table where PK = 123';  -Your SQL here!

    v_v_val VARCHAR2 (4000);

    v_n_val NUMBER;

    v_d_val DATE;

    v_ret NUMBER;

    c NUMBER;

    d NUMBER;

    col_cnt INTEGER.

    f BOOLEAN;

    rec_tab DBMS_SQL. DESC_TAB;

    col_num NUMBER;

    vAsString VARCHAR2 (4000);

    BEGIN

    -[SNIP code...]

    Message_properties. CORRELATION: = "EDF_EVENT";

    MSG: = SYS. AQ$ _JMS_BYTES_MESSAGE. Construct();

    Msg.set_string_property ('queueName', ' shipping/csi_cth');

    Msg.set_string_property ('MODE', 'CR8');

    c: = DBMS_SQL. OPEN_CURSOR;

    DBMS_SQL. PARSE (c, v_sql, DBMS_SQL. NATIVE);

    d: = DBMS_SQL. Execute (c);

    DBMS_SQL. DESCRIBE_COLUMNS (c, col_cnt, rec_tab);

    1.col_cnt J

    LOOP

    CASE rec_tab (j) .col_type

    WHEN 2 THEN

    DBMS_SQL. DEFINE_COLUMN (c, j, v_n_val);      -Number

    WHEN 12 CAN

    DBMS_SQL. DEFINE_COLUMN (c, j, v_d_val);      -Date

    ON THE OTHER

    DBMS_SQL. DEFINE_COLUMN (c, j, v_v_val, 2000);   -Else treat as varchar2

    END CASE;

    END LOOP;

    LOOP

    v_ret: = DBMS_SQL. FETCH_ROWS (c);

    WHEN OUTPUT v_ret = 0;

    1.col_cnt J

    LOOP

    -Fetch each column to the correct data type based on coltype

    CASE rec_tab (j) .col_type

    WHEN 2 THEN

    DBMS_SQL. COLUMN_VALUE (c, j, v_n_val);

    vAsString: = to_char (v_n_val);

    WHEN 12 CAN

    DBMS_SQL. COLUMN_VALUE (c, j, v_d_val);

    vAsString: = to_char (v_d_val, ' DD/MM/YYYY HH24:MI:SS');

    ON THE OTHER

    DBMS_SQL. COLUMN_VALUE (c, j, v_v_val);

    vAsString: = v_v_val;

    END CASE;

    Msg.set_string_property (rec_tab (j) .col_name, vAsString);

    END LOOP;

    END LOOP;

    DBMS_SQL. CLOSE_CURSOR (c);

    DBMS_AQ. ENQUEUE (queue_name-online 'cbus.aqjms_common',

    Enqueue_options => Enqueue_options,

    Message_properties => Message_properties,

    Payload-online msg,

    Msgid => Message_handle);

    dbms_output.put_line ('00 Msgid =' |) Message_handle);

    dbms_output.put_line('===Done=');

    -[SNIP code...]

    END;

    /

  • PL Sql Query Builder Tool

    Hello

    Is there any Pl/Sql Query Builder tool / software that can make your life easier.

    best regards

    Sandy

    SPathak wrote:

    Hello

    I agree, that oracle Pl/Sql is powerful and gives a correct result.

    little time to create a complex query with assistant was looking for easy way.

    Sandy

    You will not learn using a GUI like TOAD tool (and I LOVE TOAD).  I always write by hand in the editor, instead of drag / drop regardless of what type of gui tool.  Only, I do not use the constructors unless you are a business analyst who has no experience of SQL.

  • 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)

  • SQL query to view all build profiles, properties and values.

    I am trying to create a SQL query to retrieve all our profiles of construction, their properties and their values. The output should include the build profile name, the name of the property and the property value. I found the following tables and just need to understand the appropriate query. I still think that it is a kind of JOIN.

    SELECT [Id]

    [EntityID]

    [PropertyName]

    [Valeurpropriete]

    [IsHidden]

    [IsRuntime]

    [IsEncrypted]

    FROM [VCAC_DB]. [dbo]. [GlobalProfileProperties]

    SELECT [GlobalProfileID]

    [ProfileName]

    [Description]

    FROM [VCAC_DB]. [dbo]. [GlobalProfiles]

    Does that help?

    SELECT GlobalProfiles.ProfileName, GlobalProfileProperties.PropertyName, GlobalProfileProperties.PropertyValue

    OF GlobalProfiles

    INNER JOIN GlobalProfileProperties ON GlobalProfiles.GlobalProfileID = GlobalProfileProperties.EntityID

    I also like this because it gives all the details on the individual custom properties in a profile of construction (such as IsHidden, which tells you if the DEM will treat the property):

    SELECT GlobalProfiles.ProfileName, GlobalProfileProperties.*

    OF GlobalProfiles

    INNER JOIN GlobalProfileProperties ON GlobalProfiles.GlobalProfileID = GlobalProfileProperties.EntityID

  • The Web-based SQL Query Builder

    Hello

    I have a generator of SQL query that allows users to create their own application graphically.

    I have something I could use to do?

    Thanks a lot for your help
    Jko

    Hello

    I hope this help you start your applications to Query Builder
    http://HTMLDB.Oracle.com/pls/OTN/f?p=18326:7:1636417102512483:P7_ID:161

    BR, Jari

    Edit:

    And this
    http://HTMLDB.Oracle.com/pls/OTN/f?p=18326:7:1636417102512483:P7_ID:1222

    Published by: jarola October 29, 2009 12:30 AM

  • How to build a SQL query to print ranges of week of a month

    Hi gurus!

    I have a requirement to create a query to display the times of week of a month.
    The entries will be Year and month.
    The result should be something like this:

    If the year = 2009 and month = June

    The SQL query should return the following:

    June 1, 2009-June 6, 2009
    June 7, 2009 to June 13, 2009
    June 14, 2009-June 20, 2009
    June 21, 2009-June 27, 2009
    June 27, 2009 to June 30, 2009

    I'm confused how to proceed. Please help me. Any help would be appreciated grately.

    Thank you
    Ahmed

    Hello

    This might help (the exit is not exactly the same):

    Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0
    Connected as hr
    
    SQL>
    SQL> SELECT TRUNC(to_date('01-Jun-2009'),
      2               'D') + 7 * (LEVEL - 1) AS START_DATE,
      3         TRUNC(to_date('01-Jun-2009'),
      4               'D') + 7 * (LEVEL - 1) + 6 AS END_DATE
      5    FROM dual
      6  CONNECT BY LEVEL <= 4;
    
    START_DATE  END_DATE
    ----------- -----------
    31/05/2009  06/06/2009
    07/06/2009  13/06/2009
    14/06/2009  20/06/2009
    21/06/2009  27/06/2009
    
    SQL> 
    

    Kind regards

  • Dependencies of object database by using the SQL query?

    Hi guys,.

    I just see the nice feature to discover the database object dependencies in the APEX (generator application-> app 123-> utilities-> database object dependencies).

    Is there a way to get this dependencies just with a SQL query? Also should I calculate a process in order to get the current dependencies? In APEX, you press a calculate button before you can see the dependencies.

    Thanks in advance and greetings from the Germany

    Steven

    After you import the app Builder application APEX 4500, I saw that the Start button the following process:

    wwv_flow_theme_manager.find_object_dependencies (p_flow_id =>: fb_flow_id, p_page_id-online null);

    In my example I can't use it, only the APEX_050000 user has privileges to use, and you must grant the privileges of your schema, so if you want to use it inside your application. My application must be independent of the schema and the specific database.

    Also it is not recommended to use this procedure because it is one of the procedures not supported that are only used by oracle.

  • Clarification of the SQL query in 2 day + Guide APEX

    I worked through the Oracle Database Express Edition 2 day + Application Express Developer's Guide, and try to decipher the SQL query in Chapter 4 (building your app).

    The code is:

    SELECT d.DEPARTMENT_ID,

    d.DEPARTMENT_NAME,

    (select count (*) from oehr_employees where department_id = d.department_id)

    "Number of employees", he said.

    substr (e.first_name, 1, 1) |'. ' || Select 'Name Manager',

    c.COUNTRY_NAME 'place '.

    OEHR_DEPARTMENTS d,

    E OEHR_EMPLOYEES

    OEHR_LOCATIONS l,

    C OEHR_COUNTRIES

    WHERE d.LOCATION_ID = l.LOCATION_ID

    AND l.COUNTRY_ID = c.COUNTRY_ID

    AND e.department_id = d.DEPARTMENT_ID

    AND d.manager_id = e.employee_id

    AND instr (superior (d.department_name), superior (nvl (:P2_REPORT_SEARCH,d.department_name))) > 0)

    I don't know exactly what is happening in the last line. I think I understand what the different functions but I'm not clear on the use of the: P2_REPORT_SEARCH string.

    What does this string? This code simply checking that d.department_name isn't NA?

    I have SQL experience but am not very familiar with the Oracle PL/SQL implementation. Can someone please give me a brief breakdown that check is doing in the context of the overall query? The application seems to work even if the conditional statement is not included.

    Thank you.

    2899145 wrote:

    Thanks for the reply. I apologize if the information I added was incomplete. The code came from the day 2 + Application Express (version 4.2) Developer Guide.

    In the section 'your own Application of 4 Buuilding' https://docs.oracle.com/cd/E37097_01/doc.42/e35122/build_app.htm#TDPAX04000 , they describe the creation of a report

    page that includes the "manager_id" and 'location_id '. The SQL query, I pasted above extracted from the data in other tables to substitute the real 'name of the Manager' and 'rent '.

    for the corresponding ID values. It makes sense, and the part of the SQL query that explicitly doing this makes sense.

    However, given that the document is a guide for the development of the APEX, I guess the command:

    AND instr (upper (d.department_name), upper (nvl (:P2_REPORT_SEARCH,d.department_name))) > 0

    done something valuable, and I do not recognize what is exactly the value.

    From a practical point of view why would I need to include this conditional statement?  Which only added to the application?

    Looking at the guide in question, it is clear that the

    AND instr(upper(d.department_name),upper(nvl(:P2_REPORT_SEARCH,d.department_name)))>0
    

    the line is completely unnecessary in the context of this tutorial, and it can be removed. The search in the tutorial app page is implemented by using a report filter interactive rather than a P2_REPORT_SEARCH element, which does not seem to exist at all. (It's a quirk of the APEX that bind variable references to non-existent items are replaced with NULL silently rather than exceptions). I thought that perhaps it would be legacy code a version of the tutorial prior to the introduction of interactive reports at the APEX 3.1, but I can't find explicit instructions to create such an element of filter in the 3.0 tutorial. I guess it must have been automatically generated by the application wizard when you create a standard report page.

    If you do not want to see the effect he would have (as described in the post above), leave it in the source report, add a text element of P2_REPORT_SEARCH, and a button "submit" on page 2 and experimenting to find different values of the element and clicking on the submit button...

  • [Solved] VCenter SQL query eat CPU resources

    Does anyone else have a problem with an SQL query in the database of Vcenter eat lots of CPU resources?

    We noticed that most of the time a core processor is 100% used by the SQL Server, dig deeper, we found that it is always the same query runs over and over again:

    SELECT DISTINCT 
                             VPX_SCSI_LUN.UUID AS lunUuid, CASE VPX_HOST_BUS_ADAPTER.VPX_TYPE WHEN N'vim.host.BlockHba' THEN CAST(VPX_PSA_PATH.HOST_ID AS VARCHAR(255)) 
                             + '-' + VPX_SCSI_LUN.UUID WHEN N'vim.host.ParallelScsiHba' THEN CAST(VPX_PSA_PATH.HOST_ID AS VARCHAR(255)) + '-' + VPX_SCSI_LUN.UUID ELSE VPX_SCSI_LUN.UUID END AS scsiVolumeEntityId, 
                             VPX_DATASTORE.NAME AS datastore, VPXV_DS_LUN_INFO.DATASTORE_ID AS datastoreEntityId
    FROM            VPX_DATASTORE INNER JOIN
                             VPXV_DS_LUN_INFO INNER JOIN
                             VPX_PSA_ADAPTER INNER JOIN
                             VPX_PSA_PATH ON VPX_PSA_ADAPTER.HOST_ID = VPX_PSA_PATH.HOST_ID AND VPX_PSA_ADAPTER.KEY_VAL = VPX_PSA_PATH.ADAPTER_LINK_KEY INNER JOIN
                             VPX_PSA_DEVICE ON VPX_PSA_PATH.LUN_LINK_KEY = VPX_PSA_DEVICE.KEY_VAL AND VPX_PSA_PATH.HOST_ID = VPX_PSA_DEVICE.HOST_ID INNER JOIN
                             VPX_HOST_BUS_ADAPTER ON VPX_PSA_ADAPTER.HOST_ID = VPX_HOST_BUS_ADAPTER.HOST_ID AND VPX_PSA_ADAPTER.LINK_KEY = VPX_HOST_BUS_ADAPTER.KEY_VAL INNER JOIN
                             VPX_SCSI_LUN ON VPX_PSA_DEVICE.HOST_ID = VPX_SCSI_LUN.HOST_ID AND VPX_PSA_DEVICE.LINK_KEY = VPX_SCSI_LUN.KEY_VAL ON VPXV_DS_LUN_INFO.UUID = VPX_SCSI_LUN.UUID AND 
                             VPXV_DS_LUN_INFO.HOST_ID = VPX_PSA_PATH.HOST_ID ON VPX_DATASTORE.ID = VPXV_DS_LUN_INFO.DATASTORE_ID
    WHERE        (VPXV_DS_LUN_INFO.DATASTORE_ID IS NOT NULL)

    sp_who2
    SPID     Status     Login     HostName     BlkBy     DBName     Command     CPUTime     DiskIO     LastBatch     ProgramName     SPID     REQUESTID
    52        RUNNABLE                           NT AUTHORITY\SYSTEM     NIF-VCENTER02       .     nif-vcenter02_vcdb     SELECT               20273570     1436     10/20 12:38:26     Microsoft SQL Server JDBC Driver                   52        0

    Everyone has noticed this problem with Vcenter 5.5 Build 2183111 (update 2) and MSSQL 2012 SP2 + CU2?

    Who like a lot of information storage, perhaps associated refresh "Views of storage" in the vSphere client.

    For the mentioned tables, can you update the statistics on them and see if that helps?

  • Dynamic PL SQL query

    Hello

    I want to run and store the results of a dynamic sql query in a strored procedure.

    I get the following variables of the user running: v_column_names, v_table_name, v_col, v_value

    The query will be like:

    v_query: = 'SELECT'. v_column_names | ' A ' | v_table_name | ' WHERE ' | v_col |' = ' | v_value;

    for example v_column_names: = 'ACCOUNT_NUM, SYSTEM_ID, ENTITY_ID ';

    v_table_name: = 'ACCOUNT '.

    v_col: = 'COUNTRY_CODE ';

    v_value: = "USA";

    Here is what I tried

    DECLARE

    v_column_names VARCHAR (200): = 'ENTITY_ID, SYSTEM_ID, ACCOUNT_NUM;

    v_table_name VARCHAR (200): = 'ACCOUNT '.

    v_col VARCHAR (200): = 'COUNTRY_CODE ';

    v_value VARCHAR (200): = "'USA"';

    TYPE column_record () IS RENDERING

    VARCHAR2 (200) C1: = null;

    C2 VARCHAR2 (200): = null;

    C3 VARCHAR2 (200): = null

    );

    TYPE st_table IS TABLE OF column_record INDEX DIRECTORY.

    pk_table st_table;

    NUMBER of v1: = 1;

    BEGIN

    v_select: = 'SELECT'. v_column_names | ' A ' | v_table_name | ' WHERE ' | v_col |' = ' | v_value;

    EXECUTE IMMEDIATE v_select COLLECT LOOSE pk_table.

    I'm IN 1.pk_table.count LOOP

    DBMS_OUTPUT. Put_line (pk_table (v1). (C1);

    DBMS_OUTPUT. Put_line (pk_table (v1). (C2);

    DBMS_OUTPUT. Put_line (pk_table (v1). (C3);

    v1: = v1 + 1;

    END LOOP;

    END;

    The number of column names in v_column_names is dynamic, so I need a way to store the results and then manipulate them.

    I look forward to your reply.

    EDIT: The real question:

    1. in the settings I have pass the table name and the value of the primary key of the row I want to delete.

    2. He then checks if this line is referenced anywhere, removes the reference, and then deletes the current line. This can go, until all the rows that are referenced are removed.

    The above code, I have provided a small part of the largest of stored procedure.

    Thank you all, I found the tips very helpful. I found a solution to my problem:

    Oracle - how to build queries DELETE in PL/SQL, based on the tables of the FK relations? -Stack overflow

    It generates all removal requests to remove a specific line.  Not what I wanted, but this does not solve my problem.

    The reason why is that I discovered that we do not use the ON DELETE CASCADE that we don't remove a record accidentally, I know we have an option to restore, but it is just in case something goes wrong.

  • SQL query for the mapping of a set of prizes to a group of classrooms

    Hi all

    I use Oracle database 11g Release 2.

    I have the following data set:

    Classrooms
    ClassId ClassName ability group
    ------ ----------------------------------------------      --------------     -----------
    Babbage/software Engg Lab 1 24 1
    Basement 2 - block PG 63 1
    3 1 56 1 class
    Class 4 1 24 10
    Class 5 1 24 11
    Class 6 1 35 12
    7 13 42 1 class
    8 14 42 1 class
    9 15 42 1 class
    10 2 35 1 class
    11 3 35 1 class
    12 4 35 1 classroom
    13 5 35 1 class
    14 6 25 1 class
    15 7 25 1 class
    16 1 24 8 class
    17 9 24 1 class
    18 control Sys Lab 1 24
    19 dig & Embd Sys Lab 20 1
    20 PSD & Comm 20 1 Lab
    21 electromechanical system Lab 28 1
    Farabi 22/Web Tech Lab 1 36
    23 gen purpose Lab 40 1
    Shirazi/24dB Tech Lab 1 36
    ADV 25 elect Lab 30 2
    26 16 42 2 class
    27 17 49 2 class
    28 18 56 2 class
    29 19 42 2 class
    30 20 49 2 class
    Class 31 21 35 3
    32 22 35 3 class
    33 20 3 MDA lab

    DegreeBatches
    BatchId BatchName force
    ---------------     ----------------------- --------------
    1 BIT - 11 79
    2 BIT - 12 28
    3 BS (CS)-1 35
    4 BS (CS) 78-2
    5 BE (SE)-1 69
    6. BE (SE) 84-2
    7 BE (SE) 64-3
    8 84 BYTČA-7
    9 43 BYTČA-8
    BEE-1 10, 112
    11 151 BEE-2
    BEE-3 12, 157
    13 BEE-4 157

    I want to map a combination of batch of degree for a class rooms group of such distance that they make full use of the maximum capacity of the class rooms within a group (ideally), or as close to this as possible. Can it be done with a SQL query?

    Any response will be appreciated.

    The SQL Scripts to generate the required tables and populate data is less to:
    CREATE TABLE classrooms (ClassId NUMBER, ClassName VARCHAR2 (50), capacity NUMBER, group NUMBER);
    INSERT INTO the classrooms of the VALUES (1, "Babbage/software Engg Lab', 24, 1");
    INSERT INTO the classrooms of the VALUES (2, 'basement - PG block', 63, 1);
    INSERT INTO the classrooms of the VALUES (3, '1 class room', 56, 1);
    INSERT INTO the classrooms of the VALUES (4, '10 class room', 24, 1);
    INSERT INTO the classrooms of the VALUES (5, '11 class room', 24, 1);
    INSERT INTO the classrooms of the VALUES (6, 'class room 12', 35, 1);
    INSERT INTO the classrooms of the VALUES (7, 'class room 13', 42, 1);
    INSERT INTO the classrooms of the VALUES (8, 'class room 14', 42, 1);
    INSERT INTO the classrooms of the VALUES (9, '15 'class, 42, 1);
    INSERT INTO the classrooms of the VALUES (10, 'class 2', 35, 1);
    INSERT INTO the classrooms of the VALUES (11, 'class room 3', 35, 1);
    INSERT INTO the classrooms of the VALUES (12, 'class room 4', 35, 1);
    INSERT INTO the classrooms of the VALUES (13, 'class room 5', 35, 1);
    INSERT INTO the classrooms of the VALUES (14, 'class room 6', 25, 1);
    INSERT INTO the classrooms of the VALUES (15, '7 class room', 25, 1);
    INSERT INTO the classrooms of the VALUES (16, 'class Room 8', 24, 1);
    INSERT INTO the classrooms of the VALUES (17, 'class room 9', 24, 1);
    INSERT INTO the classrooms of the VALUES (18, 'Control Sys Lab', 24, 1);
    INSERT INTO the classrooms of the VALUES (19, 'Dig & Embd Sys Lab', 20, 1);
    INSERT INTO the classrooms of the VALUES (20, 'DSP & Comm Lab', 20, 1);
    INSERT INTO the classrooms of the VALUES (21, 'system ELECTROMECHANICAL Lab', 28, 1);
    INSERT INTO the classrooms of the VALUES (22, ' Farabi/Web Tech Lab', 36, 1);
    INSERT INTO the classrooms of the VALUES (23, 'Gen purpose Lab', 40, 1);
    INSERT INTO the classrooms of the VALUES (24, ' Shirazi/DB Tech Lab', 36, 1);
    INSERT INTO the classrooms of the VALUES (25, 'Elected Adv Lab', 30, 2);
    INSERT INTO the classrooms of the VALUES (26, 'class room 16', 42, 2);
    INSERT INTO the classrooms of the VALUES (27, 'class room 17', 49, 2);
    INSERT INTO the classrooms of the VALUES (28, '18 'class, 56, 2);
    INSERT INTO the classrooms of the VALUES (29, '19 'class, 42, 2);
    INSERT INTO the classrooms of the VALUES (30, 'class room 20', 49, 2);
    INSERT INTO the classrooms of the VALUES (31, 'class room 21', 35, 3);
    INSERT INTO the classrooms of the VALUES (32, 'room 22', 35, 3);
    INSERT INTO the classrooms of the VALUES (33, 'MDA Lab', 20, 3);

    CREATE TABLE DegreeBatches (BatchId NUMBER, BatchName VARCHAR2 (50), membership NUMBER);
    INSERT INTO DegreeBatches VALUES(1,'BIT-11',79);
    INSERT INTO DegreeBatches VALUES(2,'BIT-12',28);
    INSERT INTO DegreeBatches VALUES (3, 'BS (CS) - 1', 35);
    INSERT INTO DegreeBatches VALUES (4, 'BS (CS) - 2', 78);
    INSERT INTO DegreeBatches VALUES (5,'BE (SE) - 1', 69);
    INSERT INTO DegreeBatches VALUES (6,'BE (SE) - 2', 84);
    INSERT INTO DegreeBatches VALUES (7,'BE (SE) - 3', 64);
    INSERT INTO DegreeBatches VALUES(8,'BICSE-7',84);
    INSERT INTO DegreeBatches VALUES(9,'BICSE-8',43);
    INSERT INTO DegreeBatches VALUES(10,'BEE-1',112);
    INSERT INTO DegreeBatches VALUES(11,'BEE-2',151);
    INSERT INTO DegreeBatches VALUES(12,'BEE-3',157);
    INSERT INTO DegreeBatches VALUES(13,'BEE-4',157);

    Best regards
    Bilal

    Published by: Bilal on December 27, 2012 09:52

    Published by: Bilal on December 27, 2012 10:07

    Bilal, thanks for the nice problem! Another possibility to double check is to write a small PL/SQL function that returns 1 if a duplicate id is found, then equate to 0: "NUMBER of RETURN of Duplicate_Token_Found (p_str_main in VARCHAR2, p_str_trial VARCHAR2). It should analyze the second string and could use p_str_main LIKE '%', | l_id | ', %' for each id. In any case, the query complete (without that) is given below:

    Solution with names
    SQL> WITH rsf_itm (con_id, max_weight, nxt_id, lev, tot_weight, tot_profit, path, root_id, lev_1_id) AS (
      2  SELECT c.id,
      3         c.max_weight,
      4         i.id,
      5         0,
      6         i.item_weight,
      7         i.item_profit,
      8         ',' || i.id || ',',
      9         i.id,
     10         0
     11    FROM items i
     12   CROSS JOIN containers c
     13   UNION ALL
     14  SELECT r.con_id,
     15         r.max_weight,
     16         i.id,
     17         r.lev + 1,
     18         r.tot_weight + i.item_weight,
     19         r.tot_profit + i.item_profit,
     20         r.path || i.id || ',',
     21         r.root_id,
     22         CASE WHEN r.lev = 0 THEN i.id ELSE r.nxt_id END
     23    FROM rsf_itm r
     24    JOIN items i
     25      ON i.id > r.nxt_id
     26     AND r.tot_weight + i.item_weight <= r.max_weight
     27   ORDER BY 1, 2
     28  ) SEARCH DEPTH FIRST BY nxt_id SET line_no
     29  , rsf_con (nxt_con_id, nxt_line_no, con_path, itm_path, tot_weight, tot_profit, lev) AS (
     30  SELECT con_id,
     31         line_no,
     32         To_Char(con_id),
     33         ':' || con_id || '-' || (lev + 1) || ':' || path,
     34         tot_weight,
     35         tot_profit,
     36         0
     37    FROM rsf_itm
     38   UNION ALL
     39  SELECT r_i.con_id,
     40         r_i.line_no,
     41         r_c.con_path || ',' || r_i.con_id,
     42         r_c.itm_path ||  ':' || r_i.con_id || '-' || (r_i.lev + 1) || ':' || r_i.path,
     43         r_c.tot_weight + r_i.tot_weight,
     44         r_c.tot_profit + r_i.tot_profit,
     45         r_c.lev + 1
     46    FROM rsf_con r_c
     47    JOIN rsf_itm r_i
     48      ON r_i.con_id > r_c.nxt_con_id
     49   WHERE r_c.itm_path NOT LIKE '%,' || r_i.root_id || ',%'
     50     AND r_c.itm_path NOT LIKE '%,' || r_i.lev_1_id || ',%'
     51     AND r_c.itm_path NOT LIKE '%,' || r_i.nxt_id || ',%'
     52  )
     53  , paths_ranked AS (
     54  SELECT itm_path || ':' itm_path, tot_weight, tot_profit, lev + 1 n_cons,
     55         Rank () OVER (ORDER BY tot_profit DESC) rnk,
     56         Row_Number () OVER (ORDER BY tot_profit DESC) sol_id
     57    FROM rsf_con
     58  ), best_paths AS (
     59  SELECT itm_path, tot_weight, tot_profit, n_cons, sol_id
     60    FROM paths_ranked
     61   WHERE rnk = 1
     62  ), row_gen AS (
     63  SELECT LEVEL lev
     64    FROM DUAL
     65  CONNECT BY LEVEL <= (SELECT Count(*) FROM items)
     66  ), con_v AS (
     67  SELECT  b.itm_path, r.lev con_ind, b.sol_id, b.tot_weight, b.tot_profit,
     68          Substr (b.itm_path, Instr (b.itm_path, ':', 1, 2*r.lev - 1) + 1,
     69            Instr (b.itm_path, ':', 1, 2*r.lev) - Instr (b.itm_path, ':', 1, 2*r.lev - 1) - 1)
     70             con_nit_id,
     71          Substr (b.itm_path, Instr (b.itm_path, ':', 1, 2*r.lev) + 1,
     72            Instr (b.itm_path, ':', 1, 2*r.lev + 1) - Instr (b.itm_path, ':', 1, 2*r.lev) - 1)
     73             itm_str
     74    FROM best_paths b
     75    JOIN row_gen r
     76      ON r.lev <= b.n_cons
     77  ), con_split AS (
     78  SELECT itm_path, con_ind, sol_id, tot_weight, tot_profit,
     79         Substr (con_nit_id, 1, Instr (con_nit_id, '-', 1) - 1) con_id,
     80         Substr (con_nit_id, Instr (con_nit_id, '-', 1) + 1) n_items,
     81         itm_str
     82    FROM con_v
     83  ), itm_v AS (
     84  SELECT  c.itm_path, c.con_ind, c.sol_id, c.con_id, c.tot_weight, c.tot_profit,
     85          Substr (c.itm_str, Instr (c.itm_str, ',', 1, r.lev) + 1,
     86            Instr (c.itm_str, ',', 1, r.lev + 1) - Instr (c.itm_str, ',', 1, r.lev) - 1)
     87             itm_id
     88    FROM con_split c
     89    JOIN row_gen r
     90      ON r.lev <= c.n_items
     91  )
     92  SELECT v.sol_id,
     93         v.tot_weight s_wt, v.tot_profit s_pr, c.id c_id, c.name c_name, c.max_weight m_wt,
     94         Sum (i.item_weight) OVER (PARTITION BY v.sol_id, c.id) c_wt,
     95         i.id i_id, i.name i_name, i.item_weight i_wt, i.item_profit i_pr
     96    FROM itm_v v
     97    JOIN containers c
     98      ON c.id = To_Number (v.con_id)
     99    JOIN items i
    100      ON i.id = To_Number (v.itm_id)
    101   ORDER BY sol_id, con_id, itm_id
    102  /
    
        SOL_ID S_WT S_PR  C_ID C_NAME          M_WT C_WT  I_ID I_NAME     I_WT I_PR
    ---------- ---- ---- ----- --------------- ---- ---- ----- ---------- ---- ----
             1  255  255     1 SEECS UG Block   100  100     1 BIT-10       35   35
                                                             2 BIT-11       40   40
                                                             6 BICSE-7      25   25
                             2 IAEC Building     70   70     4 BSCS-3       40   40
                                                             7 BESE-3       30   30
                             3 RIMMS Building    90   85     3 BSCS-2       35   35
                                                             5 BEE-4        50   50
             2  255  255     1 SEECS UG Block   100   95     4 BSCS-3       40   40
                                                             6 BICSE-7      25   25
                                                             7 BESE-3       30   30
                             2 IAEC Building     70   70     1 BIT-10       35   35
                                                             3 BSCS-2       35   35
                             3 RIMMS Building    90   90     2 BIT-11       40   40
                                                             5 BEE-4        50   50
             3  255  255     1 SEECS UG Block   100  100     3 BSCS-2       35   35
                                                             4 BSCS-3       40   40
                                                             6 BICSE-7      25   25
                             2 IAEC Building     70   65     1 BIT-10       35   35
                                                             7 BESE-3       30   30
                             3 RIMMS Building    90   90     2 BIT-11       40   40
                                                             5 BEE-4        50   50
             4  255  255     1 SEECS UG Block   100  100     3 BSCS-2       35   35
                                                             4 BSCS-3       40   40
                                                             6 BICSE-7      25   25
                             2 IAEC Building     70   70     2 BIT-11       40   40
                                                             7 BESE-3       30   30
                             3 RIMMS Building    90   85     1 BIT-10       35   35
                                                             5 BEE-4        50   50
             5  255  255     1 SEECS UG Block   100   95     2 BIT-11       40   40
                                                             6 BICSE-7      25   25
                                                             7 BESE-3       30   30
                             2 IAEC Building     70   70     1 BIT-10       35   35
                                                             3 BSCS-2       35   35
                             3 RIMMS Building    90   90     4 BSCS-3       40   40
                                                             5 BEE-4        50   50
             6  255  255     1 SEECS UG Block   100  100     2 BIT-11       40   40
                                                             3 BSCS-2       35   35
                                                             6 BICSE-7      25   25
                             2 IAEC Building     70   65     1 BIT-10       35   35
                                                             7 BESE-3       30   30
                             3 RIMMS Building    90   90     4 BSCS-3       40   40
                                                             5 BEE-4        50   50
             7  255  255     1 SEECS UG Block   100  100     2 BIT-11       40   40
                                                             3 BSCS-2       35   35
                                                             6 BICSE-7      25   25
                             2 IAEC Building     70   70     4 BSCS-3       40   40
                                                             7 BESE-3       30   30
                             3 RIMMS Building    90   85     1 BIT-10       35   35
                                                             5 BEE-4        50   50
             8  255  255     1 SEECS UG Block   100  100     1 BIT-10       35   35
                                                             4 BSCS-3       40   40
                                                             6 BICSE-7      25   25
                             2 IAEC Building     70   70     2 BIT-11       40   40
                                                             7 BESE-3       30   30
                             3 RIMMS Building    90   85     3 BSCS-2       35   35
                                                             5 BEE-4        50   50
             9  255  255     1 SEECS UG Block   100  100     1 BIT-10       35   35
                                                             4 BSCS-3       40   40
                                                             6 BICSE-7      25   25
                             2 IAEC Building     70   65     3 BSCS-2       35   35
                                                             7 BESE-3       30   30
                             3 RIMMS Building    90   90     2 BIT-11       40   40
                                                             5 BEE-4        50   50
            10  255  255     1 SEECS UG Block   100  100     1 BIT-10       35   35
                                                             3 BSCS-2       35   35
                                                             7 BESE-3       30   30
                             2 IAEC Building     70   65     2 BIT-11       40   40
                                                             6 BICSE-7      25   25
                             3 RIMMS Building    90   90     4 BSCS-3       40   40
                                                             5 BEE-4        50   50
            11  255  255     1 SEECS UG Block   100  100     1 BIT-10       35   35
                                                             3 BSCS-2       35   35
                                                             7 BESE-3       30   30
                             2 IAEC Building     70   65     4 BSCS-3       40   40
                                                             6 BICSE-7      25   25
                             3 RIMMS Building    90   90     2 BIT-11       40   40
                                                             5 BEE-4        50   50
            12  255  255     1 SEECS UG Block   100   95     1 BIT-10       35   35
                                                             3 BSCS-2       35   35
                                                             6 BICSE-7      25   25
                             2 IAEC Building     70   70     2 BIT-11       40   40
                                                             7 BESE-3       30   30
                             3 RIMMS Building    90   90     4 BSCS-3       40   40
                                                             5 BEE-4        50   50
            13  255  255     1 SEECS UG Block   100   95     1 BIT-10       35   35
                                                             3 BSCS-2       35   35
                                                             6 BICSE-7      25   25
                             2 IAEC Building     70   70     4 BSCS-3       40   40
                                                             7 BESE-3       30   30
                             3 RIMMS Building    90   90     2 BIT-11       40   40
                                                             5 BEE-4        50   50
            14  255  255     1 SEECS UG Block   100  100     1 BIT-10       35   35
                                                             2 BIT-11       40   40
                                                             6 BICSE-7      25   25
                             2 IAEC Building     70   65     3 BSCS-2       35   35
                                                             7 BESE-3       30   30
                             3 RIMMS Building    90   90     4 BSCS-3       40   40
                                                             5 BEE-4        50   50
    
    98 rows selected.
    
    Elapsed: 00:00:01.42
    

    Published by: BrendanP on January 20, 2013 11:25
    I found the need to deduplicate regular expression:

    AND RegExp_Instr (r_c.itm_path | r_i.path, ',(\d+),.*?,\1,') = 0)

  • 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

  • Dynamic SQL query in the DB adapter

    Hello
    I want to use the following custom SQL query in the DB adapter:

    SELECT EMP_ID, EMP_NAME, EMP_LOCATION FROM EMP WHERE EMP_ID IN (#iNPUT_PARAMETER)

    the problem I'm facing is that I don't know at the time of the development that EMP_ID how much will be passed to the process so that I can not use the input parameter specific number. I cannot use it IN the query within which statement I concatenate all the EMP_ID separated by comma and passed under INPUT_PARAMETER, but when running, he read the full concatenated string as a single string rather than identify them as a list of different: with comma separation. could someone help me in this case

    Thank you
    Sunil

    Use the following query with two parameters. Build the string with delimiter when running and pass this string and delimiter to query... (in this example, the input value would be the same for both Delimiter1 and Delimiter2.)

    Select * from EMP WHERE deptno IN (SELECT SUBSTR (STRING_TO_TOKENIZE, DECODE (LEVEL, 1, 1, INSTR (STRING_TO_TOKENIZE, SEPARATOR, 1, LEVEL-1) + 1), INSTR (STRING_TO_TOKENIZE, SEPARATOR, 1, LEVEL)-DECODE (LEVEL, 1, 1, INSTR (STRING_TO_TOKENIZE, SEPARATOR, 1, LEVEL-1) + 1)) FROM (SELECT #StringToTokenizer | #Delimiter1 AS STRING_TO_TOKENIZE, #Delimiter2 AS DELIMITER FROM DUAL) CONNECT BY INSTR (STRING_TO_TOKENIZE SEPARATOR) ((, 1, LEVEL) > 0)

    Thank you
    -Sreeny

  • SQL (body of function from PL/SQL returning the SQL) query when you use to_char

    We are trying to build a page of Type SQL Query report (body of function from PL/SQL returning SQL query).
    our request is simple, to extract the month of the column recording_date.

    declare
    l_query varchar2 (1000);
    Start
    l_query: = 'select to_char (recording_date, 'MM') of re_productive;
    Return l_query;
    end;

    but we would like the following for this query problem

    Function returning SQL query: query cannot be parsed in the generator. If you believe that your query is syntactically correct, check the box of generic columns below the source of the region without analysis.
    (ORA-06550: line 4, column 42: PLS-00103: encountered the symbol "MY" what awaits one of the following values:.) (* @ % & = - +; <>/ at is mod remains not rem <>or! = or ~ = > = < = <>and or as between | multiset Member SUBMULTISET_ the symbol ".") was inserted before 'MY' to continue.)

    Notes:
    1 - the request is correct and it has been tested under sqlplus and toad.
    2. we tried option use generic name (analysis of query runtime only) column, but we get the same problem.


    any quick help please.

    Hello

    You have not escaped your quote in the string. Try this...

    DECLARE
    l_query VARCHAR2(32767);
    
    BEGIN
    
    l_query:= 'select to_char(recording_date,''MM'') from re_productive';
    
    RETURN l_query;
    
    END;
    

    See you soon

    Ben

Maybe you are looking for