Solved

Returning Columns from a table vertically

Posted on 2008-06-17
2
175 Views
Last Modified: 2013-11-05
Hi,
  I have a table which I want to return its data with a query in a vertical way in a single column, for example I have this table

  table_name
  | Field_1 |  Field 2 | Field 3  |
  --------------------------------
  | Value1 | Value2  | Value3 |

 And I need to get
   
  Field_1 = Value1
  Field_2 = Value2
  Field_3 = Value3
 

Thxs in advance
0
Comment
Question by:negreteo
2 Comments
 
LVL 22

Expert Comment

by:dportas
ID: 21806652
How will you make sense of the result if there is more than one row? Is the order significant?

SELECT col, val
FROM
 (SELECT 'col1', col1
  FROM tbl
  UNION ALL
  SELECT 'col2', col2
  FROM tbl
  UNION ALL
  SELECT 'col3', col3
  FROM tbl) T(col,val)
/* ORDER BY ??? something unspecified ? */ ;

0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 500 total points
ID: 21809937
hi, similar to @dportas solution above, but, if I read that properly....

you need to do a union from selecting all the different columns one at a time... have taken the value content as being numeric, not exceeding 20 characters - you might need to adjust accordingly...

select new_column
from (
select 'Field_1 = '+convert(varchar(20),isnull(value1,0)) as new_column from mytable
union all
select 'Field_2 = '+convert(varchar(20),isnull(value2,0)) from mytable
union all
select 'Field_3 = '+convert(varchar(20),isnull(value3,0)) from mytable
--etc
) c
order by new_column
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL syntax error in VBA 11 32
ms sql stored procedure 22 77
how to fix this error 14 46
Run SQL statement in Microsoft Access 9 9
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now