Row query results to column format

I have a table as follows:
DT | SN | Type | Value
1/1/15 1:15 | 1 | HID | 123
1/1/15 1:15 | 1 | VAL | 456
1/2/15 1:15 | 1 | HID | 123
1/2/15 1:15 | 1 | VAL | 111
1/4/15 1:15 | 2 | HID | 222
1/4/15 1:15 | 2 | VAL | 233
1/5/15 1:15 | 3 | HID | 333
1/5/15 1:15 | 3 | VAL | 344
How to I construct a query in the below format...
DT | SN | HID | VAL
1/1/15 1:15 | 1 | 123 | 456
1/2/15 1:15 | 1 | 123 | 111
1/4/15 1:15 | 2 | 222 | 233
1/5/15 1:15 | 3 | 333 | 344

Hi engrforever,
Use a PIVOT to do it. Modified the date values here:
Create TABLE #TEMP1
(DT datetime NULL,
SN int NULL,
[Type] varchar(12) NULL,
Value int NULL
INSERT INTO #temp1(DT, SN,Type,Value) VALUES (GETDATE()-4,1,'HID',123),
(GETDATE()-4,1,'VAL',456),
(GETDATE()-3,1,'HID',123),
(GETDATE()-3,1,'VAL',111),
(GETDATE()-2,1,'HID',222),
(GETDATE()-2,1,'VAL',233)
SELECT * FROM
(SELECT DT,SN, TYPE, Value FROM #temp1) DT
PIVOT
( SUM (VALUE)
FOR TYPE IN ([HID],[VAL])
) PT
DROP TABLE #TEMP1

Similar Messages

  • Broadcasting the precalculated query results in web format

    Hi Experts,
    We have a requirement  where we need to broadcast the precalculated query results in web format, we know that we can do this through workbook, but we want to do it in web format to avoid the limitations of the workbook like number of rows in workbook limited to 65000 etc.,
    We are using 3.x environment,
    please suggest that is this functionality can be achievable, if yes how?
    Thanks in advance,
    -Vinay

    Hi All, this problem was resolved.
    Thanks,
    vinay

  • Output query result in XML format

    folks:
    could anybody provide more information
    about how to generate query result in
    xml format? looks like DBXML is for demo only
    and i have heard that PLSQL XML parser is the way to go; so far i have not found
    a sample yet on how to actually
    do the XML generation; if you know more
    about it, please let me know; thanks in
    advance.
    Bill

    Here are some sample apps that might interest you:
    [list]
    [*][url [URL=http://technet.oracle.com/tech/xml/xsql_servlet/index2.htm?Code&files/ffhome.html]The]http://technet.oracle.com/tech/xml/xsql_servlet/index2.htm?Code&files/ffhome.html]The XML Flight Finder
    [*][url [URL=http://technet.oracle.com/sample_code/tech/xml/xmlb2b/doc/xb2bhome.html]B2B]http://technet.oracle.com/sample_code/tech/xml/xmlb2b/doc/xb2bhome.html]B2B with XML
    [list]
    Enjoy,
    -rh

  • Returning query results in XML format

    Besides using custom tag library, does anyone know any methods or techniques that i can retrive the query results from database in XML format. for example, i have a table named student in database like this:
    StudentNo  Name   Gender  Degree
    123       Tony    male    B.Comp.Sci.
    456       Tom     male    B.Fiance
    343       Mary    female  B.Accountingso, if i have query select * from table student, i would get someting like the following:
    <row>
    <studentNo>123</studentNo>
    <name>Tony</name>
    <Gender>male</Gender>
    <Degree>B.Comp.Sci</Degree>
    </row>
    <row>
    <studentNo>456</studentNo>
    <name>Tom</name>
    <Gender>male</Gender>
    <Degree>B.Finace</Degree>
    </row>
    The reason i am asking for this is i need query results returned in XML format, so i can wrap XSLT tag around, and apply for HTML, WML, and XHTML template resprectively so i can display them on different terminals. any help is appreciated.

    I have this method in a ResultSetMapper class:
         * Return result sets as an XML stream, with root tag named
         * "results", one "result" tag per row, and "result" child tag
         * names equal to the column name
         * @param query result set
         * @param list of column names to include in the result map
         * @throws SQLException if the query fails
         * @throws JDOMException if the XML stream creation fails
        public static final Document toJDOM(ResultSet rs, List wantedColumnNames)
            throws SQLException, JDOMException
            Element rows         = new Element("results");
            int numWantedColumns = wantedColumnNames.size();
            while (rs.next())
                Element row = new Element("result");
                for (int i = 0; i < numWantedColumns; ++i)
                    String columnName   = (String)wantedColumnNames.get(i);
                    Object value = rs.getObject(columnName);
                    row.addContent(new Element(columnName).setText(value.toString()));
                rows.addContent(row);
            return new Document(rows);
        }It uses JDOM from www.jdom.org. - MOD

  • SQL Developer 4.0 EA3. query result in export format problem

    Query output in export format (insert, csv, xml,...) is not working in 4.0 EA3.
    Query executed as script (F5) does not display anything:
    select /*insert*/ * from dual;
    SQL Developer 3 displays the result correctly:
    REM INSERTING into dual
    SET DEFINE OFF;
    Insert into "dual" (DUMMY) values ('X');
    About
    Oracle SQL Developer 4.0.0.13
    Version 4.0.0.13
    Build MAIN-13.30
    IDE Version: 12.1.3.0.41.131007.2031
    Product ID: oracle.sqldeveloper
    Product Version: 12.2.0.13.30
    Version
    Component    Version
    =========    =======
    Oracle IDE    4.0.0.13.30
    Java(TM) Platform    1.7.0_07
    Versioning Support    4.0.0.13.30

    That's been fixed for the next release.

  • Download BW Query results in HTML format

    On a daily basis, I need to get BW query results exported to an HTML extract file on the BW server.  The HTML file will then be transferred to a Windows server for use in a dashboard view by a third party developed portal.  The query has an input variable for period.
    What is the best/simplest method for scheduling an existing BW query to run on a daily basis to produce an HTML  extract / download file to the BW server file system?
    We are currently on BW 7.01 SP6.  We just completed a technical upgrade from BW3.5, but did not do a functional upgrade.
    I have read through many postings but have been unable to find a solution.  I have reviewed RSCRM_BAPI u2013 no html format.
    Any help would be greatly appreciated.
    Thanks,
    Mark Norton

    Hello Mark...
    Amer is right. You can create a broadcast setting to daily sends your
    query as MHTML format.
    You can send to your e-mail but there is a possibility to save on KM and CM repository.
    There is a little explanation on the link below:
    http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/5c6b686a-0901-0010-8aab-c4d0e5a093a7?quicklink=index&overridelayout=true
    To create a broadcast setting, open the query via Query Designer.
    Click "Query" -> "Publish" -> "BEx Broadcaster"
    While you are creating the broadcast, there is a "Output Format" option.
    There is MHTML.
    I hope you find this information useful.
    Thanks
    Edward

  • Query Help Preserve Column Format

    I have the following query that cast the ouput into colunms, my issues is that I need to extend the query to add the returned results of Connector_1, Cache_1, EPS_1, Sent_1, Condition_1....  ANy ideas how I can accomplish this preserving the same columns?
    SELECT NodeName,
    MAX(CASE WHEN Poller_Name='Connector' THEN CAST(Status AS varchar) ELSE '' END) Connector,
    MAX(CASE WHEN Poller_Name='Cache' THEN CAST(Status AS varchar) ELSE '' END) Cache,
    MAX(CASE WHEN Poller_Name='EPS' THEN CAST(Status AS varchar) ELSE '' END) EPS,
    MAX(CASE WHEN Poller_Name='Sent' THEN CAST(Status AS varchar) ELSE '' END) Sent,
    MAX(CASE WHEN Poller_Name='Condition' THEN CAST(Status AS varchar) ELSE '' END) Condition
    FROM
    SELECT
    Nodes.Caption AS NodeName,CustomNodePollers_CustomPollers.GroupName As GroupName ,CustomNodePollers_CustomPollers.UniqueName AS Poller_Name, CustomNodePollerStatus_CustomPollerStatus.Status AS Status
    FROM
    ((Nodes INNER JOIN CustomPollerAssignment CustomNodePollerAssignment_CustomPollerAssignment ON (Nodes.NodeID = CustomNodePollerAssignment_CustomPollerAssignment.NodeID))
    INNER JOIN CustomPollers CustomNodePollers_CustomPollers ON (CustomNodePollerAssignment_CustomPollerAssignment.CustomPollerID = CustomNodePollers_CustomPollers.CustomPollerID))
    INNER JOIN CustomPollerStatus CustomNodePollerStatus_CustomPollerStatus ON (CustomNodePollerAssignment_CustomPollerAssignment.CustomPollerAssignmentID = CustomNodePollerStatus_CustomPollerStatus.CustomPollerAssignmentID)
    WHERE
    (CustomNodePollers_CustomPollers.UniqueName = 'Connector') OR
    (CustomNodePollers_CustomPollers.UniqueName = 'Cache') OR
    (CustomNodePollers_CustomPollers.UniqueName = 'EPS') OR
    (CustomNodePollers_CustomPollers.UniqueName = 'Sent') OR
    (CustomNodePollers_CustomPollers.UniqueName = 'Condition')
    AND
    (CustomNodePollerAssignment_CustomPollerAssignment.InterfaceID = 0)
    )A
    GROUP BY NodeName, GroupName

    These values have similar data to what is being returned by the query above, I just want the results added to the relevant columns if I run the above query I get the result:
    NodeName Connector 
    Cache  EPS 
    Sent  Condition
    Node1               Connector1 
    1  0.1 
    123 Green
    Theses are values that meet PollerName CASE and where clause I want to extend this so that the same/similar set of values can be returned for the PollerName
    "Connector_1, Cache_1, EPS_1, Sent_1, Condition_1....  "  to the same columns but if I add these to the case and where clause all I get is doubled up columns. 
    I was thinking of possibly pushing the results into a table unless there is an easier/better way?

  • Saving the query result in XML format

    Hi All,
    I have a requirement to save the result of a query in XML format on the application server. Can we do this?
    Best Rgds,
    James.

    You can explore the possibility of the execution of query with XMLA:
    Official documentation
    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/1d71d190-0201-0010-239a-8b96516bf372
    and a good Blog
    /people/prakash.darji/blog/2006/09/04/work-with-xmla-web-service-for-bi-data-in-external-applications
    The result will be an SOAP message (XML) but also the request could be a SOAP message you can investigate if simulate the request by BW client or server.
    Regards,
    Sergio

  • SSMS Addin - How To Access Query Results Grid

    Hi,
    I want to access Query Result Grid(Results) in SSMS addin but I did'nt found anything.
    How can I access Query Result Grid, Rows, Columns and Cells when I executed a script?
    Thanks.

    SQL Server Management Studio currently supports query execution results to be displayed in three different ways: Results to Grid,
    Results to Text and Results to File.
    By default SQL Server Management Studio is configured to display query results in
    Grid format.  If you want to change the default to some other option follow these steps.
    1. In SQL Server Management Studio, under Tools menu, click
    Options as shown in the snippet below.
    2. In the Options dialog box, expand Query Results, expand
    SQL Server and then select General as shown in the snippet below. In the right side panel choose one of the three options shown below in the
    Default Destination for results drop down list and click
    OK to save the changes as shown in the snippet below. The changes will go into effect once you open a New Query window.
    Query Result Options for Results to Text
    In this format you have the option to display the results in a different tab as well as setting different output options.
    In the Options dialog box, expand Query Results, expand
    SQL Server and then select Results to Text tab as shown in the snippet below. In the right side panel first select the checkbox for
    Display results in a separate tab and then select the checkbox for
    Switch to results tab after the query executes and then click
    OK. If you would like to also display the column name in the result set then choose the option Include column headers in the result set
    as shown in the snippet below.
    The options you can use in the Text format are:
    Output format: - In the above snippet you could see that by default the output is displayed as columns aligned. Some of the other options that are available are Comma Delimited,
    Tab Delimited, Space Delimited and Custom Delimited. If you choose the Custom Delimited option in the Output format drop down list then you need to specify a character of your choice for the delimiter in the
    Custom delimiter text box.
    Include column headers when copying or saving the results: - If this option is selected, then whenever the results are copied to clipboard or it is saved to a file the column headers are also copied along with the results.
    Include the query in the result set: - The text of the query is displayed as part of query output under the messages tab.
    Scroll as results are received: - If this option is selected, then the display focuses on the most recently returned records at the end of the results set.
    Right align numeric values: - If this option is selected, then numeric values will be aligned to the right of the column.
    Discard results after query executes: - If this option is selected, then the query results are not displayed in the reviewing pane.
    Display results in a separate tab: - If this option is selected, then the result set after query execution will be displayed in a new tab instead of at the bottom of the query window.
    Switch to results tab after the query executes: - If this option is selected, then after the query execution the screen focus will be set to the results tab.
    Maximum number of characters displayed in each column: - This sets the maximum characters to display for any one column.  So if you have a column that is 500 characters this will only show the first 256 if you keep the default setting.
    Ref link :http://www.mssqltips.com/sqlservertip/2346/different-options-for-query-results-in-sql-server-management-studio/
    Ahsan Kabir Please remember to click Mark as Answer and Vote as Helpful on posts that help you. This can be beneficial to other community members reading the thread. http://www.aktechforum.blogspot.com/

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

  • Sending Email with Query Result (Table Format)

    Hi,
    I have a query result from ExecuteSQL task e.g.:
    ID Product
    1     Pencil
    2     Crayon
    3     Eraser
    I want to send an email with the above query result and I want it to be in Table format (such as in Microsoft Word with rows and column lines) to allow better readability to the receiver.
    Is it possible in SSIS?
    cherriesh

    It has to be HTML format . Read Tony's article
    http://sqlblogcasts.com/blogs/tonyrogerson/archive/2008/03/28/send-table-or-view-as-embedded-html-lt-table-gt-in-an-email-stored-procedure.aspx
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • Transferring multi-column query result into MS Excel

    Hello everybody!
    An ultimate novice in Oracle, with some database concepts, is here.
    The very first challenge; which I encoutered is that I want to transfer a Select query result, with multiple columns, into MS Excel sheet in a way that each field occupies a separate column in the sheet.
    I hope the forum members will show me a simple way to get around the problem.
    Thanks in anticipation.
    Rabi

    what will happen the query returns too many rows that
    Excel cant hold? I faced this problem and divided the
    report into "n" numbers based on the number of rows
    returned. Is there any internal logic for it in
    oracle?Excel is capable of holding 65536 rows of data on a sheet.
    Oracle is capable of writing data out to files.
    Oracle does not know or care whether the file it is writing is for Excel or any other product, as the file you are likely to be writing is CSV (comma seperate format) which is an open format, not specifically for Excel. If you need to limit the number of rows per CSV file then you will have to code this "business logic" into your code that produces the CSV file from Oracle.

  • Styling report rows using the query result

    Hello, is it possible to style a row in a report using the values returned by the underlying sql query?
    For example, if my query returns a column called ROW_CLASS I would like to use the placeholder #ROW_CLASS# to style the row, e.g. specifying the row class.
    Apparently such row attributes are not available to the apex developer. For each single column value I can set the "Tabular Form Attributes" and "Column Formatting", but I cannot set anything on the whole row.
    In other words I can style the TDs but not the TRs.
    I think this is clearly a "missing feature" that should be there, single the APEX engine that turns the query result into the HTML that displays the table can already access the column values via their placeholders. It would be sufficient to add some "row attributes" or "row class" field in the report.
    It could be used for lots of useful things, in my particular case, highlighting rows that contain errors.
    Also there is no way to perform that operation in CSS (e.g. get the TRs which contain at least one TD of class "ROWERROR"). I know it could be done in JS, but that's very messy for such a simple declarative problem.

    GChierico wrote:
    Hello, is it possible to style a row in a report using the values returned by the underlying sql query? Yes, for standard reports, using custom report templates: +{thread:id=2493917}+
    Interactive reports require the use of Dynamic Actions/jQuery.

  • How to implement 'Quick Select' column in a query result table?

    Hi,
    I have a requirement in OAF to design a search page with 'Quick select' column.
    One of the column in the query result table should be a quick select.
    Once user clicks on the quick select column, we have to navigate back to the previous page with the row value selected.
    Can anyone help me in this.
    Thanks.

    Also refer the search exercise in the toolbox tutorials.
    you can implement the quick search in the same way as update and delete buttons.
    --Prasanna                                                                                                                                                                                                                                                                                                                                   

  • Trying to display query results in 2 columns

    I am trying modify someone elses existing code to display the
    results of a query in 2 columns on a web page.
    The result of the existing code can be seen
    here
    Below is the code that I am trying to modify
    <table width="95%" border="0" align="center"
    cellpadding="4" cellspacing="2">
    <cfoutput query="News" startrow="#StartRow_News#"
    maxrows="#MaxRows_News#">
    <tr align="center" class="TEXTnormal">
    <td nowrap class="TEXTnormal
    style1">................................................................................. ...................................</td>
    </tr>
    <tr class="TEXTnormal">
    <td><table width="100%" border="0" cellpadding="0"
    cellspacing="0" class="TEXTnormal">
    <tr>
    <td><cfif News.ImageNameThumb gt 0>
    <a href="news_view.cfm?recordID=#News.ID#"><img
    src="uploadedimages/#News.ImageNameThumb#"
    alt="#News.ImageCaption#" hspace="8" vspace="0" border="0"
    align="left"></a>
    <cfelse>
    </cfif></td>
    <td><a href="news_view.cfm?recordID=#News.ID#"
    class="TEXThighlight">#News.Title#</a> -
    #News.Day#.#News.Month#.#News.Year# <br>
    #News.Summary#</td>
    </tr>
    </table></td>
    </tr>
    </cfoutput>
    </table>
    I have changed the above code to
    <table width="95%" border="0" align="center"
    cellpadding="4" cellspacing="2">
    <cfset LoopEndRow = CEILING(#EndRow_News#/2)>
    <cfloop
    index = "row"
    from = "#StartRow_News#"
    to = "#LoopEndRow#"
    step = "1">
    <tr class="TEXTnormal">
    <cfset breaker = 0>
    <cfloop
    index = "column"
    from = "0"
    to = "2"
    step = "1">
    <cfoutput query="News1" startrow="#StartRow_News#"
    maxrows="#MaxRows_News#">
    <td width="50%">
    <table width="100%" border="0" cellpadding="0"
    cellspacing="0" class="TEXTnormal">
    <tr>
    <td><cfif News1.ImageNameThumb gt 0>
    <a href="news_view.cfm?recordID=#News1.ID#"><img
    src="uploadedimages/#News1.ImageNameThumb#"
    alt="#News1.ImageCaption#" hspace="8" vspace="0" border="0"
    align="left"></a>
    <cfelse>
    </cfif></td>
    <td><a href="news_view.cfm?recordID=#News1.ID#"
    class="TEXThighlight">#News1.Title#</a> -
    #News1.Day#.#News1.Month#.#News1.Year# <br>
    #News1.Summary#</td>
    </tr>
    </table></td>
    <cfset breaker = breaker+1>
    <cfif breaker EQUAL 2>
    <cfbreak>
    </cfif>
    </cfoutput>
    </cfloop>
    </tr>
    </cfloop>
    </table>
    The results of this modified code can be see
    Here
    as you can see what I have done gives the correct number of
    columns (2)
    and the correct number of rows for the quantity of data (3)
    however each row shows only the first 2 pieces of data
    returned by the query
    Can I modify the query output so that it returns pieces of
    data off set by the amount of data already output?
    if so how?

    Right off the bat, you have 2 loops and I cant see why...
    So what is happening, is the outer loop is going 3 times...
    for 3 rows, then your inside loop is showing the first record, the
    Breaker var is breakign out of this loop after one row is shown.
    Then the main loop says do it again...
    You don't need nested loops to accomplish this, I dont
    understand why you would think of needing that...
    Looking at your code, just get rid of the outer loop... there
    is no need for it. You are already looping around the query object
    in the inner loop, thats all you need...
    Heres some psudeo code to help you out...
    loop around query maximum of 6 rows
    -- display data
    -- if recordcount EQ 2 or recordcount EQ 4 then
    ----- </tr><tr> <!--- this will make the new
    row --->
    end loop

Maybe you are looking for

  • WHY WILL ADOBE READER NOT OPEN PDF FILES?

    I have just downloaded free Adobe Reader X. When I try to open a pdf file I immediately get a Microsfoft box saying "Adobe Reader has encountered a problem and needs to close"  I have Windows XP. Has anyone else experienced the problem and what did y

  • How do I change the size of a Spry Input Text field box?

    I created an email reply box, and then I created a reply message box. How do I change the size of each box independantly of each other? All I have is one set of Spry CSS rules! Oh, and the Working with the Validation Text Rule widget file in the Help

  • Can't edit pics in iMovie

    Im trying til edit a photo in imovie (ken burns effect).  When i mark the clip, and push the ken burns button, i cant move the start and stop point .  Can anyone help?

  • GP with variable amount of steps

    Dear, What I'd like to have is a GP with a variable number of steps. How this is realised is of little importance, but what is most important is that we can (using the Webservice to start the GP) dynamically give for example 5 users, and then 5 steps

  • Lightroom in Adobe Application Manager

    For some reason I can't seem to install lightroom because its not in my adobe application manager.  Lightroom is one of the reasons why I purchased the Adobe Creative Cloud.  As a matter of fact, it used to be there because I had it installed at one