473,387 Members | 1,757 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,387 software developers and data experts.

How to transpose (partially) table fields

I'm trying to partially transpose a table into a delimited file. I have the bcp figured out, but would like some help with transposing fields.

I have table this way (for purpose of simplicity I am showing only a few fields, in the real table I have quite a bit more) :

MyId,A_Qtr1,A_Qtr2,A_Qtr3,A_Qtr4,B_Qtr1,B_Qtr2,B_Q tr3,B_Qtr4,Field1,Field2,Field3

How would I approach it with select query so I'd get four (4) rows out of each row in the table that looks this way:

MyId,Qtr,QtrDataA,QtrDataB,Field1,Field2,Field3

Qtr would be a new field indicating the quarter (1-4), and QtrDataA, QtrDataB would have data from A_Qtr1 through A_Qtr4 and B_Qtr1 through B_Qtr5 fields.

Thanks,

Val
Dec 3 '10 #1
1 2080
This turns out to be easier than I thought (code below), now I need to figure out how to get those headers to my delimited file out of that select statement.

From the table I get them out as follows, it adds an extra pipe at the end, but that's fine (I stumbled upon this solution on some website, but have been sleeping many night since then and cannot remember who and where it was, sorry):

Expand|Select|Wrap|Line Numbers
  1. -- Export Headers from MyTable
  2. DECLARE @sql_MyTableHeaders VARCHAR(8000)
  3. SELECT @sql_MyTableHeaders = 'bcp "SELECT column_name from [MyDB].INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=''MyTable''" queryout e:\CSV_Exports\MyTableHeaders.csv -c -r"|" -T'
  4. EXEC "MyDB"..xp_cmdshell @sql_MyTableHeaders
  5. GO
  6.  

Here is the solution to my original question:

Expand|Select|Wrap|Line Numbers
  1. (SELECT 
  2.     MyId,
  3.     1 as Qtr,
  4.     A_Qtr1 as QtrDataA,
  5.     B_Qtr1 as QtrDataB,
  6.     C_Qtr1 as QtrDataC,
  7.     D_Qtr1 as QtrDataD,
  8.     E_Qtr1 as QtrDataE
  9. FROM dbo.MyTable)
  10. UNION
  11. (SELECT 
  12.     MyId,
  13.     2 as Qtr,
  14.     A_Qtr2 as QtrDataA,
  15.     B_Qtr2 as QtrDataB,
  16.     C_Qtr2 as QtrDataC,
  17.     D_Qtr2 as QtrDataD,
  18.     E_Qtr2 as QtrDataE
  19. FROM dbo.MyTable)
  20. UNION
  21. (SELECT 
  22.     MyId,
  23.     3 as Qtr,
  24.     A_Qtr3 as QtrDataA,
  25.     B_Qtr3 as QtrDataB,
  26.     C_Qtr3 as QtrDataC,
  27.     D_Qtr3 as QtrDataD,
  28.     E_Qtr3 as QtrDataE
  29. FROM dbo.MyTable)
  30. UNION
  31. (SELECT 
  32.     MyId,
  33.     4 as Qtr,
  34.     A_Qtr4 as QtrDataA,
  35.     B_Qtr4 as QtrDataB,
  36.     C_Qtr4 as QtrDataC,
  37.     D_Qtr4 as QtrDataD,
  38.     E_Qtr4 as QtrDataE
  39. FROM dbo.MyTable)
  40.  
  41.  
Thanks,

Val
Dec 3 '10 #2

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

Similar topics

4
by: Silas | last post by:
Hi, I use view to join difference table together for some function. However, when the "real" table fields changed (e.g. add/delete/change field). The view table still use the "old fields". ...
1
by: Chris Lutka | last post by:
I've been racking my brains all day over this. And I'm not the best at SQL either. I need a query that will produce the following results:...
3
by: DStark | last post by:
Hi! Is it possible to iterate through table fields in an ADP? What I'd like to do is: Sub PrintTableFields() Dim dbs As Object Dim tbl As AccessObject Dim fld As ???? Set dbs =...
3
by: MLH | last post by:
I have a query, qryAppend30DayOld260ies that attempts to append records to tblCorrespondence. When run, it can result in any of the following: appending no records, appending 1 record or appending...
1
by: keliie | last post by:
I have a relatively simple (I assume) issue which I am at a complete loss to address. My issues is: I want to populate fields in my tables with summary data from the same table. Let me explain: ...
0
by: JDMils | last post by:
I am having trouble finding the AutoNumber field of my database with this code. The code is used to replicate a specific table, reproducing all columns including indexes and Primary Keys (there is...
3
by: teser3 | last post by:
I am creating an Access 2000 database with a Web Front end that will have around 150 questions with 148 questions having just one number input (which will either be a 1 or a 2 or a 3 or a 4 or a...
10
by: Forest14 | last post by:
Hello! Happy Christmas/holidays to you all I have this huge table named "Positions" with more than 160 fields of which the fields are named with non obvious abbreviations. I have another table...
12
by: jenniferhelen | last post by:
I am working with a query that has 6 columns and 101 rows; I would like to transpose the rows and columns. I have tried using a crosstab query but Access limits the row "fields" to 3 and this was...
1
by: Vicente | last post by:
The form fields are bound correctly to corresponding table fields. This is a simple table with a simple form (3 fields)
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.