SQL query, generating several rows by a value in a field

I am using Oracle database. Imaging I have a table with a row as following
     col1  |  col2  | col3
    'Str1' | 'Str2' | 4
The value from col3=4 should generate 4 rows as following:
     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 luck - how can I create such a select query???

You're mixing up the 'standard' WITH clause and Recursive Subquery Factoring (which NEEDS a UNION ALL)  (see: http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_10002.htm#i2129904).
This should work on 10.2:
SQL> select * from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
CORE    10.2.0.4.0      Production
TNS for Linux: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production
SQL> with t as (select 'str1' col1, 'str2' col2, 4 col3 from dual
  2            )
  3  select col1
  4  ,      col2
  5  ,      level col3
  6  from   t
  7  connect by level <= col3;
COL1 COL2       COL3
str1 str2          1
str1 str2          2
str1 str2          3
str1 str2          4
See here as well: http://technology.amis.nl/2009/09/01/oracle-rdbms-11gr2-goodbye-connect-by-or-the-end-of-hierarchical-querying-as-we-know-it/

Similar Messages

  • [Excel] Running a SQL Query to delete rows

    Hello Experts,
    Background: I am attempting to use a dba of my companies time keeping system and implement it with Power BI tools. Given the file size restrictions within Power Bi itself I need to lower my file size by removing all time logs from
    inactive employees.
    I have a question regarding whether or not you can use a sql query to delete rows in excel. I have roughly 200,000+ rows in my excel spreadsheet. I am attempting to delete all rows where an employee equals inactive. I have attempted to
    delete these rows by sorting them and doing a bulk delete and clear contents, but it seems to crash my excel every time.  My thought process is that using a query that does a timed delete might put less of a burden on deleting the massive amount of data.
    I would like to use this: DELETE * FROM [Table_(...)_Actual$] WHERE [Current] = "Inactive" (Will add more once I know it is possible to use sql queries in Excel.
    Any information on whether or not this is possible would be appreciated.
    Regards,
    Link

    Running SQL Query in Excel is possible, however, the delete query is not supported in Excel.
    You are more restricted in deleting Excel data than   data from a relational data source. In a relational database, "row" has no   meaning or existence apart from "record"; in an Excel worksheet, this is not   true. You can delete values
    in fields (cells). Please see:http://support.microsoft.com/kb/257819/en-us
    One workaround : Use update query to set the rows as null, then use select query.
    e.g. 
    SQL = "update [sheet2$A1:B5] set name=null,age=null where name='andy'"
    cnn.Execute SQL
    SQL = "select name,age from [sheet2$A1:B5] where name is not null"
    Wind Zhang
    TechNet Community Support

  • How to view the sql query generated in query designer..?

    Hi experts,
       Can i view the sql query generated by the query designer for a query in sap bw?
    regards,
    ksvsivam

    Hi Sivam,
    You can definitely view the sql query.
    Go to RSRT and give the report name. Then goto Execute+Debug and click on it.
    A new window will open, there tick the box  Display SQL/BIA Query and the press on the ok button.
    Finally click on execute. The Sql query will be displayed.
    Hope this helps.
    Thanks,
    Rahul

  • Query to delete row where column value contains alphabets

    Hi,
    Could anyone please help me to get this query work.
    Query to delete row where column value contains alphabets.
    DELETE  FROM BIN_ITEM WHERE order_nmb LIKE '%[A-Z]%' || LIKE '%[a-z]%'
    Thanks and Regards,
    Deekay.

    RaminHashimzadeh wrote:
    SELECT order_nmb FROM BIN_ITEM WHERE regexp_count(order_nmb,'[0-9]') = 0
    Ramin Hashimzade
    But that won't reject strings like 'gfgG%dgh' which aren't pure alphabetic.
    Try:
    with test_data as (
    select 'ghTYJYEhdfe' str from dual
    union
    select 'dfF5ssd' from dual
    union
    select 'rgth*dgheh' from dual
    union
    select 'ggf{' from dual
    union
    select 'rwhrhrh' from dual
    select  *
    from test_data
    where regexp_instr(str,'[^[:alpha:]]')=0;

  • How to return in pl/sql the tuples of an sql query comming  several tables

    I have a select query whose result comes from several tables. The question is how to return the tuples of the select query using a pl/sql function. In othe words, is it possible to define a construct structure to store the values of sql query and return that construct. if yes how to define such a construct where its elements comming from several tables.
    I appreciate very much ur help

    The way to return a resultset from a function is with a cursor variable. The lazy of doing this is using a weakly-typed ref cursor (in 9i use the predefined SYS_REFCURSOR).
    Cheers, APC

  • Need help in SQL Query: Update a row in a table & insert the same row into another table

    I want to update a row in a table say Table A and the updated row should be inserted into another table say Table B. I need to do it in a single SQL query and i don't want to do it in PL/SQL with triggers. And i tried with MERGE statement but its working with this scenario. (Note: I'm using Oracle Database 10g Enterprise Edition Release 10.2.0.1.0).
    Thanks in Advance.

    Using Sven's code as an example, you could save the updated row in a sql plus variable. (also untested):
    SQL> var v_id number
    update tableA  
    set colB='ABC' 
    where colC='XYZ' 
    returning id into :v_id;
    insert into table A_History (ID, colA, colB, ColC)  
    select id, ColA, ColB, ColC  
    from tableA  
    where id = :v_id;   

  • SQL query generating data in SQL management studio but not in CUIC interface

    Hello,
    I'm working on a UCCE 9.0 system
    I created SQL query for a  report.
    Whenever I run the query inside the SQL management studio, it works and generates data, but when I run it in the CUIC interface it works but generates nothing. As you can see in the below snapshot, it contains 209 records that are not being displayed.
    Any help would be greatly apprciated

    Is it a custom report ? Can you right click on your Custom Report and click on Edit Views. You need to check whether you have Grid Headers Listed.
    Regards,
    Senthil

  • Sql query union double rows

    Hi,
    i have a problem with my sql query.
    I need the difference from query 2 - query 1.
    the red marked row should be ignored because its already in table 1.
    Union compares all colums not just the first 3.
    Here's a screen of the problem:
    http://c60.img-up.net/?up=screenoracskt6.JPG
    thx for all

    Hi,
    Welcome to the forum!
    Whenever you have a problem, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and the results you want from that data.
    Explain how you get those results from that data.
    Always say what version of oracle you're using.
    user6754335 wrote:
    Hi,
    i have a problem with my sql query.
    I need the difference from query 2 - query 1.
    the red marked row should be ignored because its already in table 1.
    Union compares all colums not just the first 3.That's right; UNION compare all the columns selected. If you're only interested in 3 of the columns, then only select those 3.
    If you need to display more columns, but not where all 3 of them match, then the best way is probably an outer join; but there are lots of other ways, including NOT EXISTS, NOT IN, and MINUS (in a sub-query).
    Here's a screen of the problem:
    http://c60.img-up.net/?up=screenoracskt6.JPG
    Post anything relevant on this site.

  • Sql query to add row id without loops

    I am aware of the statement SELECT ROW_NUMBER() OVER (ORDER ...
    But we need more functionality. Can we make the row_number to repeat counting from 1 after a specific field changes in value.  I know how to achieve what I want using a cursor within a stored procedure. Is there a way we
    achieve the same just using sql statements and without stored procedure/loop
    For example, we have built the following resultset using an sql query and the result is already sorted by the first field, CompanyName:
    CompanyName, department, ManagerName
    A-company, Account, Steven
    A-company, HR, Mark
    A-company, IT, Susan
    B-Company,IT,Ross
    B-Company,Customer Service, Jack
    C-Company,Finance, Matthew
    C-Company,Finance, Helen
    C-Company,Finance, Ron
    I want to have a rew result with one additional column which is incremented automatically but the value must re-start from 1 once the Companyname changes.
    1, A-company, Account, Steven
    2, A-company, HR, Mark
    3, A-company, IT, Susan
    1, B-Company,IT,Ross
    2, B-Company,Customer Service, Jack
    1, C-Company,Finance, Matthew
    2, C-Company,Finance, Helen
    3, C-Company,Finance, Ron
    Please enlighten me.

    You have to program the numbering yourself. There is no built-in automatic numbering like identity or sequence but only OVER PARTITION BY.
    Reference:
    http://www.sqlusa.com/bestpractices2005/overpartitionby/
    Kalman Toth Database & OLAP Architect
    SQL Server 2014 Design & Programming
    New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012

  • SAP SQL Query Generator

    Hi Gali and Experts!!
    I am working on ECC 6.0 and have worked on 4.X version too. I was wondering if within SAP do we have any SQL Generator which will automatically generate the SQL query? We create views, we do lot of joins  on the tables and we write n number of sql queries, so is there a provision that whenever we generate a view or any other data object we can have access to the underlying SQL query that is being generated at backend. Of course, it must be generating the SQL query behind the scenes....This can be done in Peoplesoft. Please let me know.
    Thanks,
    ~Sid

    well..what about table joins....in query viewer....anyways thanks for the  input...
    ~Sid

  • Displaying SQL Query results in rows instead of Columns

    Hi,
    I'm fairly new to Oracle so forgive me if this is a really stupid question.
    I used Mysql for a while and if I wanted to display query results in rows instead of columns I could end the SQL command with '\G' instead of a semicolon.
    This would give me output like...
    Column_1: AAAA
    Column_2: BBBB
    Column_3: CCCC
    Instead of the normal
    Column_1 Column_2 Column_3
    AAAAAA BBBBBBB CCCCCCC
    Is there an equivalent in SQLPlus to the MySQL \G termination?
    Thanks.
    John

    > so forgive me if this is a really stupid question.
    It is certainly not a stupid question, but pivoting is a very frequently asked and answered question:
    [url http://forums.oracle.com/forums/search.jspa?threadID=&q=pivot&objID=f75&dateRange=all&userID=&numResults=15]http://forums.oracle.com/forums/search.jspa?threadID=&q=pivot&objID=f75&dateRange=all&userID=&numResults=15
    In 11g you have special PIVOT and UNPIVOT functions.
    Regards,
    Rob.

  • Problem with SQL query generated by setString

    Using Creator 2EA2 I have a rowset based on this query (it's actually only the last bit of it with the third parameter that is a problem):
    SELECT director.director_id,  director.name FROM director, director_last_modified WHERE name REGEXP '^[A-C]'  AND director_last_modified.dir_last_modified > ? AND director_last_modified.dir_last_modified <= ?  AND director.director_id = director_last_modified.director_id AND director_last_modified.dir_flagged = ? ORDER BY name; I use setObject to set the first and second parameters to a date. The third parameter refers to field in the database which indicates whether or not a director is 'flagged'. It's a TINYINT in a MySQL table and may contain either 0 or 1. If the user wants to see 'flagged' directors, I want to set the third parameter to 1. If they want to see all directors I want to set the third parameter to 0 OR 1.
    So I use setString with a String variable for the parameter:
    setString(3, strCriterion3);If strCriterion3 has been set as follows:
    strCriterion3 = "1";the generated SQL query ends with:
    director_last_modified.dir_flagged = '1' ORDER BY name; JDBC supplies the two apostrophes. So it would seem to follow that in order to produce a query which ends with:
    director_last_modified.dir_flagged = '1' OR '0' ORDER BY name;I shoud set strCriterion3 with:
    strCriterion3 = "1' OR '3";
    in order to get the required apostrophes.
    However, the generated query is then:
    director_last_modified.dir_flagged = '1\' OR '0\' ORDER BY name;How do I get rid of those backslashes? I've tried double apostrophes, by the way - that just gives me more backslashes.
    Thanks for any help you can offer.

    [Sorry, but I missed the parentheses from the query. It should end with:
    [code]AND (director_last_modified.dir_flagged = ?) ORDER BY name;
    - not that that makes any difference to the apostrophes problem.]
    I've just discovered a solution in this thread http://swforum.sun.com/jive/thread.jspa?forumID=123&threadID=56228
    So I've replaced = ? in the query with LIKE ?
    I then set strCriterion3 to either "1" or "%". That will do nicely, but it seems a bit of kludge and, under different circumstances, I'd still need to get rid of those pesky apostrophes. Any ideas?

  • Sql query generator from xml data

    Hi,
    I am looking for an open source tool in java which would generate the sql query with an xml configuration file as input. The xml configuration file schema would be defined by the tool and and would provide placeholders for giving the various information required to build the sql query.
    Are there any available?
    Please let me know.
    Anshuk

    hi All,
    I tried below . but still no luck .
    CREATE OR REPLACE FUNCTION get_audit_trail_log(cikey IN INTEGER) RETURN blob IS
    CURSOR c_log(l_cikey INTEGER) IS
    SELECT *
    FROM PROD_SOAINFRA.audit_details atr
    WHERE cikey = l_cikey
    ORDER BY count_id;
    bl BLOB;
    BEGIN
    dbms_lob.createtemporary (bl, TRUE);
    FOR r_log IN c_log(cikey)
    LOOP
    dbms_lob.append (bl,r_log.log);
    END LOOP;
    RETURN(bl);
    END;
    =======================================
    SELECT UTL_RAW.CAST_TO_VARCHAR2(UTL_COMPRESS.LZ_UNCOMPRESS(get_audit_trail_log(ci.cikey)))
    FROM PROD_SOAINFRA.audit_details ci
    WHERE cikey = 848063749
    =======================================
    Error:
    =====================
    ORA-22835: Buffer too small for CLOB to CHAR or BLOB to RAW conversion (actual: 20958, maximum: 2000)
    22835. 00000 - "Buffer too small for CLOB to CHAR or BLOB to RAW conversion (actual: %s, maximum: %s)"
    *Cause:    An attempt was made to convert CLOB to CHAR or BLOB to RAW, where
    the LOB size was bigger than the buffer limit for CHAR and RAW
    types.
    Note that widths are reported in characters if character length
    semantics are in effect for the column, otherwise widths are
    reported in bytes.
    *Action:   Do one of the following
    1. Make the LOB smaller before performing the conversion,
    for example, by using SUBSTR on CLOB
    2. Use DBMS_LOB.SUBSTR to convert CLOB to CHAR or BLOB to RAW.

  • Need sql query to convert rows to columns

    Im using oracle 11g.
    I have data in a table as below: Column names are type,month,percentage
    TYPE      MONTH   PERCENTAGE
    A1         JAN-2013     100
    A2         JAN-2013     100
    A3         JAN-2013     95
    A4         JAN-2013     98
    A5         JAN-2013     99
    A6         JAN-2013     90
    A7         JAN-2013     92
    A1         FEB-2013     100
    A2         FEB-2013     99
    A3         FEB-2013     88
    A4         FEB-2013     67
    A5         FEB-2013     98
    A6         FEB-2013     95
    A7         FEB-2013     84
    The desired output using a sql query is as below
    TYPE     JAN-2013     FEB-2013
    A1         100             100
    A2         100             99
    A3         95               88
    A4         98               67
    A5         99               98
    A6         90               95
    A7         92               84

    Try this:
    select * from table_name
    PIVOT(   max(Percentage)
                     for MONTH
                     in ('JAN-2013','FEB-2013')
                     ) ;with data
    with t(TYPE,MONTH,PERCENTAGE) as
    SELECT 'A1', 'JAN-2013', 100
      FROM DUAL
    UNION ALL
    SELECT 'A2', 'JAN-2013', 100
      FROM DUAL
    UNION ALL
    SELECT 'A3', 'JAN-2013', 95
      FROM DUAL
    UNION ALL
    SELECT 'A4', 'JAN-2013', 98
      FROM DUAL
    UNION ALL
    SELECT 'A1', 'FEB-2013', 100
      FROM DUAL
    UNION ALL
    SELECT 'A2', 'FEB-2013', 99
      FROM DUAL
    UNION ALL
    SELECT 'A3', 'FEB-2013', 88
      FROM DUAL
    UNION ALL
    SELECT 'A4', 'FEB-2013', 67
      FROM DUAL
    select * from t PIVOT(   max(Percentage)
                     for MONTH
                     in ('JAN-2013','FEB-2013')
                     ) ;

  • Modify or Extend SQL Query generated by the Application wizard?Urgent.....

    Hi !
    I have created an application based on an existing table. I also created three search items(select list with submit) for column level search of the Report on Page1.
    Now i want the 'search results' to be displayed on the P1_Report page similar to the results obtained using :P1_REPORT_SEARCH item.
    IS it possible to exentd the SQL query....
    select "ID","SACHNR_BESCHRIFTUNG","BEISCHREIBUNG_DER_SCHRIF","BEMERKUNG"
    from "BES_KGE_ENS2"
    where (
    instr(upper("SACHNR_BESCHRIFTUNG"),upper(nvl(:P1_REPORT_SEARCH,"SACHNR_BESCHRIFTUNG"))) > 0 or
    instr(upper("BEISCHREIBUNG_DER_SCHRIF"),upper(nvl(:P1_REPORT_SEARCH,"BEISCHREIBUNG_DER_SCHRIF"))) > 0 or
    instr(upper("BEMERKUNG"),upper(nvl(:P1_REPORT_SEARCH,"BEMERKUNG"))) > 0
    with another set of 'instr' functions for my search items.the results should be displayed in 'RED'.
    THNX in advance..
    Chalamalasetty.

    chalamalasetty,
    Please don't try to expedite a response to your post by posting the same question again. If you believe your post has gone unanswered too long, "bump" it up to the top by adding to the same post. I replied to your duplicate post here:
    SQL Code??
    Sergio

Maybe you are looking for

  • Connect RD Connection Broker to SQL

    Hello I'm not being able to install RD Connection Broker with High Availability. The RDS deployment consists on two servers running win2012 server1 - RDWA and RDG server2 - RDSH and RDCB The SQL server 2012 that I'm using is installed on server2 itse

  • How do I make my new Airport Extreme a base station?

    I have been using an Airport Extreme (5th generation) as my base station router for a few years. I set the main network for my and my wife's use and created a guest network for when my sons visited and needed to use the wi-fi connection. I used passw

  • Any recourse for poor "genius bar" service?

    We recently took my MacBook Air to a Genius Bar in NYC after the keyboard inexplicably stopped working. Were given a work order repair estimate of $280. We were then told that they found liquid damage that would cost in the vicinity of $400 to fix. T

  • Plotting a graph in java...is it possible?

    hi evryone, i have 100 values stored in an array and ive been looking at how i can display these as they are constantly being updated. can anyone help me in how i would go about plotting these integers as a simple dot graph? thanks...any help is appr

  • Grid Value in Delivery Schedule isn't populating with BAPI_PO_CREATE1

    Hi Gurus, hope you are doing well. I have a problem on my end. I am trying to create a Purchase Order using BAPI_PO_CREATE1. The special thing here is that I am using AFS Data. Everything is working fine, but the problem is that in Delivery Schedule