473,385 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Query Data and Column Names

52
Hello all,

I'm trying to create some reports and I've run into a bit of a tricky query... I need 1 query that will basically return 2 columns...

column1 will be a list of certain column names in the table... like columnA, columnB, columnC etc....

column2 will be a list containing the value of the corresponding column name in column1... so the end result will look like

Expand|Select|Wrap|Line Numbers
  1. column1       column2
  2. --------     ---------
  3. columnA      valueA
  4. columnB      valueB
  5. columnC      valueC 
and so on....

Also, if someone can give me a solution to this I would also like to know if all the values would have to be of the same datatype.

Thanks in advance!!
Oct 14 '09 #1
2 2604
ck9663
2,878 Expert 2GB
You're going to need to build a dynamic query. You can get the column names from sys.columns or syscolumns and link that to sys.tables or sysobjects.

Happy Coding!!!

--- CK
Oct 14 '09 #2
nbiswas
149 100+
Lets say I have a table MyTable which has 2 columns Attribute and Value both of type varchar.

I will execute the following query

Expand|Select|Wrap|Line Numbers
  1. ;with cte1 as
  2. (
  3. select 
  4.     ROW_NUMBER() over(order by ordinal_position) as rn,  
  5.     column_name 
  6. from information_schema.columns
  7. where table_name = 'MyTable'
  8. )
  9. ,cte2 as
  10. (
  11. select    attribute, 1 as rn1,
  12.         value, 2 as rn2
  13. from MyTable
  14. )
  15. select    c1.column_name as column1
  16.         ,c2.attribute as column2 
  17. from cte1 c1
  18. inner join cte2 c2
  19. on c1.rn = c2.rn1
  20.  
  21. union all
  22.  
  23. select    c1.column_name as column1
  24.         ,c2.value as column2 
  25. from cte1 c1
  26. inner join cte2 c2
  27. on c1.rn = c2.rn2
This query is for SQL SERVER 2005+ .
In the first CTE I am picking up the column names and ensuring their orders via the row_number() function.
In the next part I am picking up the information from the MyTable table and explicity assigning 1 to the 1st column values(i.e. Attribute) here and 2 for the Value(i.e. 2nd column)

Then by using innerjoin followed by union I have accomplishing the task.

Note:- This is only an example and one of the many approaches. Take the idea if you have not get one as of now and put it as per your need.

Hope this helps.
Nov 8 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: 'bonehead | last post by:
Greetings, I'd like to figure out some syntax for retrieving the data from a table when I don't know all the of field names. What I do know are, the name of the table, the names of the primary...
2
by: Joe Gazda | last post by:
I'm a relative newbie to PHP, but have been able to put together some PHP code to generate a CSV/XLS file from a Query result. Now, I would like to include custom column names instead of the MySQL...
2
by: Lyn | last post by:
Hi, How do you bind the output columns from a UNION query when the fields from the two tables have different names? Consider this query (WHERE clauses omitted)... SELECT SurnameBirth,...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
5
by: Sam | last post by:
Hi, I have one table like : MyTable {field1, field2, startdate, enddate} I want to have the count of field1 between startdate and enddate, and the count of field2 where field2 = 1 between...
4
by: m.wanstall | last post by:
I have a crosstab query that compiles data for Months of the year. I have a stacked select query on top of that crosstab query that uses the latest 2 months data and exports it to a fixed length...
9
by: JJM0926 | last post by:
I'm trying to create a running totals query in access 97. I have followed the directions on how to do it from Microsofts website article id 138911. I took their code they had and replaced it with...
2
by: hwalker | last post by:
Hello all. Long time reader, first time poster :) I'm creating a repot that shows "the last two weeks of data the next 6 weeks of data, week over week". So, I have a calculated field called...
8
by: Paul H | last post by:
I want to base a form on a crosstab query. The query shows statistics for a user defined period. The column headings will look something like this: ClientID Month01 Month02 Month03 etc.. ...
9
by: Sinner | last post by:
Hi, I have a field name 'USER' in tableMAIN. How do I replace the user names with corresponding user names. I can do that in xl using vlookup but now I'm trying to find a way to do that in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.