How do I rotate my result of a query from a line to a SQL column?

Hey, guys:

Is it possible that I can rotate my result of a query from a line to a SQL column?

It's a certain type of pivot example
Instead of
DEPTNO DNAME
-------- ---------------
10 accounting
It would be
DEPTNO: 10
DNOM: ACCOUNTING

Hello

When you have N columns to rank 1 and you want to display in the form of 1 column on several lines, it's called Unpivoting . Here's a way to do it:

WITH     cntr     AS
(
     SELECT     LEVEL     AS n
     FROM     dual
     CONNECT BY     LEVEL <= 2     -- number of columns to be unpivoted
)
SELECT     CASE c.n
         WHEN  1  THEN  'DEPTNO'
         WHEN  2  THEN  'DNAME'
     END || ':'        AS label
,     CASE c.n
         WHEN  1  THEN  TO_CHAR (d.deptno)
         WHEN  2  THEN  d.dname
     END || ':'        AS val
,     d.deptno               -- PK, if needed
FROM         cntr        c
CROSS JOIN  scott.dept  d
WHERE   deptno = 10               -- or whatever
;

The query above works in Oracle 9.1 or more.
From Oracle 11.1, you can also use the SELECT... Function of the UNPIVOT operator.

Tags: Database

Similar Questions

  • How the horizontal rotation the result set? (no swivel)

    Hi all

    Suppose that my result of the query is as follows. (Version 11g)

    Select 1 id, 'a' name, 'new york' city from dual

    Union all

    Select 1 id, 'b' name, 'manhattan' city from dual

    Union all

    Select 1 id, 'c' name, 'paris' city from dual

    Union all

    Select 1 id, 'd' name, 'ottowa' city from dual

    Union all

    Select 1 id, 'e' name, 'toronto' city from dual

    Union all

    Select 1 id, 'f' name, 'newcastle' city from dual

    Union all

    Select 1 id, 'g' name, 'liverpool' city from dual

    Union all

    Select 1 id, 'h' name, 'manchester' city from dual;

    1 in new york

    1 b manhattan

    1 c. paris

    Ottowa 1 d

    1 e toronto

    1 f newcastle

    Liverpool 1 g

    1 h manchester

    I want the following

    Select 1 id, 'a' name, 'new york' city, 'b' name, 'manhattan' city, 'c' name, 'paris' city, 'd' name, 'ottowa' city, 'e' name, 'toronto' city, 'f' name, 'newcastle' city, 'g' name, 'liverpool', 'h' name, 'manchester'  from dual;

    1 a new york manhattan ottowa paris b d e f g c. manchester liverpool newcastle toronto

    Thank you

    OR maybe you looing for:

    ----------

    WITH dataset
         AS (SELECT 1 id, 'a' name, 'new york' city FROM DUAL
             UNION ALL
             SELECT 1 id, 'b' name, 'manhattan' city FROM DUAL
             UNION ALL
             SELECT 1 id, 'c' name, 'paris' city FROM DUAL
             UNION ALL
             SELECT 1 id, 'd' name, 'ottowa' city FROM DUAL
             UNION ALL
             SELECT 1 id, 'e' name, 'toronto' city FROM DUAL
             UNION ALL
             SELECT 1 id, 'f' name, 'newcastle' city FROM DUAL
            UNION ALL
             SELECT 1 id, 'g' name, 'liverpool' city FROM DUAL
             UNION ALL
             SELECT 1 id, 'h' name, 'manchester' city FROM DUAL
    )
    ,resultset as (SELECT id||','||listagg(name ||','||city,',') within group (order by id) str FROM dataset group by id)
    select regexp_substr(str,'[^,]+',1,1) id
    ,regexp_substr(str,'[^,]+',1,2) name
    ,regexp_substr(str,'[^,]+',1,3) city
    ,regexp_substr(str,'[^,]+',1,4) name
    ,regexp_substr(str,'[^,]+',1,5) city
    ,regexp_substr(str,'[^,]+',1,6) name
    ,regexp_substr(str,'[^,]+',1,7)city
    ,regexp_substr(str,'[^,]+',1,8)name
    , regexp_substr(str,'[^,]+',1,9)city
    , regexp_substr(str,'[^,]+',1,10)name
    , regexp_substr(str,'[^,]+',1,11)city
    , regexp_substr(str,'[^,]+',1,12)name
    , regexp_substr(str,'[^,]+',1,13)city
    , regexp_substr(str,'[^,]+',1,14)name
    , regexp_substr(str,'[^,]+',1,15)city
    , regexp_substr(str,'[^,]+',1,16)name
    , regexp_substr(str,'[^,]+',1,17)city
    from resultset
    

    See you soon;

    Manik.

  • How can I write my results to Excel in a perticular line sheet?

    Hi all

    I'm new in labview and I have a problem.

    I found my results, it is in the 2D array... He sometimes in 1 d array because I want to remove the header line.

    How can I write my results one by one in the row following on it?

    Hereby, I have attached the excel sheet where I write my results.

    There are about 25 result so I have to run my VI 25 times but the result appears in excel sheet in 25 succeeding row.

    hope you find my question...

    Thank you

    Ok..

    I'll probably do...

  • return results as a query of single line sup.

    Hello

    It's my current query
    WITH car as (SELECT 'FORD' make FROM DUAL UNION SELECT 'TOYOTA' make FROM DUAL UNION SELECT'BMW' make FROM DUAL UNION SELECT 'FIAT' make FROM DUAL)
    SELECT * FROM CAR
    Which is: -.
    BMW
    FIAT
    FORD
    TOYOTA
    However, the actual output required: -.
    'BMW, FIAT, FORD, TOYOTA'
    that is, all turned around on a single line.
    Any ideas on how to do this?

    Thanks in advance!
    David

    Or

    SQL> ed
    Wrote file afiedt.buf
    
      1  WITH car as (SELECT 'FORD' make FROM DUAL UNION
      2               SELECT 'TOYOTA' make FROM DUAL UNION
      3               SELECT'BMW' make FROM DUAL UNION
      4               SELECT 'FIAT' make FROM DUAL)
      5  SELECT ltrim(sys_connect_by_path(make,','),',') as makes
      6  FROM (select make, row_number() over (order by make) rn from CAR)
      7  where connect_by_isleaf = 1
      8  connect by rn = prior rn + 1
      9* start with rn = 1
    SQL> /
    
    MAKES
    ------------------------------------------------------------------------
    BMW,FIAT,FORD,TOYOTA
    
    SQL>
    
  • How can I print the results of my research?

    I have more than 3000 DWF files in my search results. They are located in different folders on our server. How can I print the results of my research?

    This means that nomo did not read your question carefully.

    I think Directory printer Karen will do what you want.  3000 files is a lot of paper.  You can download Cute PDF Writer and save the output to a PDF instead - at least until you get the options sorted...

  • How can I find the results of a 'chkdsk' after you restart the computer?

    Original title: checkdisk

    How can I find the results of a 'chkdsk' after you restart the computer. I am running "Vista Home Premium"

    Hello

    CHKDSK (short for Checkdisk)-<-- see="" the="" vista="" log="">
    http://en.Wikipedia.org/wiki/chkdsk

    Journal of checkdisk is EventViewer which is easy to read with the 2nd utility here.

    Look in the Event Viewer to see the newspaper.
    http://www.computerperformance.co.UK/Vista/vista_event_viewer.htm

    Control Panel - Classic view - administrative tools - event - left panel - Windows logs Viewer - under the
    above - click on request - in the middle column, scroll down the list that indicates the Source - click Wininit.
    good Panel - Wininit event - click on the event properties - new window will open with the journal you want.
    Scroll as needed. Event ID is 1001.

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

    If you clear the log before you checkdisk annex, it is easier to find but by time and Date
    should be easy enough.

    MyEventViewer - free - a simple alternative in the standard Windows Event Viewer.
    TIP - Options - Advanced filter allows you to see a period of time instead of the entire file.

    http://www.NirSoft.NET/utils/my_event_viewer.html

    I hope this helps.

    Rob Brown - MS MVP - Windows Desktop Experience: Bike - Mark Twain said it right.

  • How can I return a result of popup to parent form

    Hi guys

    This is probably a very simple question.

    How can I return a result of popup to parent form?

    Thank you

    Hi dpolonsky,

    due to the fact that your screen popup and parent are in classes separated.

    So create your own set and get methods/functions that can be called from another class.

    Because the variables are known only in the class, they were defined (information hiding), then you must use the functions/methods, that affects the apropriate values.

    in your parent class, you can define a method like:

    void setFromPopup (int intval) {}

    intval = globalintvalfrompopup; globalintcalfrompopup is declared globally within your class

    }

    and in your child (popup) class, you can call via

    .

    .

    int TestInt = 10;

    parentclass.setFromPopup (testint);

    .

    .

    It is a general question of JAVA.

    Hope it helps.

  • Windows security essentials. How can I find the results of an analysis? pls

    Windows 7 Windows Vista

    To avoid confusion and duplication of effort, please post a follow-up later all replies to your original in the MSE forums-online http://answers.microsoft.com/en-us/protect/forum/mse-protect_scanning/how-do-i-find-the-results-of-a-scani-have-tried/4ae5feab-6003-46d1-b3e5-77d9a4e09016 thread

  • A pdf FichierA sent to me and and it's backwards.  How can I rotate 180 degrees?

    A pdf FichierA sent to me and and it's backwards.  How can I rotate 180 degrees?

    With the player, you can change the file, not the actual file view. You can do it via view - display rotation.

    He will present to the original when you reopen the file, however.

  • How to get the result of the query?

    I'm looking to get output something like this...

    If a profile exists on the Pb, I need Exists in the colum

    PROFILE_A EXISTS

    PROFILE_B DOES NOT EXIST

    PROFILE_C EXISTS

    Select distinct profile

    decode (profile, "PASSWORD_PROFILE", "EXISTS", 'THERE is NO') 'STATUS '.

    of dba_profiles;

    How to get the above result? Anyone?

    In the first query, you can add any profile you want to check, and then try the below

    WITH qry1 AS (SELECT profile 'PROFILE_A' FROM dual

    UNION ALL

    SELECT "PROFILE_B" FROM double

    UNION ALL

    SELECT 'DEFAULT' double)

    SELECT the profile

    CASE WHEN EXISTS (SELECT 1 FROM dba_profiles dp

    WHERE dp.profile = q1.profile)

    THEN "EXISTS."

    OTHERWISE "NOT EXIST".

    The END as status

    OF qry1 q1;

    OUTPUT:-

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

    SQL > WITH qry1 AS (SELECT profile 'PROFILE_A' FROM dual

    2. ANY TRADE UNION

    3. SELECT 'PROFILE_B' FROM dual

    4 UNION ALL

    5. SELECT 'DEFAULT' double)

    6. SELECT profile,

    7 CASE WHEN EXISTS (SELECT 1 FROM dba_profiles dp

    8 WHERE dp.profile = q1.profile)

    9 THEN 'EXISTS '.

    10. OTHERWISE "NOT EXIST".

    11 FINISSENT AS status

    Qry1 q1 12;

    PROFILE STATUS

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

    PROFILE_A DOES NOT EXIST

    PROFILE_B DOES NOT EXIST

    DEFAULT VALUE IS

  • How can I rotate a picture of any Word 90 °?

    I'm working on a document that contains hundreds of paintings, mostly adapting very well in portrait mode. However, a number of them have too many columns to fit any direction but the landscape. How can I rotate the pictures 90 ° without having to completely recreate the table in InDesign. All I see is the ability to rotate the text in a cell, which does not solve the problem.

    Set the table in its own frame and rotate the image.

  • How do you check mysqli_numrows ($result) for lines nil?

    I tried a query in PHPmy admin for a single record, and he returned

    0-0 1 lines total.

    Did the same for a nonexistent record and it came with zero results (empty)

    So it seems you need to do some math to whow several lines, there is, as it seems to return 0 when there is only a single record and 0 when there is none.

    How do you check mysql_inumrows ($result) for zero rows returned?

    Its maybe related to the fact that save shots start at 0.

    H Walker

    I don't use mysqli, but you should be able to do the following:

    If (! mysqli_numrows ($result) {})

    no lines have been returned

    }

    You're right PHPmyAdmin starts its numbers of transfer line with zero, not 1, so if there are 25 lines, they are listed as 0-24 lines, mysqli_numrows reports the sum, so it takes 1, not zero.

    Brégent, according to me, he was just using PHPmyAdmin to test his query.

  • How export (unload) result of the query without running query on sql developer

    Hello

    I want to know how export (unload) result of the query without running query on sql developer.

    I know this way.
    1. execution of query
    2. click on "Download" on the results tab
    http://i.stack.imgur.com/CQ4Qb.jpg


    Is he available unload a query result before the race?

    No, but you can do this ask the developer SQL change however, for other users can vote and add weight to the possible future implementation.

    Kind regards
    K.

  • How to display the results of the database from several tables in descending order?

    I have a key word to search for images on my website that allows visitors to search for specific things. My db msysql has 15 tables and for the search function I use a UNION ALL operation to join them any visitor can find all tables at the same time. The problem is that all the results are in ascending order. How can I get the results in descending order.

    SELECT *.

    FROM table1

    Keyword WHERE AS id LIKE %s or %s

    UNION ALL

    SELECT *.

    FROM table2

    Keyword WHERE AS id LIKE %s or %s

    UNION ALL

    SELECT *.

    TABLE 3

    Keyword WHERE AS id LIKE %s or %s

    PS

    I tried this and it doesn't work:

    SELECT * FROM table1 WHERE AS %s or id AS %s ORDER BY id DESC keyword

    and I know that this is not conventional to use SELECT * with so many tables, but believe me, I have my reasons. I'm not trying to do is to wrap the whole UNION within an external SELECTION

    Once more, the order by clause can only appear at the end of the last select statement in a UNION query. You have it on all select statements.

  • How SCORM 2004 maintains the results of the Quiz at the same time.

    How SCORM 2004 maintains the results of the Quiz at the same time and we can manage this is core PHP or XML.

    Sorry, no.

    As I mentioned above.  For information on SCORM works, you must go to the SCORM website and read their information.

Maybe you are looking for