Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Retrive Column data in a row

Author  Topic 

shki_80
Starting Member

3 Posts

Posted - 2009-08-18 : 04:16:35
I have table with fields Q1, Q2, Q3, Q4 etc having range of values like A(Excellent), B(Very Good), C(Good), D(Fair), E(Not good).

Now I want to retrieve the data with single query like


A B C D E
Question 1 5 2 3 4 5
Question 2 6 0 1 0 3
Question 3 10 12 23 3 0


But the questions are in fields, Is ther anybody helps in this case.

Pls. anybody helps. What is the query to retrieve the above result.




Shakeel, Web Developer

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-08-18 : 04:21:22
use pivot function


select question,[a],[b],[c],[d],[e]
from tablename
pivot (max(values) for types in ([a],[b],[c],[d],[e]))p
Go to Top of Page

shki_80
Starting Member

3 Posts

Posted - 2009-08-18 : 04:31:42
We can shift column into rows through PIVOT, Currently I have seen that PIVOT function is used to shift rows into columns

Shakeel, Web Developer
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-08-18 : 04:54:29
quote:
table with fields Q1, Q2, Q3, Q4

hi, is there any column other than this 4 column?


Hope can help...but advise to wait pros with confirmation...
Go to Top of Page

shki_80
Starting Member

3 Posts

Posted - 2009-08-18 : 05:50:08
yes there are many columns Q1, Q2, Q3, .......Q26

But I want result like

A B C D E
Q1 5 2 0 6 8
Q2 6 3 6 9 2
Q3 9 6 9 7 2
.
.
.
Q26 10 8 9 6 9

Like column of the table into rows

Hope you understand. I think Pivot function is being used but how pivot column into rows.



Shakeel, Web Developer
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-08-18 : 07:06:44
for this , use dynamic cross tab it is useful for u
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-08-18 : 07:15:09
can u post some sample data ?
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-08-18 : 22:33:49
hm...is there any method other than dynamic query?


Hope can help...but advise to wait pros with confirmation...
Go to Top of Page
   

- Advertisement -