Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

usp_TransposeNRows v2.0 Expand / Collapse
Author
Message
Posted Friday, January 29, 2010 5:07 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, February 11, 2013 8:24 AM
Points: 1, Visits: 151
Comments posted to this topic are about the item usp_TransposeNRows v2.0
Post #856492
Posted Wednesday, February 17, 2010 2:44 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 2:35 AM
Points: 28, Visits: 60
Hi, thanx for the contribution, this sounds good...

There are still some problems though, try following:

create table dbo.Something(
ID int identity(1,1) not null,
NVarCharValue nvarchar(50) null,
DateTimeValue datetime null
)
go

insert into dbo.Something (NVarCharValue, DateTimeValue) values ('1', null)
insert into dbo.Something (NVarCharValue, DateTimeValue) values ('2', null)
insert into dbo.Something (NVarCharValue, DateTimeValue) values ('71111111111', null)
insert into dbo.Something (NVarCharValue, DateTimeValue) values ('abc', null)
insert into dbo.Something (NVarCharValue, DateTimeValue) values ('xxxx', getdate())
insert into dbo.Something (NVarCharValue, DateTimeValue) values ('yyy', null)
insert into dbo.Something (NVarCharValue, DateTimeValue) values ('zzz', null)
go

select * from dbo.Something

exec dbo.usp_TransposeNRows 'Something',10

I will try to analyse where the problem is...

Cheers, Richard.
Post #866831
Posted Wednesday, February 17, 2010 6:23 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, February 11, 2013 8:24 AM
Points: 1, Visits: 151
Richard,

Thanks for your test case, and for offering to look into the issue.
I've done a quick check - the problem appears to be with the NULL values.
I'll dig further and post a fix soon.

Jeff
Post #866962
Posted Wednesday, September 10, 2014 5:22 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 11:10 AM
Points: 1,326, Visits: 19,276
Is this a personal preference to browse the data in this fashion? Are there particular cases (types of data, I guess) where you've found this to be advantageous?

---------------------------------------------------------
How best to post your question
How to post performance problems
Tally Table:What it is and how it replaces a loop

"stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."
Post #1612143
Posted Wednesday, September 10, 2014 6:58 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, September 11, 2014 8:08 PM
Points: 3, Visits: 64
Great stuff. As to why transpose, here is an OLTP example.

True transpose is useful to simplify several patterns. My fav was a test specification system where new tests came an went at technician's whims. The tests were in an ever changing list. Data needed to be captured in a grid, so test names transposed to columns and instances of results became rows. But storing results needed to be a row with test name and value. So spec transposed to result grid which transposed to the saved data. Oh, and the tests were in sets.

Easy in an array language, not so easy in relational. Arrays have implied axis that are not part of the array per se. Each position can be named, like an enum. In relational, the column axis is part of the meta. They are essentially enum named positions. Rows are ignored in the meta, which is correct as far as relational theory goes. In practice auto-identity and row_number() show up a lot, implying array like processing is desirable. Rows have business keys or surrogate keys or occasionally none at all. Row keys are akin to column names and like column names can be paired with positions. That is the key is an enum for a position.

By analogy, just like meta data for columns, meta data for rows makes sense. In the test specification system, we took advantage of row meta by naming the row positions as a composite of business keys. That simplified code. The row meta can be just a table 1-to-1 with rows of the original.

Having column and row meta data opens up emulating array transposes. That is (row, col) becomes (col,row). Perhaps some clever guy has or can make a fast way to use that fact to perform a transpose.

The ideal transpose will recreate the original when transposed back.

We also used dynamic sql, but without while loops. SQL imposed a column limit, thus a row limit, at the time I believe it was 1024 columns. It might be worthwhile to dynamically create an inline table valued function simply because Insert-Exec is so inelegant.
Post #1612434
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse