Home » SQL & PL/SQL » SQL & PL/SQL » SQL - QUERY
icon4.gif  SQL - QUERY [message #609035] Fri, 28 February 2014 03:10 Go to next message
vippysharma
Messages: 73
Registered: May 2013
Location: www
Member
The table is as follows:

Name | Project | Error

108 | test | Err1,Err2,Err3
109 | test2 | Err1
----------------------------------------------------------------
I want to create the following:

Name | Project | Error

108 | Test | Err 1
108 | Test | Err 2
108 | Test | Err 3
109 | Test2 | Err1

------------------------------------------------------------
Note :- Using SQL only.

Regards
Viplove Sharma
Re: SQL - QUERY [message #609042 is a reply to message #609035] Fri, 28 February 2014 04:27 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Here you go:
SQL> WITH test
  2          AS (SELECT 108 c_name, 'test' c_project, 'err1,err2,err3' c_error
  3                FROM DUAL
  4              UNION
  5              SELECT 109, 'test2', 'err1' FROM DUAL)
  6    SELECT c_name,
  7           c_project,
  8           REGEXP_SUBSTR (c_error,
  9                          '[^,]+',
 10                          1,
 11                          x.COLUMN_VALUE)
 12              one_Error
 13      FROM test t,
 14           TABLE (
 15              CAST (
 16                 MULTISET (
 17                        SELECT LEVEL lvl
 18                          FROM DUAL
 19                    CONNECT BY LEVEL <=
 20                                    1
 21                                  + LENGTH (c_error)
 22                                  - LENGTH (REPLACE (c_error, ',', ''))) AS SYS.
 23                                                                             odcinumberlist)) x
 24  ORDER BY c_name, c_project, one_error;

    C_NAME C_PRO ONE_ERROR
---------- ----- --------------
       108 test  err1
       108 test  err2
       108 test  err3
       109 test2 err1

SQL>
Re: SQL - QUERY [message #609047 is a reply to message #609042] Fri, 28 February 2014 05:09 Go to previous messageGo to next message
gazzag
Messages: 1118
Registered: November 2010
Location: Bedwas, UK
Senior Member
I wish we would distinguish between RTFM answers and doing people's homework for them. Some consistency in this might be a good idea.
Re: SQL - QUERY [message #609049 is a reply to message #609047] Fri, 28 February 2014 05:24 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
My opinion is that this kind of query is not a simple one for general audience. People struggle with regular expressions. TABLE? CAST? MULTISET? SYS.ODCINUMBERLIST? Eh? I bet that at least half of people who asked questions listed on the first (PL/)SQL forum page didn't even hear about them (apart from "create TABLE").

Minutes ago I had to explain what an operating system command prompt is.

So, how much time would RTFM take to produce such a query? Now, here's an example. Anyone who is interested in reading will have keywords and know what to search for.

As of consistency: the only consistent thing here is that nothing & nobody is consistent.
Re: SQL - QUERY [message #609056 is a reply to message #609049] Fri, 28 February 2014 09:57 Go to previous messageGo to next message
gazzag
Messages: 1118
Registered: November 2010
Location: Bedwas, UK
Senior Member
Littlefoot wrote on Fri, 28 February 2014 11:24

As of consistency: the only consistent thing here is that nothing & nobody is consistent.


You got that right Smile
Re: SQL - QUERY [message #609069 is a reply to message #609056] Fri, 28 February 2014 23:07 Go to previous messageGo to next message
ken002
Messages: 6
Registered: June 2012
Location: canada
Junior Member
Another way:
SQL> WITH T AS
  2    ( SELECT 1 ID, 'ERR1, ERR2, ERR3' AS ERR FROM DUAL
  3    UNION ALL
  4    SELECT 2 ID, 'ERR4, ERR5' FROM DUAL
  5    )
  6  SELECT ID,
  7    TRIM(REGEXP_SUBSTR(ERR, '[^,]+', 1, RN)) NEW_ERR
  8  FROM T,
  9    (SELECT LEVEL RN
 10    FROM DUAL
 11      CONNECT BY LEVEL<=
 12      (SELECT MAX(LENGTH(REPLACE(ERR, ',', ''))) FROM T
 13      )
 14    )
 15  WHERE REGEXP_SUBSTR(ERR, '[^,]+', 1, RN) IS NOT NULL
 16  ORDER BY 1, 2;

        ID NEW_ERR
---------- ----------------
         1 ERR1
         1 ERR2
         1 ERR3
         2 ERR4
         2 ERR5

SQL>

[Updated on: Fri, 28 February 2014 23:21]

Report message to a moderator

Re: SQL - QUERY [message #609086 is a reply to message #609069] Sat, 01 March 2014 05:43 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
ken002 wrote on Sat, 01 March 2014 00:07
Another way:


Difference between your solution and one from Littlefoot is null elements in the list. Your solution skips null elements while Littlefoot's doesn't. Test both solutions with, for example 'ERR4,,,,,,ERR10,'.

SY.
Re: SQL - QUERY [message #609096 is a reply to message #609086] Sat, 01 March 2014 09:42 Go to previous messageGo to next message
ken002
Messages: 6
Registered: June 2012
Location: canada
Junior Member
Try this:
SQL> WITH T AS
  2        ( SELECT 1 ID, 'ERR1, ERR2, ERR3' AS ERR FROM DUAL
  3       UNION ALL
  4        SELECT 2 ID, 'ERR4, ERR5' FROM DUAL
  5        UNION ALL
  6        SELECT 3 ID, 'ERR6,, , , , ERR11' FROM DUAL
  7       )
  8  SELECT id,TRIM(REGEXP_SUBSTR(err,'[^,]+',1,rn)) s_err
  9    FROM (SELECT t.*,LENGTH(err)-LENGTH(REPLACE(err,','))+1 cnt
 10            FROM t
 11          )
 12        ,(SELECT ROWNUM rn FROM DUAL CONNECT BY ROWNUM<=100)
 13  WHERE RN<=CNT
 14  ORDER BY 1;

        ID S_ERR
---------- ------------------
         1 ERR1
         1 ERR2
         1 ERR3
         2 ERR4
         2 ERR5
         3 ERR11
         3 ERR6
         3
         3
         3
         3

11 rows selected.

[Updated on: Sat, 01 March 2014 09:42]

Report message to a moderator

Re: SQL - QUERY [message #609097 is a reply to message #609096] Sat, 01 March 2014 09:53 Go to previous message
Michel Cadot
Messages: 68644
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
Try this:


Which is far far less good than Littlefoot's one.

Previous Topic: Get previous quarter value
Next Topic: Index on column name "number"!!!
Goto Forum:
  


Current Time: Wed Apr 24 01:17:41 CDT 2024