T-SQL: Pivot help

Status
Not open for further replies.

StanGable

Ars Legatus Legionis
18,817
I have two tables:

Code:
CheckListItems

CheckListItemId     ItemName
1                   name1
2                   name2
3                   name3
4                   name4
5                   name5
...

and

Code:
CheckListItemResponses

CheckListResponse     CheckListItemId
Yes                   1
No                    1
Seattle               3
Boat                  4
Plane                 4

and I need to get to this (there is other SQL that I will tie these results to):

Code:
Name1     Name2     Name3     Name4     Name5 ...
Yes
No                  Seattle   Boat
                              Plane

I can't seem to get the PIVOT syntax correct- it has to be dynamic as I don't know all the ItemNames (and there could be hundreds).

Anyone help out?

Thanks in advance.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
seems wrong to even try if the data is remotely sparse since you are making the DB do loads of work (filling in the blanks) that could be better done in either the presentation layer (if it's just for display) or the 'model' layer where a sparse matrix (or unsparse if you're willing to throw memory at it and want the ease/simplicity of access) and go from there.

Some SQL gurus might disagree, and I wouldn't argue the point heavily, especially if you didn't plan on reading all the resulting rows (though why?). It just seems the effort spent on dynamically constructing your sql would be better spent dealing with it in memory in the client (where plenty of useful libraries likely exist to work with such structures), likely resulting in much less work for the database in both network, IO, CPU load.
 

StanGable

Ars Legatus Legionis
18,817
Good points, Shuggy. I have to press on with this task, though, so for now I'll just take your advice under advisement.

I got the results I need, but I did so using variables and EXEC(). My task is to write a VIEW that provides this information, so my method isn't going to work.

What I have so far is this:

I have this table in a temp table, #tmpResults:

Code:
QID                 ProcessID     PersonID     ItemName     Value
1                   2             1802         DateRequest  1/1/2013
2                   15            1987         DateRequest  2/7/2012
3                   3             1976         DateComplete 9/7/2013

And I got what I needed via the following:

Code:

Results:

Code:
QID     ProcessID     PersonID     DateRequest     DateComplete
1       2             1802         1/1/2013        
2       15            1987         2/7/2012
3       3             1976                         9/7/2013

Any idea how I could get that converted to work in a view (like maybe 'inlining' the variable definitions, which I can't seem to get to work)?

Thanks for any help or hints you can offer. I really appreciate it.
 

Chord

Ars Praetorian
461
[url=http:/civis/viewtopic.php?p=24861835#p24861835:77808c57 said:
StanGable[/url]":77808c57]Bump for the evening crowd to see if anyone can tell what I need to do differently there...

the .value uses xpath to extract the value out of the XML.

Since you already have it working using dynamic SQL just put the dynamic SQL into a Table Valued Function and call that via the view.

Not the fastest way of doing it, but depending on expected usage it may be sufficient.
 

StanGable

Ars Legatus Legionis
18,817
[url=http:/civis/viewtopic.php?p=24863671#p24863671:3rdyueih said:
Chord[/url]":3rdyueih]
[url=http:/civis/viewtopic.php?p=24861835#p24861835:3rdyueih said:
StanGable[/url]":3rdyueih]Bump for the evening crowd to see if anyone can tell what I need to do differently there...

the .value uses xpath to extract the value out of the XML.

Since you already have it working using dynamic SQL just put the dynamic SQL into a Table Valued Function and call that via the view.

Not the fastest way of doing it, but depending on expected usage it may be sufficient.

I guess I may have to do that. I have to get that solution OK'd so it's not most desirable, but if there's no way to get that to work...
 

Chord

Ars Praetorian
461
[url=http:/civis/viewtopic.php?p=24863907#p24863907:2ncu5klb said:
StanGable[/url]":2ncu5klb]
[url=http:/civis/viewtopic.php?p=24863671#p24863671:2ncu5klb said:
Chord[/url]":2ncu5klb]
[url=http:/civis/viewtopic.php?p=24861835#p24861835:2ncu5klb said:
StanGable[/url]":2ncu5klb]Bump for the evening crowd to see if anyone can tell what I need to do differently there...

the .value uses xpath to extract the value out of the XML.

Since you already have it working using dynamic SQL just put the dynamic SQL into a Table Valued Function and call that via the view.

Not the fastest way of doing it, but depending on expected usage it may be sufficient.

I guess I may have to do that. I have to get that solution OK'd so it's not most desirable, but if there's no way to get that to work...

If you can post the XML that is being generated we can then determine the xpath statement to use in the .value function.
 
[url=http:/civis/viewtopic.php?p=24864001#p24864001:32ey5dlk said:
StanGable[/url]":32ey5dlk]That little STUFF() just generates this (there is only one value in my data right now, but other values, when present, would be appended, surrounded by brackets and separated by commas):

[Requested]

Is that what you're after?

[edit]

Other possible values will be:

Signed Offer
Expiration Date
Date Received
Date Signed

etc...

I don't think you will be able to do this dynamically using .value in the way that you want.
 
Status
Not open for further replies.