Gday all,
I trying to do a SQL view which is sort of like a crosstab. I've seen the BOL example but can't get it to apply to my scenario.
I already have a view which outputs two columns thus:
Col1 Col2
1 B
1 C
1 F
2 D
3 B
3 A
4 E
4 B
4 C
4 D
What I want to show is one line for each Col1 value and another column for each possible value of Col2 & have a 'Y' when that line has the Col2 value. Like this...
Col1 A B C D E F
1 Y Y Y
2 Y
3 Y Y
4 Y Y Y Y
That is hard enough (for me anyway).
Further complexity when I have simplified the example above! In actual fact, the A, B, C, D etc are string values of whole words which I don't know what they'll be. I'm hoping that in the SELECT statement, I read the field value and make it the column headings, but I don't know how many unique values there'll be all up (I DO know it'll be a maximum of 30ish values).
Lets say I'm getting a list of customers and I dunno, say transportation preferences.
Using my sample above, my source data may read
J Smith Bike
J Smith Car
J Smith Bus
Mr Jones Tram
Mr Peters Moped
Mr Peters Bike
Mrs Smith Car
Mrs Smith Scooter
and I want to see
Person Bike Car Bus Tram Moped Scooter
J Smith Y Y Y
Mr Jones Y
Mr Peters Y Y
Mrs Smith Y Y
without even knowing what the different transportation types there are.
Hope this explains enough. I'm using SQL Server 2000.
cheers
Danster
I trying to do a SQL view which is sort of like a crosstab. I've seen the BOL example but can't get it to apply to my scenario.
I already have a view which outputs two columns thus:
Col1 Col2
1 B
1 C
1 F
2 D
3 B
3 A
4 E
4 B
4 C
4 D
What I want to show is one line for each Col1 value and another column for each possible value of Col2 & have a 'Y' when that line has the Col2 value. Like this...
Col1 A B C D E F
1 Y Y Y
2 Y
3 Y Y
4 Y Y Y Y
That is hard enough (for me anyway).
Further complexity when I have simplified the example above! In actual fact, the A, B, C, D etc are string values of whole words which I don't know what they'll be. I'm hoping that in the SELECT statement, I read the field value and make it the column headings, but I don't know how many unique values there'll be all up (I DO know it'll be a maximum of 30ish values).
Lets say I'm getting a list of customers and I dunno, say transportation preferences.
Using my sample above, my source data may read
J Smith Bike
J Smith Car
J Smith Bus
Mr Jones Tram
Mr Peters Moped
Mr Peters Bike
Mrs Smith Car
Mrs Smith Scooter
and I want to see
Person Bike Car Bus Tram Moped Scooter
J Smith Y Y Y
Mr Jones Y
Mr Peters Y Y
Mrs Smith Y Y
without even knowing what the different transportation types there are.
Hope this explains enough. I'm using SQL Server 2000.
cheers
Danster