SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Crosstab, dynamic number of columns, No Pivot function!


Crosstab, dynamic number of columns, No Pivot function!

Author
Message
manssourianm
manssourianm
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 118
hi, is it possible to make a query that will turn a result set into a crosstab and it will automatically know how many columns are needed, but WITHOUT using the Pivot function?
here is a table and data to illustrate what i'm talking about:


CREATE TABLE Lines
(
line varchar(5) NULL,
stopid varchar(5) NULL,
boardings int NULL
)


insert into lines (line, stopid, boardings)
values (2,1,58)
insert into lines (line, stopid, boardings)
values (2,2,37)
insert into lines (line, stopid, boardings)
values (2,3,40)
insert into lines (line, stopid, boardings)
values (2,4,41)
insert into lines (line, stopid, boardings)
values (4,1,13)
insert into lines (line, stopid, boardings)
values (4,2,21)
insert into lines (line, stopid, boardings)
values (4,3,25)
insert into lines (line, stopid, boardings)
values (4,4,20)
insert into lines (line, stopid, boardings)
values (4,5,12)
insert into lines (line, stopid, boardings)
values (4,6,9)
insert into lines (line, stopid, boardings)
values (4,7,4)
insert into lines (line, stopid, boardings)
values (4,8,3)
insert into lines (line, stopid, boardings)
values (4,9,1)
--------------------------------------
so when I select line 2, there would be four columns, each with the boardings number in it, when I select line 4, there would be nine columns.

Thanks in advance for any help given!!
-Martin
Erin Ramsay
Erin Ramsay
SSC Eights!
SSC Eights! (839 reputation)SSC Eights! (839 reputation)SSC Eights! (839 reputation)SSC Eights! (839 reputation)SSC Eights! (839 reputation)SSC Eights! (839 reputation)SSC Eights! (839 reputation)SSC Eights! (839 reputation)

Group: General Forum Members
Points: 839 Visits: 1099
Is there a particular reason you want to avoid the pivot function, even dynamically?
HowardW
HowardW
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2343 Visits: 9892
so when I select line 2, there would be four columns, each with the boardings number in it, when I select line 4, there would be nine columns.


Each row has a different number of columns? As long as you're only ever selecting one row at a time, this is a relatively simple piece of dynamic SQL and it doesn't have anything much to do with pivoting/cross-tabbing, which is converting multiple rows into multiple columns. You're generating multiple columns from a single row.

What's your sample output for a given row? What are the columns called?
manssourianm
manssourianm
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 118
thanks for the replies.

erin,
the reason i need to avoid th pivot function is because i'm using an older version that doesn't have it.

howard,
the name of the columns should be the value in [stopid]
a sample output when line 2 is chosen would be:

line 1 2 3 4
2 58 37 40 41

thanks again!!
HowardW
HowardW
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2343 Visits: 9892
Sorry, I misunderstood. When you said line, I thought you meant a row of data, rather than a Line ID in your data. You need to use dynamic SQL plus either PIVOT or a "Crosstab" solution using case statements. Have a look at Jeff Moden's article on the subject:

http://www.sqlservercentral.com/articles/Crosstab/65048/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search