473,405 Members | 2,300 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,405 software developers and data experts.

Creating view (transpose records to columns)

I have a table like the following

Field1 Field2 Field3
------ ------- ------
x1 y1 z1
x1 y2 z2
x1 y3 z3
x1 y4 z4
x2 y1 z5
x2 y2 z6
x2 y3 z7
x2 y4 z8
x3 y1 z9
............and so on
I want to create a view with x1, x2, x3.. as unique
records; y1, y2, y3.... as fields; and z1, z2, z3.... as the values

When I do

CREATE VIEW xyz (y1, y2, y3, y4) AS
SELECT field1 ,
( SELECT field3 FROM table WHERE field2 = 'y1'),
( SELECT field3 .....
FROM table

I get the error that the sql query creates duplicate values. I think I
may have to do a join using distinct values of field1. I was looking
for some guidance with the join.

Thanks for your help in advance

(using SQLSERVER 2000)
Jul 20 '05 #1
2 4792
On 1 Oct 2004 06:09:37 -0700, Sanjay Asrani wrote:

I want to create a view with x1, x2, x3.. as unique
records; y1, y2, y3.... as fields; and z1, z2, z3.... as the values


Perhaps this is what you want (with DDL statements included):

create table T1 (
col1 varchar(10),
col2 varchar(10),
col3 varchar(10),
primary key (col1,col2)
)

insert into T1 (col1, col2, col3) values ('x1','y1','z1')
insert into T1 (col1, col2, col3) values ('x1','y2','z2')
insert into T1 (col1, col2, col3) values ('x1','y3','z3')
insert into T1 (col1, col2, col3) values ('x1','y4','z4')
insert into T1 (col1, col2, col3) values ('x2','y1','z5')
insert into T1 (col1, col2, col3) values ('x2','y2','z6')
insert into T1 (col1, col2, col3) values ('x2','y3','z7')
insert into T1 (col1, col2, col3) values ('x2','y4','z8')
insert into T1 (col1, col2, col3) values ('x3','y1','z9')

select col1,
min(case when col2='y1' then col3 else null end) [y1],
min(case when col2='y2' then col3 else null end) [y2],
min(case when col2='y3' then col3 else null end) [y3],
min(case when col2='y4' then col3 else null end) [y4]
from T1
group by col1

Here's the result:
col1 y1 y2 y3 y4
-----------------------------------
x1 z1 z2 z3 z4
x2 z5 z6 z7 z8
x3 z9 NULL NULL NULL
Jul 20 '05 #2
Ross Presser <rp******@imtek.com> wrote in message news:<5c**************@rpresser.invalid>...
Thanks a million Ross
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jonathan | last post by:
In the organisation where I work we currently have a public sanitised version of our existing HR personnel database. This is very widely used as the main source of employee info by many internal...
20
by: Neil | last post by:
I have an Access 2000 MDB file with a SQL 7 back end. I have a main table with 50,000 records; and I have a selections table with 50,000 records for each machine that uses the database (about...
1
by: jenny.rhodes | last post by:
Hello, Please can anyone guide me on how to transpose an access table where I have many records per id eg UserID Question Answer 1 1 a 1 2 d 1 3 ...
4
by: Filippo Pandiani | last post by:
I have a grid that shows the file list from a folder. On the postback, how do I get a Dataset from this grid? Thanks, Filippo.
1
by: Chris Smith | last post by:
Experience Posters, Sorry if this is not the right group to post this question. He is my issue; Is there a way without the use of 3rd party controls, to transpose the rows of a datatable to...
7
by: Leszek Gruszka | last post by:
I wrote an aplication that write something into tableA in sql2000. I want to write the same, but transposed into tableB. Someone can help me? Any example? *** Sent via Developersdex...
8
by: Leszek Gruszka | last post by:
I wrote code, that fill TableA with records by executenonquery. But i want to have second TableB, that will be transposed TableA. My code: Public Sub SQL_Wpis() Dim sSQL As String sSQL =...
5
by: hummer | last post by:
In an Access query, I am trying to do the following: I have this: Account # Issue Problem ------------------------------------------------------------------- 1 a...
5
by: jenniferhelen | last post by:
I have been searching threads for a while and found the instructions listed below many times, however when I get to step 6 and select to save, I always receive the following error, "The information...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.