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
 General SQL Server Forums
 New to SQL Server Programming
 Displaying SQL data

Author  Topic 

klee
Starting Member

2 Posts

Posted - 2011-04-01 : 18:57:49
Hello,
I have this db that has 2 columns namely field_name (contains header info) and mask (contains actual data). Question is how can I get present the data in excel or something in the proper format by traversing field_name and populating mask data into the appropriate field_name column? Not sure if this makes sense at all.

EG data in sql
Cust_No | FieldName | Mask
ABC ACC # 1234
ABC ACC Name ABC Corp
ABC City xxxx
DEF ACC # 5678
DEF ACC Name DEF Corp
DEF City yyyy

Like to have
Cust_No | ACC # | ACC Name | City
ABC mask val


Thanks,
Kev

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-01 : 19:02:53
Sounds like you need to use the PIVOT function.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

klee
Starting Member

2 Posts

Posted - 2011-04-01 : 19:09:43
I've tried pivot. Unless i'm doing something wrong, I can't get the mask data to display. It does the count/sum/etc.
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-04-01 : 20:22:00
If you were trying to use the PIVOT keyword, there are couple of things that can cause it to not work.

1. You have to be using SQL 2005 or higher.
2. Compatibility level of your database should be 90 or higher.

If those are true, then the following should work:
select
*
from
YourTable
pivot
( max(Mask) for FieldName in ( [ACC @],[ACC Name],[City]))P

I wrote it out very carefully, it parses, but I have not tested it.
Go to Top of Page
   

- Advertisement -