Results 1 to 2 of 2
  1. #1
    Join Date
    May 2003
    Posts
    8

    Unanswered: show data horizontally

    my table contains

    table1:

    col1
    a
    b
    c


    while retreiving i want to show my data as ie horizontally or column wise

    a b c


    how is this possible

  2. #2
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1

    Crosstab query

    Cross-tab style queries such as you are trying to create are not built into SQL Server as they are in MS Access, but you can create them yourself. Books-Online (search for "Cross-Tab Reports") does a good job of explaining how. Here is some sample code from the help topic:

    SELECT Year,
    SUM(CASE Quarter WHEN 1 THEN Amount ELSE 0 END) AS Q1,
    SUM(CASE Quarter WHEN 2 THEN Amount ELSE 0 END) AS Q2,
    SUM(CASE Quarter WHEN 3 THEN Amount ELSE 0 END) AS Q3,
    SUM(CASE Quarter WHEN 4 THEN Amount ELSE 0 END) AS Q4
    FROM Northwind.dbo.Pivot
    GROUP BY Year
    GO

    The drawback is that you have to know the column headers ahead of time. If you don't know the headers, then you will need to construct a dynamic sql string including the headers and execute it. Very complex.

    blindman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •