Format of sql query

COMPANY ATTRIBUTE ATTRIBUTE_VAL
IBM Country US
IBM State NY
IBM City New York
HP Country US
HP State CA
HP City Palo Alto
Google Country US
Google State CA
Google City Mountain View
From this o/p i require the o/p as
Country US
IBM State NY
City New York
Country US
HP State CA
City Palo Alto
Country US
Google State CA
City Mountain View
please help me..how can i do it..and how can i implement..Thanks in Advance..
Edited by: startup on Sep 3, 2011 3:24 PM

Try this
SQL> select decode(mod(rn,2),0,company,null) COMPANY, attribute, attribute_val
  2  from (select company, attribute, attribute_val, rownum rn from sample);
COMPANY ATTRIBUTE       ATTRIBUTE_VAL
                Country         US
IBM             State           NY
                City            Newyork
HP              Country         US
                State           CA
HP              City            Palo Alto
                Country         US
Google          City            CA
                City            Mountain ViewOn the other hand, if you want the first occurence to be displayed and the second one to be hidden, it can be done simply as follows using BREAK
SQL> Break on company
SQL>
SQL> select company, attribute, attribute_val from sample;
COMPANY         ATTRIBUTE       ATTRIBUTE_VAL
IBM             Country         US
                State           NY
                City            Newyork
HP              Country         US
                State           CA
                City            Palo Alto
Google          Country         US
                City            CA
                City            Mountain ViewEdited: Alternative SQL included

Similar Messages

  • Checking correct data format using sql query

    1) I got column date of joining which accepts date in below format
    DD-MON-YYYY
    DD-MON-YY
    MON-DD-YYYY
    MON-DD-YY
    Month DD,YYYY
    Question:- how do i check whether all dates in Date of joining column are in above format or not using sql query?
    2) I got one more date column which accepts date in below format
    MMDDYYYY
    YYYYMMDD
    MM/DD/YYYY
    MM/DD/YY
    YYYY/DD/MM
    Question:- how do i check whether all dates in date column are in above format or not using sql query?
    sorry if it is a very simple question since I am new to sql and trying to learn ......Thanks for the answers from the group............

    In short, NO, it's not possible.  If you store dates correctly in the database as DATE datatype then you don't have this problem.  If you store them as VARCHAR2 you have a problem.
    So, you get a date of 20092012
    Is that 20th September 2012?  or is it 20th December 2009?
    What about...
    11-Jan-12
    Is that 11th January 2012 or 12th January 2011?
    Dates should never be stored on the database as strings.  That is why Oracle gives you a DATE datatype so you can store them properly.
    Also, when dates are passed from an application to the database, the application should be passing them as DATE datatype, and the application interface should be designed to accept dates from the user in a format specific to their country/locality and it would then know what that format is and automatically convert it to a DATE datatype before it gets anywhere near the database or any SQL.

  • How to convert milliseconds to date format in sql query

    Hi All,
    The following code is in java.     
    String yourmilliseconds = "1316673707162";**
    Date resultdate = new Date(Long.parseLong(yourmilliseconds));
    could you plese tell me how to convert milliseconds into date format in query and comparing with another date like(sysdate-3)

    Hello,
    http://stackoverflow.com/questions/3820179/convert-epoch-to-date-in-sqlplus-oracle
    Regards

  • Date formatting in sql query

    hi,
    plz need hpl for this
    i have this query :
    q = "select count(*) from logs where dateLog=current_date";i have a logs in this current date but i havn't any data output ?

    Show us more of your code, please--especiall the spot where you read data from the ResultSet.

  • Report from SQL Query -Excel format only show the first page

    Hi,
    I have some Reports from SQL Query. In the display options, I choose Excel as default format. The Excel documents only show the first page. If I choose HTML as default format, it shows every rows.
    Please advise on how to have a complete Excel document as HTML format does.
    Thanks a lot.
    Lina Han

    I beleive when you export Oracle reports to Excel it is the Data that is transferred along with Column Names.
    You cannot export the variables defined on the report.
    Hence you will not be able to export the Footer to excel. (Still looking into it will let you know if i find anything interesting)
    Regards,
    Bhushan Salgar

  • Can we generate the output of SQL Query in XML format ..

    Hi Team,
    Can we generate an XML doc for an SQL Query.
    I've seen in SQL Server 2000.It is generating the output of an SQL Query in xml format.
    select * from emp for xml auto
    The output looks like
    <emp EMPNO="7369" ENAME="SMITH" JOB="CLERK" MGR="7902" HIREDATE="1980-12-17T00:00:00" SAL="2800" DEPTNO="20"/><emp EMPNO="7370" ENAME="SMITH" JOB="CLERK" MGR="7902" HIREDATE="1980-12-17T00:00:00" SAL="2800" DEPTNO="10"/>

    Just a little bit of short hand.
    Get the XML out of your database, via HTTP
    Of course the easiest method is just to return an XMLType from a stored procedure and let the calling routine figure out what to do with it. Instead
    of that way though, I'll show you how to do it via HTTP. It's all completely built into 10g and is super easy to use.
    CREATE OR REPLACE VIEW emps_and_depts AS
    SELECT e.employee_id AS "EmployeeId",
    e.last_name AS "Name",
    e.job_id AS "Job",
    e.manager_id AS "Manager",
    e.hire_date AS "HireDate",
    e.salary AS "Salary",
    e.commission_pct AS "Commission",
    XMLFOREST (
    d.department_id AS "DeptNo",
    d.department_name AS "DeptName",
    d.location_id AS "Location"
    ) AS "Dept"
    FROM employees e, departments d
    WHERE e.department_id = d.department_id
    Some people hear web and immediately start salivating about security issues. Let me address that quickly. Just because you have the HTTP and/or
    FTP servers running in the database, that does not mean you have a security problem. For one, I would hope your databases are behind a firewall.
    Second, with the correct architecture (DMZ, app servers, etc) you can make this data available outside the firewall fairly transparently and third,
    just because it's on the web does not mean the data MUST be available on the internet. This is a good way to make your data available on your
    intranet. If you are worried about people INSIDE your firewall, that still doesn't preclude web based access. Follow Oracle security guidelines.
    Before I show you how to get to your data, let's talk about URLs and URIs. A URL is a Uniform Resource Locater and URI is a Uniform Resource
    Identifier. A URL is the way you would identify a document on the net, i.e. http://www.oracle.com is a URL. A URI is a more generic form of a URL.
    Oracle supports three types of URI: HTTPURIType - basically a URL (which would be like the URL above), XDURIType - a pointer to an XDB resource
    (usually an XML document but can be other objects), and DBURIType - a pointer to database objects.
    It's the DBURIType that we're going to concentrate on here. The DBURIType let's us reference database objects using a file/folder paradigm. The
    format for a DBURI is /oradb/<schema>/<table>. Oradb is shorthand for the database; it is not the database name or SID. My database is named XE
    but I still use oradb in the DBURI. For example, the view we created above is in my XE database, is owned by HR (at least in my case) and is called
    EMPS_AND_DEPTS. This can be referenced as /oradb/HR/EMPS_AND_DEPTS.
    If the view had many rows and you wanted only one of them, you can restrict it by including a predicate. The documentation for XDB has a great
    write up on Using DBURIs.In our case, we are going to write out the entire document. Now that you understand that the DBURI is a pointer to
    objects in our instance, we can use that to access the data as a URL.
    The format for the URL call is http://<machinename>:<port>/<DBURI>
    In my case, my XE database is running on a machine called mach1 and is listening on port 8080. So to see the view we created above, I open my
    browser and navigate to: http//mach1:8080/oradb/HR/EMPS_AND_DEPTS
    The created URL will be as http//mach1:8080/oradb/PUBLIC/EMPS_AND_DEPTS
    If your database is set up correctly and listening on port 8080 (the default), your browser should ask you to login. Login as the user who created the
    view (in my case HR). You should now get an XML document displayed in your browser.
    And that's it. It doesn't get much simpler than that. If you get rid of the descriptive text above, it basically comes down to:
    Create a table or view
    Open your web browser
    Enter a URL
    Enter a user ID and password
    View your XML
    If you notice, Oracle formatted the data as XML for us. Our view returns scalar columns and an XML fragment called Dept. Oracle formatted the
    return results into an XML format.
    And as a side note, if you look closely, you'll see that my URL has PUBLIC where I said to put HR. PUBLIC is a synonym for all objects that your
    logged in user can see. That way, if your user has been granted select access on many schemas, you can use PUBLIC and see any of them.

  • SQL query, function or SP for converting varbinary to image(.jpg format).

    Dear Sir/Mam
    I want SQL query, function or SP which converts binary data to image (.jpg format).
    I m able to convert image (.jpg format) to varbinary. But not able to convert vice versa.
    thanks in advance.

    Binary data is binary data - "image" is only an intrepretation of binary data.  Therefore your question makes little sense in the context of sql server since it does not have any facilities to utilize images.  Since you provide no context, I'm
    guessing that you are trying to retrieve an image stored in the database in a varbinary column and have your application use it as an image.  If so, you should post your question to a forum that is specific to your development environment.  

  • Date format in PL/SQL Procuredure returning SQL Query

    We have parameters (a begin date and end date) that are entered in on a form and then passed as parameters to a report that is displayed using PL/SQL Procedure returning SQL Query. We would like the users to be able to enter the date in the format of mm/dd/yyyy but when we change the format mask on the begin and end date fields we get invalid month entered. I then went into my PL/SQL procedure and changed the code to the following
    q := q || ' WHERE ' ||
    '(:P41_W_BEGIN_DT IS NULL OR (:P41_W_BEGIN_DT IS NOT NULL AND
    :P41_W_BEGIN_DT <= to_char(HARDWARE_INFO.WARRANTY_END_DT,''mm/dd/yyyy')))
    AND
    (:P41_W_END_DT IS NULL OR (:P41_W_END_DT IS NOT NULL AND :P41_W_END_DT >=
    to_char(HARDWARE_INFO.WARRANTY_END_DT,''mm/dd/yyyy''))
    The code is accepted but when we run the report against the parameters it seems to only be looking at the mm/dd constraint it pays no attention to the YYYY constraint. In the to_char condition if I try using single tick marks it errors out. The only way I can get the tick marks to be accepted in the code is to use 2 single tick marks ''. Any ideas on what I am doing wrong.
    Thanks!

    user520671,
    You are doing the conversion in reverse. Because you're applying a function (TO_CHAR) to the columns in your predicate, you'll be negating the use of any indexes. Not prudent from a performance standpoint.
    Instead, convert your input values to a date and do the comparison that way.
    q := q || ' WHERE ' ||
    '(:P41_W_BEGIN_DT IS NULL OR
        (:P41_W_BEGIN_DT IS NOT NULL AND to_date(:P41_W_BEGIN_DT,''MM/DD/YYYY'') <= HARDWARE_INFO.WARRANTY_END_DT))
    AND
    (:P41_W_END_DT IS NULL OR
        (:P41_W_END_DT IS NOT NULL AND to_date(:P41_W_END_DT,''MM/DD/YYYY'') >= HARDWARE_INFO.WARRANTY_END_DT))'Also ensure that you are truly passing in a 4-digit year.
    Joel

  • Sql query row format

    Hi .
    Please look at the below table
    EMP_ID EMP_NAME
    1 Micel
    2 Jonh
    3 Steev
    I need to display all emp_id & corresponding emp_name row wize.
    Ie in the below format
    1 2 3
    Micel Jonh Steev
    But for this I need to use SQL only .
    Summery : I need to display the columns in a table in row using SQL query only.
    Is there any command in SQL which can do it for me , or else how to do it , Can u help me in this regard??

    A summary of different string aggregation techniques can be found here
    http://www.oracle-base.com/articles/10g/StringAggregationTechniques.php

  • Formatting SQL Query output in HTMLDB

    Guys, I would like to show the result in report of SQL Query to be formatted in such way:
    I got a table USERS with columns:FIrst_name, second_name, team_name, team_leader.
    So in a report in HTMLDB I would like to format the result to show all employees , 500 of them, on 1 page, and show in a header for each team, team leader and team name with the rest of team underneath,so only two columns, break on team_leader or team_name, then next team, and then, NOT all this in one long display, but in three sets of result, spread in wide of report, cause the screen dimensions are quite wide.I tried few things but they do not work. I can break on tem leader or team name, but all this is still in one long set of result, not in 3 or 4 tables sets.
    help
    Jozef

    Hi Kamal,
    I am using Oracle Applications 11.5.7 iProcurement. Database is 9i. I need to place a link in iProcurement homepage using AK Developer and when clicked it should run a query and output the result in a browser.
    help appreciated
    thanks

  • Sql query need for date format''Mon 23 Apr 16:45 pm'

    i need SQL query to print date like 'Mon 23 Apr 16:45 pm' format

    SQL> select to_char(sysdate,'Dy fmddfm Mon hh24:mi pm','nls_date_language=american') from dual
      2  /
    TO_CHAR(SYSDATE,'DY
    Fri 27 Apr 13:04 pm
    1 rij is geselecteerd.Regards,
    Rob.

  • Sql query required to fetch data in desired format.

    Hi All,
    Greetings,
    I have 1 table called sessioninfo. below are the table structure and table data,
    CREATE TABLE HR.SESSIONINFO
    REPORT_DATE DATE,
    REPORT_TIME VARCHAR2(100 BYTE),
    SESSION1 NUMBER,
    SESSION2 NUMBER,
    SESSION3 NUMBER,
    SESSION4 NUMBER,
    SESSION5 NUMBER,
    SESSION6 NUMBER,
    SESSION7 NUMBER,
    SESSION8 NUMBER,
    SESSION9 NUMBER,
    SESSION10 NUMBER,
    SESSION11 NUMBER,
    SESSION12 NUMBER,
    SESSION13 NUMBER,
    SESSION14 NUMBER,
    SESSION15 NUMBER,
    SESSION16 NUMBER,
    SESSION17 NUMBER,
    SESSION18 NUMBER,
    SESSION19 NUMBER,
    SESSION20 NUMBER,
    SESSION21 NUMBER,
    SESSION22 NUMBER,
    SESSION23 NUMBER,
    SESSION24 NUMBER,
    SESSION25 NUMBER,
    SESSION26 NUMBER,
    SESSION27 NUMBER,
    SESSION28 NUMBER,
    SESSION29 NUMBER,
    SESSION30 NUMBER,
    SESSION31 NUMBER,
    SESSION32 NUMBER,
    SESSION33 NUMBER,
    SESSION34 NUMBER,
    SESSION35 NUMBER,
    SESSION36 NUMBER,
    SESSION37 NUMBER,
    SESSION38 NUMBER,
    SESSION39 NUMBER,
    SESSION40 NUMBER,
    SESSION41 NUMBER,
    SESSION42 NUMBER,
    SESSION43 NUMBER
    REPORT_DATE     REPORT_TIME     SESSION1     SESSION2     SESSION4     SESSION5     SESSION6     SESSION7     SESSION8     SESSION9     SESSION10     SESSION11     SESSION14     SESSION15     SESSION16     SESSION17     SESSION18     SESSION19     SESSION20     SESSION21     SESSION22     SESSION23     SESSION24     SESSION25     SESSION26     SESSION27     SESSION30     SESSION31     SESSION32     SESSION35     SESSION36     SESSION37     SESSION38     SESSION40     SESSION41     SESSION42     SESSION43
    8/22/2011     7:01:00     2     1     2               2               1          2     1     2     1     1     1     1     1     2     1     1     0     1     2     2     0     1     1     2     2     2     2     1     1     2
    8/22/2011     7:15:41     1     2     2               2               2          3     1     2     2     1     1     1     1     2     3     1     3     2     2     5     1     1     2     1     2     1     2     1     1     1
    8/22/2011     7:30:45     1     1     4               3               2          3     2     2     3     2     1     2     2     1     2     1     2     2     2     2     2     3     2     1     2     2     2     2     1     2
    8/22/2011     7:46:10     1     2     1               2               2          2     3     3     3     1     1     3     4     3     1     5     3     3     2     2     3     3     2     3     3     2     3     2     2     2
    8/22/2011     8:01:05     5     5     7               4               2          3     3     5     2     6     2     3     2     3     4     4     3     3     4     4     2     4     3     4     2     3     4     3     3     4
    8/22/2011     8:15:48     2     4     4               4               5          3     3     3     3     4     3     4     4     4     3     3     5     4     4     4     7     3     3     4     3     3     3     5     4     5
    8/22/2011     8:30:50     4     5     3               5               4          3     5     5     5     4     5     5     6     5     4     6     7     3     3     5     7     5     4     4     4     4     5     8     3     4
    8/22/2011     8:45:42     4     7     5               5               6          5     4     5     4     4     6     4     6     4     7     5     4     4     7     5     7     5     5     5     6     3     7     6     5     5
    8/22/2011     9:01:24          5     6               6               5          3     6     8     6     7     6     4     5     4     7     7     5     6     4     7     5     4     5     4     4     5     6     11     4     6
    8/22/2011     9:15:56     5     6     4               4               4          6     6     4     13     5     8     5     4     6     3     5     4     6     8     5     5     6     7     5     5     7     5     7     6     8
    8/22/2011     9:30:45     7     7     5               5               12          7     4     5     5     6     15     8     6     7     9     9     5     6     5     5     6     5     5     8     9     9     7     5     7     8
    8/22/2011     9:45:41     5     10     7               8               7          6     7     5     6     8     8     7     7     9     5     6     7     9     7     8     6     11     6     10     8     10     6     12     9     12
    8/22/2011     10:01:00     8     5     5               9               6          7     6     7     5     7     7     7     9     6     6     7     7     7     11     8     7     8     10     8     6     5     7     6     9     6
    8/22/2011     10:16:16     6     8     6               14               9          7     7     10     8     8     23     11     16     6     6     4     8     10     9     7     7     10     7     14     12     7     5     10     11     6
    8/22/2011     10:31:09     7     9     8               9               8          7     8     8     9     7     20     11     6     8     9     9     11     8     14     11     12     7     8     7     12     10     12     8     7     11
    8/22/2011     10:46:11     7     8     10               8               12          7     7     9     8     9     8     11     10     12     9     8     9     7     8     6     8     9     10     10     9     8     9     18     7     9
    8/22/2011     11:01:10     8     7     6               9               13          8     10     9     11     9     10     8     7     8     11     14     6     9     12     8     8     7     17     13     12     7     16     19     14     9
    8/22/2011     11:15:40     11     12     9               10               10          11     8     10     9     10     10     8     7     9     8     10     6     10     11     10     8     13     14     8     10     13     9     10     12     10
    8/22/2011     11:31:45     8     7     10               11               10          12     11     11     8     13     30     11     8     8     8     7     8     12     7     10     9     10     12     15     11     11     10     9     9     11
    8/22/2011     11:46:05     9     7     8               7               12          6     11     8     6     9     8     10     7     8     14     9     8     13     8     23     13     7     12     8     12     8     12     10     9     9
    8/22/2011     12:01:22     9     10     10               11               11          10     13     7     9     13     11     11     10     10     8     10     13     12     12     11     10     8     7     8     10     9     20     23     10     9
    8/22/2011     12:16:02     8     9     11               10               9          11     12     9     22     9     7     7     6     9     10     11     9     8     9     8     25     6     9     10     12     18     16     10     7     7
    8/22/2011     12:31:11     18     11     8               14               9          11     5     8     9     7     8     7     8     7     21     10     13     7     6     10     8     7     10     8     7     10     7     10     10     10
    8/22/2011     12:46:15     10     7     11               12               8          13     16     14     23     10     8     10     11     33     9     15     13     16     14     9     10     25     11     7     11     11     9     9     11     8
    8/22/2011     13:00:57     13     16     8               10               16          12     6     9     10     7     22     11     11     8     7     15     8     11     10     14     8     15     9     10     10     10     10     8     8     9
    8/22/2011     13:15:57     6     21     14               26               7          10     9     13     10     12     13     7     8     18     11     10     10     9     25     17     8     21     7     11     7     18     17     9     17     6
    8/22/2011     13:30:53     9     12     6               5               9          14     6     9     7     15     10     6     8     12     8     9     7     9     10     12     7     8     13     11     13     7     8     10     7     10
    8/22/2011     13:46:05     6     13     8               9               10          10     8     22     8     11     8     10     10     11     8     10     6     9     9     8     8     7     11     7     9     8     8     7     10     8
    8/22/2011     14:00:46     13     8     8               11               9          12     8          9     14     9     9     11     14     10     9     11     14     8     16     23     9     6     12     8     8     9     9     9     18
    8/22/2011     14:15:46     8     13     9               7               9          29     7     9     18     10     10     7     9     9     17     10     10     10     12     10     13     12     15     7     12     8     13     9     8     9
    8/22/2011     14:31:13     9     11     11               20               10          9     8     9     9     9     8     25     9     7     11     8     9     10     12     7     10     23     11     9     10     9     10     9     9     9
    8/22/2011     14:45:52     9     12     12               13               25          11     17     8     12     9     11     10     11     10     11     8     21     11     8     7     9     25     11     10     11     10     13     11     8     8
    8/22/2011     15:00:52     7     33     11               9               7          12     9     11     12     10     34     10     11     9     7     9     11     10     9     29     11     10     13     8     10     13     17     15     14     12
    8/22/2011     15:16:16     8     10     6               25               13          10     9     9     13     13     15     10     10     17     8     12     10     11     61     9     9     9     26     10     10     12     10     11     13     9
    8/22/2011     15:31:07     9     10     9               11               11          9     14     14     12     12     19     12     8     11     11     9     9     12     0     11     10     7     24     11     11     9     19     22     10     9
    8/22/2011     15:45:44     7     10     9               25               14          11     23     10     34     11     7     8     7     7     15     16     8     5     9     9     10     10     7     9     7     7     10     10     21     13
    8/22/2011     16:00:59     27     9     12               10               11          9     16     16     16     19     10     10     11     9     7     17     10     10     6     27     11     12     9     20     9     10     18     12     12     9
    8/22/2011     16:15:42     12     9     8               8               9          18     10     9     25     11     8     8     16     10     13     11     23     33     12     8     11     20     9     8     9     22     10     8     11     10
    8/22/2011     16:31:03     10     10     9               14               9          14     10     15     9     14     9     10     11     22     8     9     10     13     8     10     10     8     11     13     10     9     11     10     11     9
    8/22/2011     16:45:44     10     9     10               11               13          10     10     12     8     9     10     9     16     7     8     9     8     14     7     10     11     5     7     11     8     9     10     11     20     11
    8/22/2011     17:00:46     9     12     10               9               11          7     12     10     9     7     11     8     9     7     8     12     10     10     8     15     12     12     8     6     6     10     9     10     9     9
    8/22/2011     17:16:04     7     7     7               20               9          9     9     8     9     8     10     20     8     17     31     10     10     9     8     17     9     8     8     16     8     6     14     10     9     9
    8/22/2011     17:31:22     6     7     9               9               8          6     8     7     8     9     5     18     8     7     8     7     7     12     13     11     16     8     7     25     9     8     22     12     14     10
    8/22/2011     17:45:56     12     6     11               8               8          8     7     6     7     7     7     15     9     8     10     10     13     14     5     9     9     19     9     6     5     14     17     9     14     6
    8/22/2011     18:00:53     8     8     6               8               7          8     5     8     6     7     6     8     16     5     7     7     9     15     8     8     6     8     6     8     9     7     10     7     6     10
    8/22/2011     18:16:04     6     7     6               19               12          12     9     9     8     6     6     7     12     8     7     7     5     6     7     7     7     10     6     7     8     6     6     6     6     9
    8/22/2011     18:31:00     7     6     9               6               11          6     6     6     6     9     6     5     7     7     12     9     18     8     5     5     5     7     5     6     10     6     10     10     15     8
    8/22/2011     18:45:43     17     13     22               11               5          6     6     6     12     4     5     5     5     4     5     5     9     8     4     6     7     5     6     6     7     5     6     7     6     5
    8/22/2011     19:00:57     7     7     4               7               15          17     6     7     10     8     4     6     6     4     5     6     5     13     7     5     6     3     5     5     6     6     16     7     6     4
    8/22/2011     19:15:47     5     4     6               5               4          4     4     4     6     3     5     5     6     4     3     10     6     6     4     7     5     6     6     6     7     5     4     4     8     5
    8/22/2011     19:30:39     5     5     7               4               8          12     3     5     3     5     6     6     4     5     4     4     5     6     4     6     5     4     4     6     7     4     4     6     4     3
    8/22/2011     19:45:39     5     5     15               5               6          6     6     4     5     8     6     6     6     4     6     5     7     4     3     5     3     3     3     4     6     7     6     4     5     5
    8/22/2011     20:01:02     3     4     4               5               3          6     4     5     5     4     2     3     4     4     5     6     4     4     5     4     5     5     4     5     3     5     5     3     4     5
    8/22/2011     20:15:38     6     4     3               4               5          5     4     2     5     3     4     6     4     4     4     5     2     2     4     4     5     4     3     2     4     3     5     4     3     4in short table contains the information about some sessions.
    now i want the output like below, (highest session for the report_date and report_time with the name of session)
    REPORT_DATE     REPORT_Hour     SESSION_CODE     SESSION_VALUE
    8/22/2011     7     Session30     5
    8/22/2011     7     Session24     5
    8/22/2011     8     Session41     8
    8/22/2011     9     Session19     15
    8/22/2011     10          
    8/22/2011     11          
    8/22/2011     12          
    8/22/2011     13          
    8/22/2011     14          
    8/22/2011     15          
    8/22/2011     16          
    8/22/2011     17          
    8/22/2011     18          
    8/22/2011     19          
    8/22/2011     20          How can i get using sql query.
    Any help appriciated.

    In version 11 you can use UNPIVOT.
    A sample with just three columns:
    SQL> with sessioninfo as (
      2     select date '2011-08-29' report_date, '7:12:34' report_time,
      3            1 session1, cast(null as number) session2, 11 session3 from dual union all
      4     select date '2011-08-29' report_date, '7:56:00' report_time,
      5            4 session1, 2 session2, cast(null as number) session3 from dual union all
      6     select date '2011-08-29' report_date, '14:44:44' report_time,
      7            cast(null as number) session1, cast(null as number) session2, 2 session3 from dual
      8  )
      9  select
    10  report_date,
    11  substr(lpad(report_time,8),1,2) report_hour,
    12  max(session_code) keep (dense_rank last order by session_value) session_code,
    13  max(session_value) session_value
    14  from sessioninfo
    15  unpivot(
    16     session_value for session_code in (
    17        session1 as 'Session1',
    18        session2 as 'Session2',
    19        session3 as 'Session3'
    20     )
    21  )
    22  group by
    23  report_date,
    24  substr(lpad(report_time,8),1,2)
    25  order by
    26  report_date,
    27  substr(lpad(report_time,8),1,2)
    28  ;
    REPORT_D RE SESSION_ SESSION_VALUE
    11-08-29  7 Session3            11
    11-08-29 14 Session3             2For your 43 columns you just need to expand the list in lines 17,18,19 - the rest will work fine.
    (Your best way would actually be to change your datamodel to be more useful in a relational database - having columns session1, session2,...,session43 is not a very useful datamodel. Storing a timestamp in a VARCHAR2 is also not very good for the database.)

  • Formatting SQL query as of iSQL*PLUS query output format through PL/SQL

    This is my procedure through which i m sending an e-mail. In my e-mail i want to show output from error log query. I can show it in simple text but i want the output for the error log to be formatted as the query result is formated in iSQLPlus.
    In simple words instead of the message i want to send the query result which is formatted as iSqlPlus query output. Thankx in advance. Please provide a simple solution that iSqlPlus engine recieves my any given query and gives the formatted output for that query in the e-mail
    DECLARE
    conn utl_smtp.connection;
    v_log number;
    BEGIN
    conn := demo_mail.begin_mail(
    sender => '<sender-email>',
    recipients => '<recipient-email>',
    subject => 'HTML E-mail Test',
    mime_type => 'text/html');
    </h1>
    -- This method will be used for oracle error reporting');
    demo_mail.write_text(
    conn => conn,
    message => '<h1>Hi! This is a <i>test msg</i>);
    demo_mail.end_mail( conn => conn );
    END;

    oe@ORA102>
        select * from
        (select rownum r1, p.promo_id, promo_name from promotions p where rownum <5)
        full outer join
        (select rownum r2, w.warehouse_id from warehouses w where rownum <4)
       on (r1 = r2)
    oe@ORA102>/
            R1   PROMO_ID PROMO_NAME                   R2 WAREHOUSE_ID
             1          1 everyday low price            1            1
             2          2 blowout sale                  2            2
                                                        3            3
    oe@ORA102>But there should be "some connection" between the result sets, otherwise the resulting result set become unpredictable / meaning less.
    I am joining on ROWNUM, Do you want every row from emp related to every row from stocks (like a cartesian join)?
    Trying to understand the requirement.
    vr,
    Sudhakar B.

  • Comma delimited in Sql query decode function errors out

    Hi All,
    DB: 11.2.0.3.0
    I am using the below query to generate the comma delimited output in a spool file but it errors out with the message below:
    SQL> set lines 100 pages 50
    SQL> col "USER_CONCURRENT_QUEUE_NAME" format a40;
    SQL> set head off
    SQL> spool /home/xyz/cmrequests.csv
    SQL> SELECT
    2 a.USER_CONCURRENT_QUEUE_NAME || ','
    3 || a.MAX_PROCESSES || ','
    4 || sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'Q',1,0),0)) Pending_Standby ||','
    5 ||sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'I',1,0),0)) Pending_Normal ||','
    6 ||sum(decode(b.PHASE_CODE,'R',decode(b.STATUS_CODE,'R',1,0),0)) Running_Normal
    7 from FND_CONCURRENT_QUEUES_VL a, FND_CONCURRENT_WORKER_REQUESTS b
    where a.concurrent_queue_id = b.concurrent_queue_id AND b.Requested_Start_Date <= SYSDATE
    8 9 GROUP BY a.USER_CONCURRENT_QUEUE_NAME,a.MAX_PROCESSES;
    || sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'Q',1,0),0)) Pending_Standby ||','
    ERROR at line 4:
    ORA-00923: FROM keyword not found where expected
    SQL> spool off;
    SQL>
    Expected output in the spool /home/xyz/cmrequests.csv
    Standard Manager,10,0,1,0
    Thanks for your time!
    Regards,

    Get to work immediately on marking your previous questions ANSWERED if they have been!
    >
    I am using the below query to generate the comma delimited output in a spool file but it errors out with the message below:
    SQL> set lines 100 pages 50
    SQL> col "USER_CONCURRENT_QUEUE_NAME" format a40;
    SQL> set head off
    SQL> spool /home/xyz/cmrequests.csv
    SQL> SELECT
    2 a.USER_CONCURRENT_QUEUE_NAME || ','
    3 || a.MAX_PROCESSES || ','
    4 || sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'Q',1,0),0)) Pending_Standby ||','
    5 ||sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'I',1,0),0)) Pending_Normal ||','
    6 ||sum(decode(b.PHASE_CODE,'R',decode(b.STATUS_CODE,'R',1,0),0)) Running_Normal
    7 from FND_CONCURRENT_QUEUES_VL a, FND_CONCURRENT_WORKER_REQUESTS b
    where a.concurrent_queue_id = b.concurrent_queue_id AND b.Requested_Start_Date <= SYSDATE
    8 9 GROUP BY a.USER_CONCURRENT_QUEUE_NAME,a.MAX_PROCESSES;
    || sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'Q',1,0),0)) Pending_Standby ||','
    >
    Well if you want to spool query results to a file the first thing you need to do is write a query that actually works.
    Why do you think a query like this is valid?
    SELECT 'this, is, my, giant, string, of, columns, with, commas, in, between, each, word'
    GROUP BY this, is, my, giant, stringYou only have one column in the result set but you are trying to group by three columns and none of them are even in the result set.
    What's up with that?
    You can only group by columns that are actually IN the result set.

  • Tweak for sql query - help needed for smalll change

    Hi.
    I am trying to run a script that checks for used space on all tablespaces and returns the results.
    So far so good:
    set lines 200 pages 2000
    col tablespace_name heading 'Tablespace' format a30 truncate
    col total_maxspace_mb heading 'MB|Max Size' format 9G999G999
    col total_allocspace_mb heading 'MB|Allocated' format 9G999G999
    col used_space_mb heading 'MB|Used' format 9G999G999D99
    col free_space_mb heading 'MB|Free Till Max' like used_space_mb
    col free_space_ext_mb heading 'MB|Free Till Ext' like used_space_mb
    col pct_used heading '%|Used' format 999D99
    col pct_free heading '%|Free' like pct_used
    break on report
    compute sum label 'Total Size:' of total_maxspace_mb total_allocspace_mb used_space_mb - free_space_mb (used_space_mb/total_maxspace_mb)*100 on report
    select
    alloc.tablespace_name,
    (alloc.total_allocspace_mb - free.free_space_mb) used_space_mb,
    free.free_space_mb free_space_ext_mb,
    ((alloc.total_allocspace_mb - free.free_space_mb)/alloc.total_maxspace_mb)*100 pct_used,
    ((free.free_space_mb+(alloc.total_maxspace_mb-alloc.total_allocspace_mb))/alloc.total_maxspace_mb)*100 pct_free
    FROM (SELECT tablespace_name,
    ROUND(SUM(CASE WHEN maxbytes = 0 THEN bytes ELSE maxbytes END)/1048576) total_maxspace_mb,
    ROUND(SUM(bytes)/1048576) total_allocspace_mb
    FROM dba_data_files
    WHERE file_id NOT IN (SELECT FILE# FROM v$recover_file)
    GROUP BY tablespace_name) alloc,
    (SELECT tablespace_name,
    SUM(bytes)/1048576 free_space_mb
    FROM dba_free_space
    WHERE file_id NOT IN (SELECT FILE# FROM v$recover_file)
    GROUP BY tablespace_name) free
    WHERE alloc.tablespace_name = free.tablespace_name (+)
    ORDER BY pct_used DESC
    The above returns something like this:
    MB MB % %
    Tablespace Used Free Till Ext Used Free
    APPS_TS_ARCHIVE 1,993.13 54.88 97.32 2.68
    APPS_TS_TX_IDX 14,756.13 1,086.88 91.37 8.63
    APPS_TS_TX_DATA 20,525.75 594.25 80.18 19.82
    APPS_TS_MEDIA 6,092.00 180.00 74.37 25.63
    APPS_TS_INTERFACE 13,177.63 366.38 71.49 28.51
    The above works fine, but I would like to further change the query so that only those tablespaces with free space less than 5% (or used space more than 95%) are returned.
    I have been working on this all morning and wanted to open it up to the masters!
    I have tried using WHERE pct_used > 95 but to no avail.
    Any advice would be appreciated.
    Many thanks.
    10.2.0.4
    Linux Red Hat 4.

    Thanks for that.
    What is confusing is that the below query works for every other (about 10 others) database but not this one (?)
    SQL> set lines 200 pages 2000
    SQL>
    SQL> col tablespace_name heading 'Tablespace' format a30 truncate
    SQL> col total_maxspace_mb heading 'MB|Max Size' format 9G999G999
    SQL> col total_allocspace_mb heading 'MB|Allocated' format 9G999G999
    SQL> col used_space_mb heading 'MB|Used' format 9G999G999D99
    SQL> col free_space_mb heading 'MB|Free Till Max' like used_space_mb
    SQL> col free_space_ext_mb heading 'MB|Free Till Ext' like used_space_mb
    SQL> col pct_used heading '%|Used' format 999D99
    SQL> col pct_free heading '%|Free' like pct_used
    SQL>
    SQL> break on report
    SQL> compute sum label 'Total Size:' of total_maxspace_mb total_allocspace_mb used_space_mb - free_space_mb (used_space_mb/total_maxspace_mb)*100 on report
    SQL>
    SQL> select /*+ALL_ROWS */
    2 alloc.tablespace_name,
    3 alloc.total_maxspace_mb,
    4 alloc.total_allocspace_mb,
    5 (alloc.total_allocspace_mb - free.free_space_mb) used_space_mb,
    6 free.free_space_mb+(alloc.total_maxspace_mb-alloc.total_allocspace_mb) free_space_mb,
    7 free.free_space_mb free_space_ext_mb,
    8 ((alloc.total_allocspace_mb - free.free_space_mb)/alloc.total_maxspace_mb)*100 pct_used,
    9 ((free.free_space_mb+(alloc.total_maxspace_mb-alloc.total_allocspace_mb))/alloc.total_maxspace_mb)*100 pct_free
    10 FROM (SELECT tablespace_name,
    11 ROUND(SUM(CASE WHEN maxbytes = 0 THEN bytes ELSE maxbytes END)/1048576) total_maxspace_mb,
    12 ROUND(SUM(bytes)/1048576) total_allocspace_mb
    13 FROM dba_data_files
    14 WHERE file_id NOT IN (SELECT FILE# FROM v$recover_file)
    15 GROUP BY tablespace_name) alloc,
    16 (SELECT tablespace_name,
    17 SUM(bytes)/1048576 free_space_mb
    18 FROM dba_free_space
    19 WHERE file_id NOT IN (SELECT FILE# FROM v$recover_file)
    20 GROUP BY tablespace_name) free
    21 WHERE alloc.tablespace_name = free.tablespace_name (+)
    22 ORDER BY pct_used DESC
    23 /
    ((alloc.total_allocspace_mb - free.free_space_mb)/alloc.total_maxspace_mb)*100 pct_used,
    ERROR at line 8:
    ORA-01476: divisor is equal to zero

Maybe you are looking for

  • HTTP connection is thread isn't working

    Hi, I am writing an application that streams an input from a HTML webpage. I have put the connection in a separate thread (as advised). I need the input steam to be saved in the RMS (which is contained in the main part of the program) and as the thre

  • HT2008 Can I use iChat with Skype?

    I have Skype on my iphone. Can I use iChat on my iMac and talk over Skype?

  • '09 Having underlying audio troubles after export

    So, I'm creating a video in iMovie '09 and everything has went smoothly. I can watch it in the project screen and everything looks perfect...but, when I export the movie(in any format), I get unexpected audio inserts in random spots in the movie. The

  • Wirerless printing

    HP Envy 4500 I am having trouble printing with the wireless connection. When I type in the IP address the computor recognizing the printer and will the "Network Configuration page". The control panel on the printer recognizes the router and says the

  • Sound problems and not speaker

    "i have a bb 9320 and my sound keeps going off or really quiet,